简体   繁体   English

TableModelListener介于两个不同的JTable之间

[英]TableModelListener between two different JTable

I am currently working with two JTable. 我目前正在使用两个JTable。 Each of them has its own custom TableModel. 他们每个人都有自己的自定义TableModel。

In the first JTable i have items that can be select (checkbox). 在第一个JTable中我有可以选择的项目(复选框)。 The second one starts empty. 第二个开始是空的。 When i check a line in the first table, the backgroundColor of the line changes (thanks to a preparedRenderer method) and i would like the content of the checked line to be added in the second JTable. 当我检查第一个表中的一行时,该行的backgroundColor发生了变化(感谢preparedRenderer方法),我希望在第二个JTable中添加选中行的内容。

I suppose i should use a TableModelListener but i don't see how i tell the second Table (and its model) to listen to the first one. 我想我应该使用TableModelListener,但我不知道我如何告诉第二个表(及其模型)听第一个。

Does anyone have some thought about that ? 有没有人想过这个?

Here is a sample of what the code i got look like : 这是我得到的代码的示例:

public class MyClass {

    private Model1 model1;
    private Model2 model2;

    private JTable table1;
    private JTable table2;

    public void myMethod()
    {
        table1 = new JTable();
        model1 = new Model1();
        table1.setModel(model1);

        table1.getModel().addTableModelListener(new TableModelListener()
        {
            @Override
            public void tableChanged(TableModelEvent e) 
            {
                //Here some code to enable a button when at least one row is checked
                //that works fine
            }
        });


        table2 = new JTable();
        model2 = new Model2();
        table2.setModel(model2);
    }
}

Thanks for your help :) 谢谢你的帮助 :)

working with two JTable. 使用两个JTable。 Each of them has its own custom TableModel. 他们每个人都有自己的自定义TableModel。

In the first JTable i have items that can be select (checkbox). 在第一个JTable中我有可以选择的项目(复选框)。 The second one starts empty. 第二个开始是空的。 When i check a line in the first table, the backgroundColor of the line changes (thanks to a preparedRenderer method) 当我检查第一个表中的一行时,该行的backgroundColor发生了变化(感谢preparedRenderer方法)

  • don't to use TableModelListener , override setValueAt ( Each of them has its own custom TableModel. ) for first XxxTableModel eg tableModelFirts.setValueAt(whatever_linked_in_second_model) 不要使用TableModelListener ,覆盖setValueAtEach of them has its own custom TableModel. )用于第一个XxxTableModel例如tableModelFirts.setValueAt(whatever_linked_in_second_model)

  • TableModelListener could not be used as notifier to change value in model, inside or outside TableModelListener不能用作通知程序来更改模型中的值,内部或外部

You can update model2 in the listener of model1 and refresh its table2: 您可以在model1的侦听器中更新model2并刷新其table2:

public class MyClass  {

    private Model1 model1;
    private Model2 model2;

    private JTable table1;
    private JTable table2;

    public void myMethod()
    {
        table1 = new JTable();
        model1 = new Model1();
        table1.setModel(model1);

        table2 = new JTable();
        model2 = new Model2();
        table2.setModel(model2);

        table1.getModel().addTableModelListener(new TableModelListener()
        {
            @Override
            public void tableChanged(TableModelEvent e) 
            {
                //Here some code to enable a button when at least one row is checked
                //that works fine
                Object aValue = "something"; //fill
                int row = 1; //fill
                int column = 1; //fill
                model2.setValueAt(aValue, row, column);
                model2.fireTableDataChanged();
            }
        });
    }
}

Thanks guys ! 多谢你们 !

I finally managed to get it working. 我终于设法让它工作了。 I used a mix of your solutions. 我使用了各种解决方案。

In my tableListener (from table1) i get the checked object and call the setValue method from my table2 that i have override to do what i want (because my object is a custom object). 在我的tableListener(来自table1)中,我得到了被检查的对象并从我的table2调用了setValue方法,我已经覆盖了我想做的事情(因为我的对象是一个自定义对象)。

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

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