Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The process for developing a JTable with both row and column headers involves the following steps:

  1. Create a new JTable object.
  2. Create a new TableModel object which defines the data structure of the table.
  3. Specify the data for the row headers and column headers in two separate arrays.
  4. Create a new JScrollPane object which will hold the JTable and allow the user to scroll through it.
  5. Set the row header of the JScrollPane to a new JList object containing the row headers.
  6. Set the column header of the JTable to a new JTableHeader object containing the column headers.
  7. Add the JScrollPane to the main JFrame.

Example code:

String[] columnHeaders = {"Header1", "Header2", "Header3"};
String[][] data = {{"Row1 Data1", "Row1 Data2", "Row1 Data3"}, 
                   {"Row2 Data1", "Row2 Data2", "Row2 Data3"}, 
                   {"Row3 Data1", "Row3 Data2", "Row3 Data3"}};
String[] rowHeaders = {"Row1", "Row2", "Row3"};

TableModel tableModel = new DefaultTableModel(data, columnHeaders);
JTable table = new JTable(tableModel);

JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setRowHeaderView(new JList(rowHeaders));
table.setColumnModel(new JTableHeader(table.getColumnModel()));

frame.add(scrollPane);