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

发布于 2024-08-18 18:20:36 字数 1714 浏览 0 评论 0原文

我开发了一个基本的自定义 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);
  }

  // ...
}

所示 我然后使用一个基本的自定义 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;
  }
}

我还有一个自定义 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());
    // ...
  }
}

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

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

  }
}

我想要完成的是从表中获取选定的 MyClass 无论排序

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

I have developed a basic custom JTableModel as follows

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

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

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.

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.

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

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

发布评论

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

评论(1

当爱已成负担 2024-08-25 18:20:36

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

你必须使用:

JTable.convertRowIndexToModel( int viewIndex )

将视图中的行索引映射到基础 TableModel。如果模型的内容未排序,则模型索引和视图索引相同。

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

注意 表排序与此方法一起在 Java 1.6 中引入

...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 )

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.

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

Note Table sorting along with this method was introduced in Java 1.6

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