从 JTable 获取对象
Getting an object from JTable
我想从 JTable 中获取有关用户的信息。一切都很好,但是当我按名称对数组进行排序时,对象读取错误。例子:
Here is everything ok
以上一切正常。我从 table 中选择 4 个值,显示用户列表中的 4 个项目。展示了两本书。但现在我按 'number of loans' 排序,我选择有两次贷款的用户。但是数组读取为 'you chose the first value' 并显示用户列表中的第一个值。
After sorting
我想在从板中选择后接收特定用户。谢谢
我的代码:
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,
为什么要对数组进行排序?
数据存储在 TableModel 中。排序应该在 table 上进行,而不是在某些外部数组中的数据上进行。你不想要两个地方的数据,保持数据同步太难了。
阅读 Sorting and Filtering 上的 Swing 教程部分了解更多信息。
如果您想从 table 中获取排序后的值,您可以使用:
table.getValueAt(table.getSelectedRow(), theColumn);
我想从 JTable 中获取有关用户的信息。一切都很好,但是当我按名称对数组进行排序时,对象读取错误。例子: Here is everything ok
以上一切正常。我从 table 中选择 4 个值,显示用户列表中的 4 个项目。展示了两本书。但现在我按 'number of loans' 排序,我选择有两次贷款的用户。但是数组读取为 'you chose the first value' 并显示用户列表中的第一个值。
After sorting
我想在从板中选择后接收特定用户。谢谢
我的代码:
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,
为什么要对数组进行排序?
数据存储在 TableModel 中。排序应该在 table 上进行,而不是在某些外部数组中的数据上进行。你不想要两个地方的数据,保持数据同步太难了。
阅读 Sorting and Filtering 上的 Swing 教程部分了解更多信息。
如果您想从 table 中获取排序后的值,您可以使用:
table.getValueAt(table.getSelectedRow(), theColumn);