java 从(排序的)JTable 中正确获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2075396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:17:11  来源:igfitidea点击:

Correctly getting data from a (sorted) JTable

javajtable

提问by pek

I have developed a basic custom JTableModel as follows

我开发了一个基本的自定义 JTableModel 如下

public class CustomTableModel extends DefaultTableModel {
  List<MyClass> data;
  public CustomTableModel(List<MyClass> data) {
    this.data = data;
  }

  public Class<?> getColumnClass(int columnIndex) {
    return MyClass.class;
  }

  public MyClass getValueAt(int rowIndex, int columnIndex) {
    return data.get(rowIndex);
  }

  // ...
}

I then use a basic custom JTableCellRenderer as follows

然后我使用一个基本的自定义 JTableCellRenderer 如下

public class CustomTableCellRenderer extends JLabel implements TableCellRenderer {

  public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {

    MyClass myClass = (MyClass)value;

    lbl.setText(myClass.getString());

    return this;
  }
}

I also have a custom JPanel that displays various information as follows

我还有一个自定义的 JPanel 显示各种信息如下

public class MyPanel extends JPanel {
  private MyClass myClass;

  public MyPanel(MyClass myClass) {
    // initialize components
  }

  public setMyClass(MyClass myClass) {
    this.myClass = myClass;
    updateFields();
  }

  private void updateFields() {
    this.fieldString.setText(myClass == null ? "" : myClass.getString());
    // ...
  }
}

Finally, I use a table to list my data and the custom panel to display the details of the selected data.

最后,我使用一个表格来列出我的数据,并使用自定义面板来显示所选数据的详细信息。

public class JCustomFrame extends JFrame {
  public JCustomFrame(List<MyClass> data) {
    // ...
    JTable table = new JTable(new CustomTableModel(data));
    table.setDefaultRenderer(MyClass.class, new CustomTableCellRenderer());

  }
}

What I am trying to accomplish is get the selected MyClass from the table regardless of sorting.

我想要完成的是从表中获取选定的 MyClass,而不考虑排序

I tried ListSelectionListener but the methods do not return anything other than the selected indexes. Even if I have the index, if the table is sorted, my model is not so sophisticated and will return the wrong object.

我尝试了 ListSelectionListener 但这些方法不返回所选索引以外的任何内容。即使我有索引,如果表被排序,我的模型也不是那么复杂,会返回错误的对象。

回答by OscarRyz

...Even if I have the index, if the table is sorted, my model is not so sophisticated and will return the wrong object...

...即使我有索引,如果表已排序,我的模型也不是那么复杂,会返回错误的对象......

You have to use:

你必须使用:

JTable.convertRowIndexToModel( int viewIndex )

JTable.convertRowIndexToModel( int viewIndex )

Maps the index of the row in terms of the view to the underlying TableModel. If the contents of the model are not sorted the model and view indices are the same.

根据视图将行的索引映射到基础 TableModel。如果模型的内容没有排序,则模型和视图索引是相同的。

With that index you can access your table model and see what's the object you need.

使用该索引,您可以访问您的表模型并查看您需要的对象是什么。

NoteTable sorting along with this method was introduced in Java 1.6

注意表排序和这个方法是在 Java 1.6 中引入的