0

我在选择 JTable 实例上的行时遇到问题。

这就是我想要的:
我想要两个相等的表,行中的数据相同。当您在第一个表上选择一行时,例如第三行,我还希望自动选择第二个表上的第三行。我已经通过向 JTable 添加一个ListSelectionListener来解决这个问题,该 JTable 更新了一个只保存所选值的类。然后该类使用第一个选定的值触发另一个 JTable。

我的问题是:
当用户对其中一张表上的行进行排序时,就会出现我的问题。然后视图会发生变化,但模型中的底层对象不会发生变化,它们的顺序与以前相同。

假设我启动应用程序时表格如下所示:

Column_1_header_in_table_1            Column_1_header_in_table_2
   Peter                                    Peter  
   John                                     John
   Steve                                    Steve

在选择第一行时table 1(即Peter),则应选择包含Peter的行,该行table 2也是第一行。
但是,如果我按下列标题table 1以便对列进行排序,那么该表的视图将变为:

Column_1_header_in_table_1            Column_1_header_in_table_2
   John                                     Peter  
   Steve                                    John
   Peter                                    Steve 

现在,如果我选择第一table 1(即 John),table 2则将选择第一行(即 Peter)。table 1但我希望在上选择与 in 同名的行table 2,即第 2 行table 2

有什么方法可以用来解决这个问题吗?


编辑

好的,我将尝试用我在没有编辑器的情况下编写的以下代码来描述我的解决方案,所以我可能包含一些错误。但我只想从概念上展示它现在是如何工作的。首先,我完成了 MyTable 实现的这个接口:

public interface TableUpdater {
    public void updateTable(int age);
}

PersonH​​older 类只保存最后选择的值,并在从第一个选择新值时触发另一个表。

public class PersonHolder {
    private static int age;
    private List<TableUpdater> tables = new ArrayList<>();

    public static void subscribe(TableUpdater table){
         tables.add(table);
    }


    public static void setValue(int value){
        age = value;
        for(TableUpdater table : tables) {
            table.updateTable(age);
        } 
    }
    public static int getValue(){
        return age;
    }
}

然后我们有表本身:

public class MyTable extends JTable implements TableUpdater {
    public MyTable {
        table.getSelectionModel().addListSelectionListener(new MySelectionListener());
        PersonHolder.subscribe(this);
    }
...
     @Override
     public void updateTable(int age) {
           this.getSelectionModel().setSelectionInterval(age, age);
     }
     private class MySelectionListener implements ListSelectionListener {
           public void valueChanged(ListSelectionEvent e) {
               Person p = (Person)getValueAt(e.getLastIndex(), 0);
               PersonHolder.setValue(p.getAge());
           }
     }
}
4

1 回答 1

5

您应该使用:javax.swing.JTable.convertRowIndexToModel(int)将当前选择索引转换为模型索引值,然后在另一个表中,将模型索引转换回视图索引,javax.swing.JTable.convertRowIndexToView(int)并将该索引设置为选定行(假设两个表中的模型相同或等效,否则您将不得不根据值进行查找)。

这是我想到的一个例子(我什至改组了两个 JTable 的 baseModel 并在另一个中执行索引查找):

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

public class TestSortedTable {

    class MyTableModel extends AbstractTableModel {

        private List<Person> baseModel;

        public MyTableModel(List<Person> baseModel) {
            super();
            this.baseModel = new ArrayList<Person>(baseModel);
        }

        @Override
        public int getRowCount() {
            return baseModel.size();
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
            case 0:
                return "First Name";
            case 1:
                return "Last Name";
            }
            return null;
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            switch (columnIndex) {
            case 0:
                return getPersonAtIndex(rowIndex).getFirstName();
            case 1:
                return getPersonAtIndex(rowIndex).getLastName();
            }
            return null;
        }

        public Person getPersonAtIndex(int rowIndex) {
            return baseModel.get(rowIndex);
        }

        public int getIndexOfPerson(Person person) {
            return baseModel.indexOf(person);
        }

    }

    protected void initUI() {
        List<Person> personModel = new ArrayList<TestSortedTable.Person>();
        personModel.add(new Person("John", "Smith"));
        personModel.add(new Person("Peter", "Donoghan"));
        personModel.add(new Person("Amy", "Peterson"));
        personModel.add(new Person("David", "Anderson"));
        JFrame frame = new JFrame(TestSortedTable.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Collections.shuffle(personModel);
        final MyTableModel table1Model = new MyTableModel(personModel);
        final JTable table1 = new JTable(table1Model);
        table1.setAutoCreateRowSorter(true);
        Collections.shuffle(personModel);
        final MyTableModel table2Model = new MyTableModel(personModel);
        final JTable table2 = new JTable(table2Model);
        table2.setAutoCreateRowSorter(true);
        table1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    return;
                }
                int index = table1.getSelectedRow();
                if (index > -1) {
                    int table1ModelIndex = table1.convertRowIndexToModel(table1.getSelectedRow());
                    Person p = table1Model.getPersonAtIndex(table1ModelIndex);
                    int table2ModelIndex = table2Model.getIndexOfPerson(p);
                    int indexInTable2 = table2.convertRowIndexToView(table2ModelIndex);
                    table2.getSelectionModel().setSelectionInterval(indexInTable2, indexInTable2);
                }
            }
        });
        table2.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    return;
                }
                int index = table2.getSelectedRow();
                if (index > -1) {
                    int table2ModelIndex = table2.convertRowIndexToModel(table2.getSelectedRow());
                    Person p = table2Model.getPersonAtIndex(table2ModelIndex);
                    int table1ModelIndex = table1Model.getIndexOfPerson(p);
                    int indexInTable1 = table1.convertRowIndexToView(table1ModelIndex);
                    table1.getSelectionModel().setSelectionInterval(indexInTable1, indexInTable1);
                }
            }
        });
        frame.add(new JScrollPane(table1), BorderLayout.WEST);
        frame.add(new JScrollPane(table2), BorderLayout.EAST);
        frame.pack();
        frame.setVisible(true);
    }

    public class Person {
        private final String firstName;
        private final String lastName;

        public Person(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestSortedTable().initUI();
            }
        });
    }

}
于 2013-01-31T19:50:17.163 回答