Question

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

Was it helpful?

Solution

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.

OTHER TIPS

....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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top