您的位置:首页 > 移动开发 > Objective-C

How to display the data from database on JTable

2011-07-21 15:31 661 查看
Java.Swing.JTable has several constructors and I have only tested two of them, which are 
(1) JTable(Object[][] rowData, Object[] columnNames)
(2)
JTable(TableModel dm)
 
 If you have the constant data object[][], the first one is the choice. However, most of the time we need to dynamiclly retrieve data from database and put it into JTable. It is better to use (2) in that condition since it can supply a better package.
The class "TableModel" requires implementing several methods, e.g "getColumnName(int i)", "getColumnCount()" and so on, so in the class you have to explicitly write those functions. The link below is just an example:
 http://www.java2s.com/Code/Java/Swing-JFC/DisplayResultSetinTableJTable.htm
In the constructor of TableModel, you should fill the container, no matter in the form of vector, array or map, with the data from database. In other words, you have to instanciate the container which records the data retrieved from database when the TableModel object is first established. After building the TableModel class, you can simply load the data to JTable by "JTable table = new JTable(TableModel)". Link below from Sun talks about what and how to use TableModel: http://download.oracle.com/javase/tutorial/uiswing/components/table.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