简体   繁体   中英

Swing design JTable auto refresh?

I have a question about my application's design.

I have 2 JTable s (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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM