简体   繁体   English

JTable中正在编辑的单元格的位置

[英]Position of cell being edited in JTable

I want to get the coordinates (row index and column index) of a cell of JTable when key is released during editing that cell. 我想获取在编辑该单元格时释放键时JTable单元格的坐标(行索引和列索引)。 I am trying to do it in this way: 我正在尝试通过这种方式:

@Override
public void keyReleased(KeyEvent released) {
    JTable tbl = (JTable)(released.getSource());
    if (tbl.getCellEditor() != null)
        System.out.println(tbl.getSelectedRow()+","+tbl.getSelectedColumn());
}

But this does not return position (row index and column index) of cell being edited. 但这不会返回正在编辑的单元格的位置(行索引和列索引)。 Please help. 请帮忙。

when key is released during editing that cell. 在编辑该单元格期间释放键时。

Doesn't make sense. 没道理 The editor has focus when a cell is being edited. 编辑单元格时,编辑器将具有焦点。 Also the user could use the mouse to put the cell in editing mode. 用户还可以使用鼠标将单元格置于编辑模式。

If you want to know when a cell starts editing you can add a PropertyChangeListener to the JTable : 如果您想知道单元格何时开始编辑,可以将PropertyChangeListener添加到JTable

//
//  Implement the PropertyChangeListener interface
//
    @Override
    public void propertyChange(PropertyChangeEvent e)
    {
        //  A cell has started/stopped editing

        if ("tableCellEditor".equals(e.getPropertyName()))
        {
            if (table.isEditing())
                // code for editing started;
            else
                // code for editing stopped;
        }
    }

Then you just use the getSelected...() methods to get the row/column. 然后,您只需使用getSelected...()方法即可获取行/列。

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

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