简体   繁体   English

编辑 JTable 后获取选定的行和列

[英]Get selected row and column AFTER editing JTable

I would like to perform an action after a table cell has been edited.我想在编辑表格单元格后执行操作。 However, after I used tablechanged and print the source cell, there was an error.但是,在我使用tablechanged并打印源单元格后,出现错误。 Then I managed to find that the error was due to e.source not being an instance of a table but instead a DefaultTableModel .How do you get the selected row and column AFTER editing?然后我设法发现错误是由于e.source不是table的实例,而是由于DefaultTableModel如何在编辑后获得选定的行和列?

Here's a sample code:这是一个示例代码:

public static void main(String[] args) {
    JFrame main = new JFrame();

    JTable table = new JTable(6, 4);
    table.setSize(300, 300);

    table.getModel().addTableModelListener(new TableModelListener() {

        public void tableChanged(TableModelEvent e) {
            Object s = e.getSource();
            //JTable t = (JTable) s;
            //int x = t.getSelectedRow();
            //int y = t.getSelectedColumn();
            //System.out.println("Cell at " + x + "," + y);

            if (s instanceof JTable)
                System.out.println("TABLE");
            else
                System.out.println("Not a table");
        }
    });
    main.add(table);
    main.setSize(300,300);
    main.setLocationRelativeTo(null);
    main.setVisible(true);
    main.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}

((TableModel)e.getSource()).getValueAte.getFirstRow/LastRow之间的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM