user3122136
user3122136

Reputation: 197

Swing design JTable auto refresh?

I have a question about my application's design.

I have 2 JTables (with my own TableModel) and I want to create a third table, which is built based on the selection in the other two tables. So when I start my application nothing is selected and this third table is supposed to be empty.

How do I find out what is selected in the two tables and how do I refresh the third one?

Upvotes: 0

Views: 182

Answers (1)

dic19
dic19

Reputation: 17971

How do I find out what is selected in the two tables and how do I refresh the third one?

Please note you have two different yet related problems here.

First one is about selection in tables (see User Selections section of How to Use Tables tutorial). Basically you need to attach a ListSelectionListener to the table's ListSelectionModel in order to listen for selection changes. Something like this:

JTable table1 = new JTable();
table1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        // Implementation here
    }
});

See JTable API:

The second problem is about refreshing/updating your third table. In order to accomplish this you need to work with the model of this third table by adding/removing/updating rows. Then this model will notify the view that something has changed and your table will be automatically updated.

Upvotes: 2

Related Questions