java - 删除 JTable 中选定的列表元素

标签 java swing jtable

我有一个基于表模型的 JTable,它使用 List,我想删除在此 JTable 中选择的元素,从 List 中,JTable 是可排序的,因此当我获取选定的行时,我不能简单地 list.remove,因为顺序不同。有办法解决这个问题吗?

最佳答案

看看JTable方法convertRowIndexToViewconvertRowIndexToModel:

The selection is always in terms of JTable so that when using RowSorter you will need to convert using convertRowIndexToView or convertRowIndexToModel. The following shows how to convert coordinates from JTable to that of the underlying model:

  int[] selection = table.getSelectedRows();
  for (int i = 0; i < selection.length; i++) {
    selection[i] = table.convertRowIndexToModel(selection[i]);
  }
  // selection is now in terms of the underlying TableModel

查看How to Use Tables # Sorting and Filtering举一些例子。

关于java - 删除 JTable 中选定的列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24317216/

相关文章:

java - 使用 JTextFields 中的字符串填充 JTable

java - 为什么在PreparedStatement中只更新一列的值

java - 左下角的 float JButton

java - 将值从 jtable 行插入数据库 - IN OUT 错误

java - 我可以在网格布局中操作特定的网格吗?

java - 排序 JTable 除了最后一行

java - 如何从绑定(bind)的 JTable 中删除行?

java - xjc 如何确定生成的类使用的包?

java - JAVA如何解析HTTP响应参数?

java - 在特定字节上拆分字节数组