简体   繁体   English

使用RowSorter时如何在JTable上获取数据?

[英]How to get data on JTable when using RowSorter?

I am having problem in getting the data when selecting a row from a JTable . JTable选择行时获取数据时出现问题。 This happens whenever I enable setAutoCreateRowSorter(true) of the table. 每当我启用表的setAutoCreateRowSorter(true)时,就会发生这种情况。 So far this is what I did: 到目前为止,这是我所做的:

private void displayBooks(){
    bookTable.setAutoCreateRowSorter(true);
    bookTable.getTableHeader().setFont(new java.awt.Font("Century Gothic", 1, 14));
    dtm = (DefaultTableModel) bookTable.getModel();
    clearTable(dtm);
    for(Book book: books){
        dtm.addRow(new Object[]{book.getId(), ...//rest of the code
    }
}

On the bookTableMouseClicked method this is what I did: bookTableMouseClicked方法上,这是我所做的:

 ...
 if(bookTable.getSelectedRow() >= 0){
     Book book = books.get(bookTable.getSelectedRow());
        setBook(book);
 }...

I am now having ambiguous data when I clicked the header table to sort the data. 单击标题表对数据进行排序时,我现在的数据不明确。

The selected row number on a JTable instance is always the selected row number on the view side . JTable实例上的选定行号始终是视图侧的选定行号。

If you activate row sorters this no longer matches the row number on the model side. 如果激活行排序器,则它不再与模型端的行号匹配。

To transform between these two row numbers the JTable offers methods for converting from "view row index" to "model row index" and vice versa. 为了在这两个行号之间进行转换, JTable提供了从“视图行索引”转换为“模型行索引”的方法,反之亦然。 These methods are named convertRowIndexToModel and convertRowIndexToView . 这些方法分别称为convertRowIndexToModelconvertRowIndexToView

In your mouseClicked handler you need to call the function convertRowIndexToModel as follows: 在mouseClicked处理程序中,您需要按如下所示调用函数convertRowIndexToModel

if (bookTable.getSelectedRow() >= 0){
    Book book = books.get(bookTable.convertRowIndexToModel(bookTable.getSelectedRow()));
    setBook(book);
}

The problem is that you are storing data in two places: 问题是您将数据存储在两个位置:

  1. in the TableModel 在TableModel中
  2. in an ArrayList 在ArrayList中

The data should only be stored in the TableModel. 数据应仅存储在TableModel中。 This way you don't need to worry about syncing the data since it is only in one place. 这样,您就不必担心数据同步,因为它只在一个地方。

You could simply create a Book object from the selected row by using getValueAt(..) method of the JTable. 您可以简单地使用JTable的getValueAt(..)方法从选定的行创建Book对象。 You would need to invoke that method for each column in the table. 您将需要为表中的每个列调用该方法。

Or the other approach is to create a custom TableModel that holds Book objects, then you could just get the Book object directly from the table. 或者另一种方法是创建一个包含Book对象的自定义TableModel,然后您可以直接从表中获取Book对象。 This is a little more work, but it is the better approach. 这是更多的工作,但这是更好的方法。

Check out Row Table Model for a step-by-step approach on how to create a custom TableModel for a custom object. 请查看行表模型 ,以获取有关如何为自定义对象创建自定义TableModel的分步方法。

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

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