Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中从jTable中选择排序数据_Java_Swing - Fatal编程技术网

在Java中从jTable中选择排序数据

在Java中从jTable中选择排序数据,java,swing,Java,Swing,我有一个jTable,它将数据从excel文件加载到未排序的jTable中。每一行数据都有一个唯一的ID号,我有一个函数,可以按ID对数据进行数字排序。单击表中的特定行时,该数据ID将打印到控制台。这最初在数据未排序之前起作用,但在数据排序之后,错误的ID会打印到控制台。到目前为止,我得到的是:第一个函数按ID对数据进行排序,第二个函数从jTable获取(错误的)ID并将其打印到控制台。如何修复此问题以选择正确的ID private void sort(int columnIndex){

我有一个jTable,它将数据从excel文件加载到未排序的jTable中。每一行数据都有一个唯一的ID号,我有一个函数,可以按ID对数据进行数字排序。单击表中的特定行时,该数据ID将打印到控制台。这最初在数据未排序之前起作用,但在数据排序之后,错误的ID会打印到控制台。到目前为止,我得到的是:第一个函数按ID对数据进行排序,第二个函数从jTable获取(错误的)ID并将其打印到控制台。如何修复此问题以选择正确的ID

private void sort(int columnIndex){
    TableRowSorter<TableModel> sorter = new TableRowSorter<>(jTable1.getModel());
    jTable1.setRowSorter(sorter);
    List<RowSorter.SortKey> sortKeys = new ArrayList<>();
    sortKeys.add(new RowSorter.SortKey(columnIndex, SortOrder.ASCENDING));
    sorter.setSortKeys(sortKeys);
    sorter.sort();
}



private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        TableModel dtm = (DefaultTableModel) jTable1.getModel();
        int index = jTable1.getSelectedRow();
        String ID = dtm.getValueAt(index, 1).toString();
        System.out.println(ID);            
}
private void排序(int-columnIndex){
TableRowSorter-sorter=新的TableRowSorter(jTable1.getModel());
jTable1.设置行分拣机(分拣机);
List sortKeys=new ArrayList();
添加(新的RowSorter.SortKey(columnIndex,SortOrder.升序));
分拣机设置快捷键(快捷键);
sorter.sort();
}
私有void jTable1MouseClicked(java.awt.event.MouseEvent evt){
TableModel dtm=(DefaultTableModel)jTable1.getModel();
int index=jTable1.getSelectedRow();
字符串ID=dtm.getValueAt(索引,1.toString();
系统输出打印项次(ID);
}
示例1:数据未排序,我选择了作业ID 8953528,因此“8953528”会像我最初计划的那样打印到控制台

示例2:现在数据已排序,我选择了作业ID 8793343,但不再打印该ID,而是再次打印“8953528”。注意:在这两种情况下,我都从第三行选择了数据


当您单击已排序的
JTable
上的行时,
getSelectedRow()
会为您提供“可视”行索引。您需要的(为了访问数据)是“model”行索引

    index = sorter.convertRowIndexToModel(index);

下面是我如何修复它的

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        TableRowSorter<TableModel> sorter = new TableRowSorter<>(jTable1.getModel());
        int index = jTable1.getSelectedRow();
        index = sorter.convertRowIndexToModel(index);
        System.out.println(jTable1.getValueAt(index, 0));  
}
private void jTable1MouseClicked(java.awt.event.MouseEvent evt){
TableRowSorter-sorter=新的TableRowSorter(jTable1.getModel());
int index=jTable1.getSelectedRow();
索引=分拣机.convertRowIndexToModel(索引);
System.out.println(jTable1.getValueAt(index,0));
}