简体   繁体   English

Swing设计JTable自动刷新?

[英]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. 我有2个JTable (带有我自己的TableModel ),我想创建第三个表,该表是根据其他两个表中的选择构建的。 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. 基本上,您需要将ListSelectionListener附加到表的ListSelectionModel上 ,以侦听选择更改。 Something like this: 像这样:

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

See JTable API : 参见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. 然后,此模型将通知视图某些内容已更改,并且您的表将自动更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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