Java排序表侦听器

Java排序表侦听器,java,sorting,Java,Sorting,我有一个表,在->setAutoCreateRowSorter上有一个行分类器(true)和ListSelectionModel,我将侦听器附加到它们。如果我排序它(通过点击标题) 问题是,如果对其进行排序,然后单击单元格,它将返回位于该位置的原始值(表排序之前的值): 若您阅读java代码,则getSelectedRow()位于选择模型上,而getModel()返回数据模型。数据模型和选择模型是两种不同的模型,这就是为什么您无法获得正确的索引 正确的解决方案是调用JTable.getRowSo

我有一个表,在->
setAutoCreateRowSorter上有一个行分类器(true)和ListSelectionModel,我将侦听器附加到它们。如果我排序它(通过点击标题)

问题是,如果对其进行排序,然后单击单元格,它将返回位于该位置的原始值(表排序之前的值):


若您阅读java代码,则getSelectedRow()位于选择模型上,而getModel()返回数据模型。数据模型和选择模型是两种不同的模型,这就是为什么您无法获得正确的索引

正确的解决方案是调用JTable.getRowSorter().convertRowIndexToModel()来获取数据模型索引。排序器是将底层数据模型重新排列到视图模型中的一个。从逻辑上讲,它必须是维护两个模型之间映射的模型。所以你的代码应该是

System.out.println(String.valueOf(tableProducts.getModel()
  .getValueAt(tableProducts.getRowSorter().convertRowIndexToModel(
  tableProducts.getSelectedRow()), 1)));
