简体   繁体   中英

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. 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 :

//
//  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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