0

Is there a way to update a JTable during run time and without using the constructor?

I have a JTable that I have already added to my JPanel but I want to be able to define the columns and data at a later point in the code instead of immediately in the constructor method, is there a way to do this?

JPanel jp = new JPanel();
JTable table = new JTable();
jp.add(table);

Also, is there a way to convert a 2 dimensional array list into a 2 dimensional object array so I can use it in the JTable?

user2908849
  • 77
  • 2
  • 9

3 Answers3

3

I want to be able to define the columns and data at a later point in the code instead of immediately in the constructor method, is there a way to do this?

Use a TableModel To manage the data. See the "default" implementation DefaultTableModel. At any time you can use JTable#setModel and pass new TableModel.

JTable table = new JTable();
...
// Some time later
String[] headers = ...
Object[][] data = ...
DefaultTableModel model = new DefaultTableModel();
table.setModel(model);

Or you can create the table with the model, and at whatever time, call the model's methods, like (in the case of DefaultTableModel), setColumnIdentifiers, addRow, setDataVector

String[] headers = ...
DefaultTableModel model = new DefaultTableModel(headers, 0);
JTable table = new JTable(model);
...
// Some time later
Object[] row = ...
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addRow(row);
String[] newHeaders = ...
model.setColumnIdentifiers(newHeaders);

"Also, is there a way to convert a dimensional array list into a 2 dimensional object array so I can use it in the JTable?"

I've never heard the term "dimensional array list". But if you want to add an ArrayList<String> as a row, you can simply call list.toArray and add a row to the DefaultTableModel. If it's an ArrayList<YourType>, you may need to create a custom table model implementation. (See the link below)

See for more info:

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Sorry, I meant converting something like an ArrayList> into an object[][], for purposes of putting it into the JTable. (and I meant to say "2 dimensional array list", missed the 2, still not sure if that's an actual term though). – user2908849 Mar 06 '15 at 19:05
  • You can use `Vector>`, and use `DefaultTableModel.setDataVector(...)`. `Vector` works like `ArrayList`. It's dynamic with `add` method. – Paul Samsotha Mar 06 '15 at 19:09
  • OK I tried your method with the DefaultTableModel and it seems to work fine thanks. I want to ask another question, is it possible to have something in the same JTable that displays on the side of the table (e.g. the row number)? I made an image trying to explain what I want. http://i.imgur.com/iaAfDfn.jpg Basically similar to how we have headers for the column names, I want something where I can put row names. – user2908849 Mar 08 '15 at 21:16
1

Yes, you can set the table's contents at runtime. see JTable.setModel(TableModel)

Also, is there a way to convert a dimensional array list into a 2 dimensional object array so I can use it in the JTable?

I'm not sure what you mean by "dimensional array list" but you can either convert the data structure to a standard TableModel implementation like DefaultTableModel , or write a wrapper class that implements the TableModel interface. The wrapper class would read data directly from your data structure and expose its elements to the JTable via the methods defined in TableModel.

rob
  • 6,147
  • 2
  • 37
  • 56
0

You can use addRow method of DefaultTableModel to add data to table, at runtime. And use fireTableDataChanged to notify your JTable to update it's UI. Document here

mr.icetea
  • 2,607
  • 3
  • 24
  • 42
  • *"And use fireTableDataChanged"* - No never don't. The `fireTableXxx` methods should never be used outside of the model itself, it's the responsibility of the model to make the decisions about when it will make notifications of changes – MadProgrammer Mar 06 '15 at 01:31