以Java教程中的JTable示例为基础,请参阅带有System.out.println(“Right Value=”)的行如何调用上述方法,完整代码在这里。它是可执行的,您可以通过启动应用程序来测试它,单击姓氏列标题,然后单击每个姓氏,观察控制台输出

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/*
 * SimpleTableDemo.java requires no other files.
 */

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class SimpleTableDemo extends JPanel {
    private boolean DEBUG = false;

    public SimpleTableDemo() {
        super(new GridLayout(1,0));

        String[] columnNames = {"First Name",
                                "Last Name",
                                "Sport",
                                "# of Years",
                                "Vegetarian"};

        Object[][] data = {
        {"Kathy", "Smith",
         "Snowboarding", new Integer(5), new Boolean(false)},
        {"John", "Doe",
         "Rowing", new Integer(3), new Boolean(true)},
        {"Sue", "Black",
         "Knitting", new Integer(2), new Boolean(false)},
        {"Jane", "White",
         "Speed reading", new Integer(20), new Boolean(true)},
        {"Joe", "Brown",
         "Pool", new Integer(10), new Boolean(false)}
        };

        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setAutoCreateRowSorter(true);
        table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(e.getValueIsAdjusting()) {
                    System.out.println("Wrong Value = " + String.valueOf(table.getModel().getValueAt(table.getSelectedRow(), 1)));
                    System.out.println("Right Value = " + String.valueOf(table.getModel().getValueAt(table.getRowSorter().convertRowIndexToModel(table.getSelectedRow()), 1)));
                }
            }

        });
        table.setFillsViewportHeight(true);

        if (DEBUG) {
            table.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    printDebugData(table);
                }
            });
        }

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }

    private void printDebugData(JTable table) {
        int numRows = table.getRowCount();
        int numCols = table.getColumnCount();
        javax.swing.table.TableModel model = table.getModel();

        System.out.println("Value of data: ");
        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + model.getValueAt(i, j));
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        SimpleTableDemo newContentPane = new SimpleTableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
导入java.awt.Dimension;
导入java.awt.GridLayout;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
/*
*SimpleTableDemo.java不需要其他文件。
*/
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
导入javax.swing.ListSelectionModel;
导入javax.swing.event.ListSelectionEvent;
导入javax.swing.event.ListSelectionListener;
公共类SimpleTableDemo扩展了JPanel{
私有布尔调试=false;
公共SimpleTableDemo(){
超级(新网格布局(1,0));
String[]columnNames={“名字”,
“姓氏”,
“运动”,
“#年”,
“素食者”};
对象[][]数据={
{“凯西”,“史密斯”,
“滑雪板”,新整数(5),新布尔值(假)},
{“约翰”,“多伊”,
“划船”,新整数(3),新布尔值(真)},
{“苏”,“黑”,
“编织”,新整数(2),新布尔值(假)},
{“简”、“白”,
“快速读取”,新整数(20),新布尔值(真)},
{“乔”,“布朗”,
“Pool”、新整数(10)、新布尔值(false)}
};
最终JTable表=新JTable(数据、列名称);
表.setPreferredScrollableViewportSize(新维度(500,70));
表.setAutoCreateRowSorter(真);
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(新ListSelectionListener()){
@凌驾
public void值已更改(ListSelectionEvent e){
如果(如getValueIsAdjusting()){
System.out.println(“错误值=“+String.valueOf(table.getModel().getValueAt(table.getSelectedRow(),1)));
System.out.println(“Right Value=“+String.valueOf(table.getModel().getValueAt(table.getRowSorter().convertRowIndexToModel(table.getSelectedRow()),1));
}
}
});
表.setFillsViewPerthweight(真);
如果(调试){
表.addMouseListener(新的MouseAdapter(){
公共无效mouseClicked(MouseEvent e){
打印数据(表格);
}
});
}
//创建滚动窗格并将表添加到其中。
JScrollPane scrollPane=新的JScrollPane(表);
//将滚动窗格添加到此面板。
添加(滚动窗格);
}
私有无效printDebugData(JTable表){
int numRows=table.getRowCount();
int numCols=table.getColumnCount();
javax.swing.table.TableModel=table.getModel();
System.out.println(“数据值:”);
对于(int i=0;i
用示例代码更新了我的答案。请参见打印正确值的行。
System.out.println(String.valueOf(tableProducts.getModel()
  .getValueAt(tableProducts.getRowSorter().convertRowIndexToModel(
  tableProducts.getSelectedRow()), 1)));
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/*
 * SimpleTableDemo.java requires no other files.
 */

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class SimpleTableDemo extends JPanel {
    private boolean DEBUG = false;

    public SimpleTableDemo() {
        super(new GridLayout(1,0));

        String[] columnNames = {"First Name",
                                "Last Name",
                                "Sport",
                                "# of Years",
                                "Vegetarian"};

        Object[][] data = {
        {"Kathy", "Smith",
         "Snowboarding", new Integer(5), new Boolean(false)},
        {"John", "Doe",
         "Rowing", new Integer(3), new Boolean(true)},
        {"Sue", "Black",
         "Knitting", new Integer(2), new Boolean(false)},
        {"Jane", "White",
         "Speed reading", new Integer(20), new Boolean(true)},
        {"Joe", "Brown",
         "Pool", new Integer(10), new Boolean(false)}
        };

        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setAutoCreateRowSorter(true);
        table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(e.getValueIsAdjusting()) {
                    System.out.println("Wrong Value = " + String.valueOf(table.getModel().getValueAt(table.getSelectedRow(), 1)));
                    System.out.println("Right Value = " + String.valueOf(table.getModel().getValueAt(table.getRowSorter().convertRowIndexToModel(table.getSelectedRow()), 1)));
                }
            }

        });
        table.setFillsViewportHeight(true);

        if (DEBUG) {
            table.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    printDebugData(table);
                }
            });
        }

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }

    private void printDebugData(JTable table) {
        int numRows = table.getRowCount();
        int numCols = table.getColumnCount();
        javax.swing.table.TableModel model = table.getModel();

        System.out.println("Value of data: ");
        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + model.getValueAt(i, j));
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        SimpleTableDemo newContentPane = new SimpleTableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}