tags:

views:

315

answers:

2

Hi, I would like to make a JTable that users when selecting an uneditable cell the it changes the 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?

All the Best!

+1  A: 

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

OMG Ponies
A: 

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);
camickr