JTable in Java Swing with Examples

JTable in Java is a swing component, that is used to show data in two dimensional Grid.

It is used to display and edit data in cell format in table

Constructors of JTable

Sr NoConstructor and Description
1JTable()
Construct a Default JTable with default values
2JTable​(int numRows, int numColumns)
Construct JTable with numRows and numColumns empty cells using DefaultTableModel
3JTable​(Object[][] rowData, Object[] columnNames)
Construct JTable with rowData and provide column names with columnNames
4JTable​(TableModel dm)
Construct a JTable with TableModel dm as a data model
5JTable​(TableModel dm, TableColumnModel cm)
Constuct JTable with TableModel dm and TableColumnModel cm
6JTable​(TableModel dm, TableColumnModel cm, ListSelectionModel sm)
Constuct JTable with TableModel dm and TableColumnModel cm and ListSelectionModel sm

Methods of JTable

Sr NoMethods and Description
1int getRowCount()
Returns Number of Rows in JTable
2int getRowHeight()
Returns Row Height of JTable
3JTableHeader getTableHeader()
Returns JTable Header
4Object getValueAt​(int row, int column)
Returns the cell value

All Constructors and method are available at official doc

JTable Java Example to Show Data

Without JScroll Pane we have to add Table Header in container.

container.setLayout(new BorderLayout());
container.add(table.getTableHeader(), BorderLayout.PAGE_START);
container.add(table, BorderLayout.CENTER);

These lines are used to add header and data.

JScrollPane JTable Example in Java Swing

Here we set container layout to null.

Added JScrollPane to JTable and set bound in JScrollPane.

Setting Column Width in JTable

table.getColumnModel().getColumn(index).setPreferredWidth(size);

is used to set column width.

column index start from zero.

Java JTable Example to find Number of Rows and Columns and Finding JTable Cell Value on Clicking on Cell

Table have method getRowCount() and getColumnCount() to find the number of rows and colums in table.

table.addMouseListener is used to get mouseClick event if mouse it clicked one time them we get table’s getSelectedRow() and getSelectedColumn() to get clicked row and columns.

getValueAt(row,column) is used to get value at cell.

JTable Cell Value on Click
Fig: JTable Cell Value on Clicking

How to Show Image in JTable Cell

Here we are getting table’s Column Model and setting .setCellRenderer is getDefaultRenderer and setting ImageIcon.class to column.

table.getColumnModel().getColumn(3).setCellRenderer(table.getDefaultRenderer(ImageIcon.class));

Show Image in JTable Cell
Fig: Show Image in JTable Cell

DefaultTableModel to get data From Database

DefaultTableModel is used to create JTable. We are extracting data from database and assigning row to DefaultTableModel and printing it to Frame.

Download mysql-connector-java-5.1.14-bin.jar or any latest jar to connect java with mysql

DefaultTableModel to get data From Database
Fig: DefaultTableModel to get data From Database

Editing JTable Cell and using CheckBox and JComboBox in JTable

To make out JTable editable we created MyTableModel that extends AbstractTableModel.

Here we have override methods and one of the method in

public boolean isCellEditable(int row, int col) {
return true;
}

making true in above method allows user to edit cell value.

To Show JComboBox in table cell TableColumn is used as below.

TableColumn tc = table.getColumnModel().getColumn(4);
tc.setCellEditor(new DefaultCellEditor(branchList));

Editing JTable Cell  CheckBox and JComboBox in JTable
Fig: Editing JTable Cell CheckBox and JComboBox in JTable

How to Change Row Color of JTable

How to Change Row Color of JTable
Fig: How to Change Row Color of JTable

How to change Column Color of JTable

in above example instead or row use column as below

How to change Column Color of JTable
Fig: How to change Column Color of JTable

How to Change Row Color of JTable on Row Select

Change Column Color of JTable on Row Select
Fig : Change Column Color of JTable on Row Select

How to Change Cell Color of JTable on hasFocus

Change Cell Color of JTable on hasFocus
Fig: Change Cell Color of JTable on hasFocus

How to Color Specific Cell Based on value in JTable

prepareRenderer is used to access specific cell in JTable as below

How to Color Specific Cell Based on value in JTable
Fig: How to Color Specific Cell Based on value in JTable

Read More

  1. JLabel in Java Swing
  2. JComboBox in Java Swing
  3. JTable in Java Swing
  4. JTable Pagination in Java JDBC
  5. Registration Form in Java Swing
  6. Login form in Java Swing
  7. Simple Calculator in Java Applet
  8. Applet Life Cycle in Java