JTable in Java Swing

Home  »  Java  »  Advance Java  »  Java Swing II  »  JTable


A table is a component of the swing class that displays rows and columns of data. Tables are implemented by using the JTable class, which extends JCOmponents. The JTable component of swing represents a two dimensional grid of objects. A JTable displays data in rows and column format. You can edit data values in a table as well. The JTable class of swing represents swing tables. The JTable class extends the JCompoent class is stored in the javax.swing package. The syntax to create a table in Java Swing is:



The various constructors that swing provides to create a JTable are:

  • JTable(): Creates an empty JTable.

  • JTable(int row, int col): Creates a JTable with the size specified as arguments. The arguments row and col are used to specify the number of rows and columns in the JTable.

  • JTable(Object[][] row_data, Object[] col_names): Creates a JTable with the rowdata and the column names as specified in the arguments of the constructor.

  • JTable(TableModel d): Creates a JTable that is initialised with the TableModel passed as an argument to the constructor.

  • JTable(TableMode d, TableColumnModel c): Crates a JTable that is initialised with the TableModel and TableColumnModel passed as an argument to the constructor.

  • JTable(TableModel d, TableColumnModel c, ListSelectionModel s): Crates a JTable that is initialised with the TableModel, TableColumnModel and ListSelectionModel passed as an argument to the constructor.

  • JTable(Vector row_data, Vector column_names): Creates a JTable that displays the components of the vector arguments passed to the constructor. The argument, row_data is used to specify the data that is represented as rows in the JTable. The arguments, column_name specifies the name of columns in the JTable.



Table Header

A table header is the top portion of each column in a table where the column title is displayed. In Swing, the column header is represented by the class JTableHeader. The header of a table object can be retrieved by calling the method getTableHeader(). There is another method called getDefaultTableHeader() that returns the table header when no other header has been assigned. A different table header can be assigned by using the class setTableHeader().

Simple Table Code Example
The following example creates a very simple table and adds it to an applet. The program uses the table constructor that requires the number of rows and columns to be specified. The table is editable by default.



C:\>jdk1.4\bin>javac JTableDemo1.java
C:\>jdk1.4\bin>appletviewer JTableDemo1.java

Output






Table Built with Arrays Code Example

The following example creates a Swing table in which the data to be displayed in a table has been stored in array. The column array is simply in array of heading to be displayed in each column. The rows are represented by an arrays because each row itself is an array of data items to be displayed. The table has been positioned in an applet.



C:\>jdk1.4\bin>javac JTableDemo2.java
C:\>jdk1.4\bin>java JTableDemo2

Output





Java Swing-II


JOptionPane Class Input Dialog Boxes JSliders
JProgressBars JTables JTabbedPane
JMenu JPopupMenu JToolBar
JFileChooser JScrollBar JScrollPane
JRootPane JTextPane JTree
JSplitPane JDesktopPane JEditorPane




1 comment: