简体   繁体   中英

Select next cell JTable

I would like to make a jTable in which when user select an uneditable cell then it should change focus to the next editable cell automatically. Important: the user could select a cell by keyboard (tab or arrow) and by mouse clicking. Is it possible?? How to to it?

This link details Programmatically Making Selections in a JTable Component ; you'd have to have mouselisteners/etc chained to work off this.

Table Tabbing shows how you can do it with the keyboard.

I've never tried it but you should be able to use a MouseListener to invoke the same Action when you click on a cell.

Just did a quick test for the MouseListener and it seems to work fine:

JTable table = new JTable(...);
final EditableCellFocusAction action = 
    new EditableCellFocusAction(table, KeyStroke.getKeyStroke("TAB"));

MouseListener ml = new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        JTable table = (JTable)e.getSource();
        int row = table.rowAtPoint(e.getPoint());
        int column = table.columnAtPoint(e.getPoint());

        if (! table.isCellEditable(row, column))
        {
                ActionEvent event = new ActionEvent(
                    table,
                    ActionEvent.ACTION_PERFORMED,
                    "");
                action.actionPerformed(event);
        }
    }
};
table.addMouseListener(ml);

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