0

I have created a JTable table

String[] ColumnNames = { "Name" , "Father Name", "D.O.B" };
Object[][] data;    
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
JPanel panel = new JPanel();
panel.add(pane);

So what I want to do is when I populate Object data (not initialized yet, but it get populated correctly no issue here), table rows should also be updated with new data and when I search again and If no result is found then table should be empty.

In short I want to update table every time I hit "Search" button. Increase/Decrease in rows.

Rahul
  • 44,383
  • 11
  • 84
  • 103
Haris Mehmood
  • 854
  • 4
  • 15
  • 26

1 Answers1

2

Create a new TableModel and set it to the instance of JTable on the view. The change will automatically update the table.

This will require you to make table an instance variable within the class...

How to use tables might help

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Why can't I do this with DefaultTableModel ? – Haris Mehmood Nov 20 '13 at 07:27
  • It will depend. Do you want to do a selective update, remove a few rows, add a few rows, or completely update the entire data (remove every thing and start afresh)? – MadProgrammer Nov 20 '13 at 07:28
  • I have to start fresh every time. – Haris Mehmood Nov 20 '13 at 07:30
  • So the simplest thing is to use a new `TableModel` (like a new `DefaultTableModel`), as removing the rows and adding the rows would trigger at least two updates, where as setting the model would do the same thing in a single update – MadProgrammer Nov 20 '13 at 07:32
  • I am using model.firetableDataChanged(); but its not updating the table accordingly. – Haris Mehmood Nov 20 '13 at 08:06
  • 1- Make sure that you are only calling `firetableDataChanged` from within the context of the model (not externally) and 2- That the model you are referencing is the same model that the table is using – MadProgrammer Nov 20 '13 at 08:08