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)
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
(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
相关文章推荐
- How to get the data from a cell when I click on the GridButtonColumn of the same row
- How to fix: Recieve error "Unable to display this Web Part" on Data Views after restoring site from backup
- How to reduce the size of logging database OR How to purge the old data from Logging Database
- How to move a datafile from ASM to the file system
- 在服务器上远程链接另一台服务器的数据库的方法how to connet the database from the other host
- Data transfer from GPIO port to RAM buffer using DMA upon receiving a trigger signal on the timer capture input channel.
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- How to capture video frames from the camera as images using AV Foundation on iOS
- How to create svn database on the ubuntu
- How to handle the concurrency problems on ASP.Net Database
- How to configure dbconsole to display information from the ASM DISKGROUPS [ID 329581.1]
- Using .NET Databinding to Display Ink from a Database
- How to populate the datagrid on background thread with data binding by using Visual C#
- How do i prevent end users from connecting to the database other than my application?", version 8.1.5
- How to get the rowid when insert the data to Oracle database
- How to select the data type in SQLserver database such as varchar, nvarchar
- How to load a persisted workflow from the database
- How to Export and Import the AWR Repository From One Database to Another (文档 ID 785730.1)
- How to access HBase from spark-shell using YARN as the master on CDH 5.3 and Spark 1.2
- How to Record your Voice from the Microphone on Ubuntu/Linux Mint