简体   繁体   English

从 JTable 获取 object

[英]Getting an object from JTable

I want to get information about user from the JTable.我想从 JTable 获取有关用户的信息。 Everything is okay, but when I sort the array by name, the objects get read wrong.一切都很好,但是当我按名称对数组进行排序时,对象被读取错误。 Example: Here is everything ok示例:一切正常

Above everything is ok.最重要的是一切正常。 I choose 4 value from the table, shows me 4 items from the Users list.我从表中选择 4 个值,显示用户列表中的 4 个项目。 Two books are displayed.展示了两本书。 But now I sort by 'number of loans', and i choose user with two loans.但现在我按“贷款数量”排序,我选择有两笔贷款的用户。 But the array reads as 'you chose the first value' and shows the first value from the User list.但是该数组读取为“您选择了第一个值”并显示了用户列表中的第一个值。

After sorting排序后

I'd like to receive a specific user after selecting from the board.从董事会中选择后,我想接收特定用户。 Thanks.谢谢。

My code:我的代码:

        tablicaWypozyczen.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                int row = tablicaWypozyczen.rowAtPoint(e.getPoint());
                int col = tablicaWypozyczen.columnAtPoint(e.getPoint());
                if (row >= 0 && col >= 0) {
                    JOptionPane.showMessageDialog(null, Dane.uzytkownicyZWypozyczeniami.get(row).toString(), "Informacje o użytkowniku", 1);
                    System.out.println(tablicaWypozyczen.getSelectedRow());
                }
            }
        }
    });

but when I sort the array by name,但是当我按名称对数组进行排序时,

Why are you sorting the Array?为什么要对数组进行排序?

Data is stored in the TableModel.数据存储在 TableModel 中。 The sorting should be done on the table, not on data in some external array.排序应该在表上完成,而不是在某些外部数组中的数据上。 You don't want data in two places, it is too hard to keep the data in sync.您不希望数据在两个地方,保持数据同步太难了。

Read the section from the Swing tutorial on Sorting and Filtering for more information.阅读 Swing 教程中有关排序和过滤的部分以获取更多信息。

If you want to get the sorted value from the table you use:如果要从使用的表中获取排序值:

table.getValueAt(table.getSelectedRow(), theColumn);

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

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