简体   繁体   English

JTable,TableColumnModelListener检测选定的行

[英]JTable, TableColumnModelListener detecting selected row

what i want to accomplish is the following: If the user focuses the 5th column of a row in my JTable (either by clicking on it or pressing tab until he reaches the 5th cell), i show a dialog. 我想要完成的是:如果用户将我的JTable中的第5行聚焦(通过单击它或按Tab键直到他到达第5个单元格),我会显示一个对话框。 The result of the dialog will then be put in that cell. 然后,对话框的结果将放入该单元格中。 To access that cell i need to know the row and the column that is focused. 要访问该单元格,我需要知道所关注的行和列。

The problem that i am facing now is, that if i click on the cell, before focusing anything else in the table, getSelectedRow() returns -1, while getSelectedColumn() returns the right column. 我现在面临的问题是,如果我单击单元格,在聚焦表中的任何其他内容之前,getSelectedRow()返回-1,而getSelectedColumn()返回右列。

Question: How can i determine the selected row on the first click in the table. 问题:如何确定表中第一次单击时选定的行。 Or is my only option to do a big workaround to handle that first click separately. 或者我唯一的选择是做一个大的解决方法来单独处理第一次点击。

new TableColumnModelListener() {
    .
    .
    .
    @Override
    public void columnSelectionChanged(ListSelectionEvent e) {
        System.out.println(getSelectedColumn()); // this is correct
        System.out.println(getSelectedRow());  // -1 on first click in JTable
    }    
}

The problem arises because row and column selection are handled by two unrelated models: a trigger that changes the selection in both (fi a mousePressed) will do so by changing first the one, then the other (with no guarantee on sequence). 问题出现是因为行和列选择由两个不相关的模型处理:一个触发器改变两者中的选择(通过鼠标按下)将通过首先改变一个,然后改变另一个(不保证序列)来实现。 Consequently, at the time of receiving the change notification on one, you can't know whether or not the other is already changed or not. 因此,在收到更改通知时,您无法知道另一个是否已经更改。

To solve, wrap your custom handling of the notification into an invokeLater: this is guaranteed to happen after all pending events are processed: 要解决这个问题,请将自定义的通知处理包装到invokeLater中:这可以保证在处理完所有挂起事件发生:

@Override
public void columnSelectionChanged(ListSelectionEvent e) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            System.out.println(table.getSelectedColumn()); // this is correct
            System.out.println(table.getSelectedRow());  // -1 on first click in JTable
        }
    });
}

try this: 尝试这个:

table.addMouseListener(new MouseListener()
    {
        @Override
        public void mousePressed(MouseEvent evt)
        {   
           int row = table.getSelectedRow();
           int col = table.getSelectedColumn();
           System.out.println(row);
           System.out.println(col);
           if (col==4){
               JOptionPane.showInputDialog(null, "hi");
           }
        }

that would give you the selected row and column every time the user clicks. 每次用户点击时,它会为您提供所选的行和列。 The only disadvantage is that row one would print out 0, therefore you'd have to set (col==4) to trigger the dialog box. 唯一的缺点是第一行会打印出0,因此你必须设置(col == 4)来触发对话框。 this is not a tablecolumnmodellistener though. 这不是一个tablecolumnmodellistener。

Alternatively, you might want to look at this, because, someone seemed to have a similar problem: JTable not returning selected row correctly 或者,你可能想看看这个,因为,有人似乎有类似的问题: JTable没有正确返回选定的行

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

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