JTable Java Swing Example defaulttablemodel cell color

JTable in Java is a swing component, that is used to show data in a two-dimensional Grid. Here we will discuss JTable Java Swing Example with various parameters

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

Constructors of JTable in Java Swing

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 in Java Swing

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 Swing Example to Show Data

Without JScroll Pane we have to add a Table Header in the 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 in Java Swing Example

Here we set container layout to null.

Added JScrollPane to JTable and set bound in JScrollPane.

Setting Column Width in Jtable Java Swing

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

is used to set column width.

column index start from zero.

Jtable Java Swing 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

How to display data from database in jtable in java using DefaultTableModel

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

DefaultTableModel Example: how to display database values in jtable in java

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 the above method allows users to edit cell values.

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 the above example instead of 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

Jtable change cell color based on value

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