0

I have been reading the forums extensively and tried numerous methods on how to solve this problem.

The Problem: I made a custom AbstractTableModel so I can control the way my data is displayed. It is simply stored in a String[][]. It connects to a DB using JDBC to populate the initial data. So, when my user (through the Swing GUI) searches for a something, behind the scenes I simply construct a query to query the DB and it returns a ResultSet. Now, I want to display this new Data on the JTable.

Research: I read a lot about listeners and firing updates and things. But I have read and reread them and still do not completely understand them. When I set a new model to the JTable, I actually want to KEEP my custom AbstractTableModel I created and just update the data, is this possible (maybe with public methods inside AbstractTableModel class that I can create)?

Additional Questions: Should I be using a listener for this functionality? Do I need to add listeners or observers?

Thanks, I really appreciate it! Rich

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Richie Episcopo
  • 383
  • 1
  • 3
  • 15

2 Answers2

1

Research: I read a lot about listeners and firing updates and things. But I have read and reread them and still do not completely understand them. When I set a new model to the JTable, I actually want to KEEP my custom AbstractTableModel I created and just update the data, is this possible (maybe with public methods inside AbstractTableModel class that I can create)?

Yes it's possible. Basically, you need to either...

  1. Provide functionality in your custom table model to add and remove rows or
  2. Construct a new instance of the custom table model using the new data

Additional Questions: Should I be using a listener for this functionality? Do I need to add listeners or observers?

It's difficult to say without context. I would say, generally no. In your case it's probably simpler to create a SwingWorker, perform the database query and either populate a new table model and return it from the doInBackground method or use the publish/process methods to update the existing model.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

....so I can control the way my data is displayed

A model is used to store data. The view (JTable) displays the data.

It is simply stored in a String[][].

There is no need to create a custom TableModel. Just use the DefaultTableModel.

I actually want to KEEP my custom AbstractTableModel

Use the setDataVector() method of the DefaultTableModel

See the Table From Database Example in the Get the Code section.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • The reason I want to keep my AbstractTableModel is because I have a row of check boxes. This functionailyt isn't provided by a DefaultTableModel. So how would you reccomend I handle updating the table rows with this? – Richie Episcopo Jul 12 '13 at 17:30
  • @RichieEpiscopo, yes you can display a row of check boxes in the JTable when using the DefaultTableModel. Again I will stress the model stores the data, the table renders the data. You store Boolean values in the model, then you override the `getColumnClass()` method to return the appropriate class and the appropriated render/editor will be used by the table. – camickr Jul 12 '13 at 17:35
  • Thank you for all your help. You also cleared up a few things for me. I got it working now! – Richie Episcopo Jul 16 '13 at 15:10