在 JTable 上使用自动排序并返回正确的数据

发布于 2024-09-16 12:34:50 字数 626 浏览 4 评论 0原文

我有一个 JTable,其模型是自定义的,扩展了 AbstractTableModel。

通过这种方式,我获取数据的方式是来自列表类。

public Object getValueAt(int row, int col)
{
 if ( col == 0 ) {
  return searchThings.getThing(row).getTitle();
 } else if ( col == 1 ) {
  return searchThings.getThing(row).getDate();
 } else {
  return "";
 }
}

这工作得很好,日期和标题都是正确的。它们都可以使用自动方法进行排序(单击标题进行排序,您可以很好地交换列等)并且所有内容都可以正确显示。

问题是:如何返回正确的项目引用或数据集?因为他们不保留原始索引。

示例“test 2”是条目 1。现在,如果我不对表进行排序,我可以返回该行并使用该数字来查找列表中的位置。但如果我按日期排序,测试 2 现在是索引 4,并且我得到了错误的数据集引用(因为我直接查看原始来源)。

现在必须有一种方法可以找出哪个是原始数据索引,因为表显示了正确的数据!请帮忙!

:)

提前致谢!

I've got a JTable which model is custom that extends AbstractTableModel.

In this the way I get the data is from a list class.

public Object getValueAt(int row, int col)
{
 if ( col == 0 ) {
  return searchThings.getThing(row).getTitle();
 } else if ( col == 1 ) {
  return searchThings.getThing(row).getDate();
 } else {
  return "";
 }
}

This works perfectly, and the dates and titles are all correct. They can all be sorted using the auto method (click the header and it sorts, you can swap around the cols just fine etc) and every thing displays correctly.

The question is this: How do I return the correct item reference or data set? Since they don't keep their original indexes.

Example "test 2" is entry 1. Now if I don't sort the table I can return the row and use that number to look up the position in the list. But if I sort by date, test 2 is now index 4 and I get the wrong data set reference (since I'm looking directly at the original source).

Now there must be a way to find out which was the original data index since the table is showing the correct data! Please assist!

:)

thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文