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 No | Constructor and Description |
---|---|
1 | JTable() Construct a Default JTable with default values |
2 | JTable(int numRows, int numColumns) Construct JTable with numRows and numColumns empty cells using DefaultTableModel |
3 | JTable(Object[][] rowData, Object[] columnNames) Construct JTable with rowData and provide column names with columnNames |
4 | JTable(TableModel dm) Construct a JTable with TableModel dm as a data model |
5 | JTable(TableModel dm, TableColumnModel cm) Constuct JTable with TableModel dm and TableColumnModel cm |
6 | JTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm) Constuct JTable with TableModel dm and TableColumnModel cm and ListSelectionModel sm |
Methods of JTable in Java Swing
Sr No | Methods and Description |
---|---|
1 | int getRowCount() Returns Number of Rows in JTable |
2 | int getRowHeight() Returns Row Height of JTable |
3 | JTableHeader getTableHeader() Returns JTable Header |
4 | Object getValueAt(int row, int column) Returns the cell value |
All Constructors and method are available at official doc
Jtable in Java Swing Example to Show Data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package swing; import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; public class JTableExamle1 extends JFrame { Container container; JTable table; public JTableExamle1() { container = this.getContentPane(); String[] columnNames = {"First Name", "Last Name", "Age", "Gender", "Mobile No", "Mail Id"}; Object[][] rowData = { }; table = new JTable(rowData, columnNames); table.setBounds(10, 10, 600, 90); container.setLayout(new BorderLayout()); container.add(table.getTableHeader(), BorderLayout.PAGE_START); container.add(table, BorderLayout.CENTER); } public static void main(String[] args) { JTableExamle1 frame = new JTableExamle1(); frame.setTitle("JTable Example"); frame.setVisible(true); frame.setSize(700, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; public class JTableExamle1 extends JFrame { Container container; JTable table; JScrollPane scrollPane; public JTableExamle1() { container = this.getContentPane(); container.setLayout(null); String[] columnNames = {"First Name", "Last Name", "Age", "Gender", "Mobile No", "Mail Id"}; Object[][] rowData = { }; table = new JTable(rowData, columnNames); scrollPane = new JScrollPane(table); scrollPane.setBounds(10, 10, 600, 90); container.add(scrollPane); } public static void main(String[] args) { JTableExamle1 frame = new JTableExamle1(); frame.setTitle("JTable Example"); frame.setVisible(true); frame.setSize(700, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
Setting Column Width in Jtable Java Swing
table.getColumnModel().getColumn(index).setPreferredWidth(size);
is used to set column width.
column index start from zero.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; public class JTableExamle1 extends JFrame { Container container; JTable table; JScrollPane scrollPane; public JTableExamle1() { container = this.getContentPane(); container.setLayout(new BorderLayout()); String[] columnNames = {"First Name", "Last Name", "Age", "Gender", "Mobile No", "Mail Id"}; Object[][] rowData = { }; table = new JTable(rowData, columnNames); table.getColumnModel().getColumn(2).setPreferredWidth(30); table.getColumnModel().getColumn(5).setPreferredWidth(120); scrollPane = new JScrollPane(table); container.add(scrollPane); } public static void main(String[] args) { JTableExamle1 frame = new JTableExamle1(); frame.setTitle("JTable Example"); frame.setVisible(true); frame.setSize(700, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import java.awt.Container; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; public class JTableExamle3 extends JFrame { Container container; JTable table; JPanel mainPain, tablePane, infoPane; JScrollPane pane; JLabel row, column, selected; public JTableExamle3() { container = this.getContentPane(); String[] columnNames = {"First Name", "Last Name", "Age", "Gender", "Mobile No", "Mail Id"}; Object[][] rowData = { }; table = new JTable(rowData, columnNames); pane = new JScrollPane(table); tablePane = new JPanel(); tablePane.add(pane); mainPain = new JPanel(new GridLayout(2, 1)); row = new JLabel("No of Rows " + getRows()); column = new JLabel("No of columns " + getColumns()); selected = new JLabel(); infoPane = new JPanel(); infoPane.add(row); infoPane.add(column); infoPane.add(selected); mainPain.add(tablePane); mainPain.add(infoPane); container.add(mainPain); table.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(final MouseEvent e) { if (e.getClickCount() == 1) { JTable jTable = (JTable) e.getSource(); int row = jTable.getSelectedRow(); int column = jTable.getSelectedColumn(); Object valueInCell = jTable.getValueAt(row, column); selected.setText("Value at [" + row + "," + column + "]=" + valueInCell.toString()); } } }); } int getRows() { return table.getRowCount(); } int getColumns() { return table.getColumnCount(); } public static void main(String[] args) { JTableExamle3 frame = new JTableExamle3(); frame.setTitle("JTable Example"); frame.setVisible(true); frame.setSize(700, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
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));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import java.awt.BorderLayout; import java.awt.Container; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; public class JTableExamle4 extends JFrame { Container container; JTable table; JScrollPane pane; public JTableExamle4() { container = this.getContentPane(); container.setLayout(new BorderLayout()); String[] columnNames = {"First Name", "Last Name", "Age", "Photo"}; Object[][] rowData = { {"Ram", "Kumar", 22, new ImageIcon("d://icons/lock.png")}, {"Mohan", "Nath", 24, new ImageIcon("d://icons/lock.png")}, {"Rita", "Singh", 22, new ImageIcon("d://icons/lock.png")}, {"Anita", "Sharma", 21, new ImageIcon("d://icons/lock.png")} }; table = new JTable(rowData, columnNames); table.setRowHeight(50); table.getColumnModel().getColumn(3).setCellRenderer(table.getDefaultRenderer(ImageIcon.class)); pane = new JScrollPane(table); container.add(pane); } public static void main(String[] args) { JTableExamle4 frame = new JTableExamle4(); frame.setTitle("JTable Example"); frame.setVisible(true); frame.setSize(700, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |

how to display data from database in jtable in java using netbeans
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class JTableExamle5 extends JFrame { Container container; JTable table; JScrollPane pane; StudentDAO dao = new StudentDAO(); public JTableExamle5() { container = this.getContentPane(); container.setLayout(new BorderLayout()); DefaultTableModel dm = (DefaultTableModel) dao.fetchBySize(0, 10); table = new JTable(dm); table.setRowHeight(50); pane = new JScrollPane(table); container.add(pane); } public static void main(String[] args) { JTableExamle5 frame = new JTableExamle5(); frame.setTitle("JTable Example"); frame.setVisible(true); frame.setSize(700, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.swing.table.DefaultTableModel; public class StudentDAO { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; int st;//status public DefaultTableModel fetchBySize(int start, int size) { DefaultTableModel model = new DefaultTableModel(new String[]{"Id", "Name", "Gender", "MailId",}, 0); con = ConnectionFactory.getConnection(); try { String query = "select * from student"; System.out.println(query); ps = con.prepareStatement(query); rs = ps.executeQuery(); int i = 0; while (rs.next()) { long id = rs.getLong("id"); String name = rs.getString("name"); String author = rs.getString("gender"); String publication = rs.getString("mailid"); model.addRow(new Object[]{id, name, author, publication}); } } catch (Exception e) { e.printStackTrace(); } finally { try { con.close(); } catch (SQLException ex) { ex.printStackTrace(); } } return model; } } |
Download mysql-connector-java-5.1.14-bin.jar or any latest jar to connect java with mysql

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));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import java.awt.BorderLayout; import java.awt.Container; import javax.swing.DefaultCellEditor; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableColumn; import javax.swing.table.TableModel; public class Example6 extends JFrame implements TableModelListener { Container container; JTable table; JScrollPane pane; JComboBox<String> branchList; public Example6() { container = this.getContentPane(); container.setLayout(new BorderLayout()); table = new JTable(new MyTableModel()); table.setRowHeight(50); /*Creating combo box*/ branchList = new JComboBox<String>(); branchList.addItem("CSE"); branchList.addItem("IT"); branchList.addItem("Mech"); branchList.addItem("Civil"); branchList.addItem("EEE"); /*Setting table editor as combo box*/ TableColumn tc = table.getColumnModel().getColumn(4); tc.setCellEditor(new DefaultCellEditor(branchList)); pane = new JScrollPane(table); container.add(pane); table.getModel().addTableModelListener(this); } @Override public void tableChanged(TableModelEvent e) { int row = e.getFirstRow(); int column = e.getColumn(); TableModel model = (TableModel) e.getSource(); Object data = model.getValueAt(row, column); System.out.println("row " + row + " column " + column + " data " + data); } public static void main(String[] args) { Example6 frame = new Example6(); frame.setTitle("JTable Example"); frame.setVisible(true); frame.setSize(700, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class MyTableModel extends AbstractTableModel { String[] columnNames = {"First Name", "Last Name", "Age", "City", "Branch", "Topper"}; private Object[][] data = { {"Ram", "Kumar", 22, "Delhi", "CSE", new Boolean(true)}, {"Mohan", "Nath", 24, "Mumbai", "IT", new Boolean(false)}, {"Rita", "Singh", 22, "Kolkata", "ETC", new Boolean(true)}, {"Anita", "Sharma", 21, "Bhopal", "MECH", new Boolean(false)} }; public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { return data[row][col]; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int row, int col) { return true; } public void setValueAt(Object value, int row, int col) { data[row][col] = value; fireTableCellUpdated(row, col); } } |

How to Change Row Color of JTable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; public class JTableExample7 extends JFrame { Container container; JTable table; JScrollPane pane; public JTableExample7() { container = this.getContentPane(); container.setLayout(new BorderLayout()); String[] columnNames = {"First Name", "Last Name", "City", "Branch", "Percent"}; Object[][] rowData = { {"Ram", "Kumar", "Delhi", "CSE", 91}, {"Mohan", "Nath", "Mumbai", "IT", 77}, {"Rita", "Singh", "Kolkata", "ETC", 66}, {"Anita", "Sharma", "Bhopal", "MECH", 59}, {"Sohan", "Gupta", "Kolkata", "ETC", 90}, {"Ritesh", "Sao", "Delhi", "MECH", 88} }; table = new JTable(rowData, columnNames); table.setRowHeight(50); table.setDefaultRenderer(Object.class, new MyTableRenderer()); pane = new JScrollPane(table); container.add(pane); } public static void main(String[] args) { JTableExample7 frame = new JTableExample7(); frame.setTitle("JTable Example"); frame.setVisible(true); frame.setSize(700, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } //Custom DefaultTableCellRenderer class MyTableRenderer extends DefaultTableCellRenderer { // You should override getTableCellRendererComponent @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); c.setForeground(Color.WHITE); if (row % 2 == 0) { c.setBackground(Color.LIGHT_GRAY); } else { c.setBackground(Color.PINK); } return c; } } |

How to change Column Color of JTable
in the above example instead of row use column as below
1 2 3 4 5 6 | if (column % 2 == 0) { c.setBackground(Color.LIGHT_GRAY); } else { c.setBackground(Color.PINK); } |

How to Change Row Color of JTable on Row Select
1 2 3 4 5 6 7 8 | if (isSelected) { c.setBackground(Color.red); c.setForeground(Color.WHITE); c.setFont(new Font("Verdana", Font.BOLD, 16)); } else { c.setBackground(Color.WHITE); c.setForeground(Color.BLACK); } |

How to Change Cell Color of JTable on hasFocus
1 2 3 4 5 6 7 8 | if (hasFocus) { c.setBackground(Color.BLUE); c.setForeground(Color.WHITE); c.setFont(new Font("Verdana", Font.BOLD, 16)); } else { c.setBackground(Color.WHITE); c.setForeground(Color.BLACK); } |

Jtable change cell color based on value
prepareRenderer is used to access specific cell in JTable as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableModel; public class JTableExample7 extends JFrame { Container container; JTable table; JScrollPane pane; public JTableExample7() { container = this.getContentPane(); container.setLayout(new BorderLayout()); String[] columnNames = {"First Name", "Last Name", "City", "Branch", "Percent"}; Object[][] rowData = { {"Ram", "Kumar", "Delhi", "CSE", 91}, {"Mohan", "Nath", "Mumbai", "IT", 77}, {"Rita", "Singh", "Kolkata", "ETC", 66}, {"Anita", "Sharma", "Bhopal", "MECH", 59}, {"Sohan", "Gupta", "Kolkata", "ETC", 90}, {"Ritesh", "Sao", "Delhi", "MECH", 88} }; table = new JTable(rowData, columnNames) { @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component c = super.prepareRenderer(renderer, row, column); Object obj = getModel().getValueAt(row, column); if (obj.toString().equalsIgnoreCase("MECH")) { c.setBackground(Color.GREEN); } else { c.setBackground(Color.WHITE); } return c; } }; pane = new JScrollPane(table); container.add(pane); } public static void main(String[] args) { JTableExample7 frame = new JTableExample7(); frame.setTitle("JTable Example"); frame.setVisible(true); frame.setSize(700, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } public class MyTable extends JTable { public MyTable(TableModel model) { super(model); } @Override public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex) { Component rComp = super.prepareRenderer(renderer, rowIndex, vColIndex); if (getModel() != null) { Client client = ((ProfitAbilityTableModel) getModel()).getClient(rowIndex); if (client.getExpected() == client.getReceived()) { rComp.setBackground(new Color(139, 255, 182)); } else { rComp.setBackground(new Color(255, 139, 147)); } } return rComp; } } |

Q You can create a jtable using_____________
Using JTable Constructor as here table = new JTable(rowData, columnNames);