2

I have a JTable with editable cells. When I click in a cell, it enters edit mode; the same happens when I'm moving through cell using the directional arrows. Now I want to select the cell instead of start editing, and edit the cell only when the Enter key is pressed.

If any other information is needed, please just ask for it.

Edit: Action for Enter key

class EnterAction extends AbstractAction {

    @Override
    public void actionPerformed(ActionEvent e) {
        JTable tbl = (JTable) e.getSource();
        tbl.editCellAt(tbl.getSelectedRow(), tbl.getSelectedColumn());
        if (tbl.getEditorComponent() != null) {
            tbl.getEditorComponent().requestFocus();
        }
    }
}

Now this is for left arrow action the rest of 3 are not hard to deduce from this one:

class LeftAction extends AbstractAction {
    @Override
    public void actionPerformed(ActionEvent e) {
        JTable tbl = (JTable)e.getSource();
        tbl.requestFocus();
        tbl.changeSelection(tbl.getSelectedRow(), tbl.getSelectedColumn() > 0 ? tbl.getSelectedColumn()-1:tbl.getSelectedColumn(), false, false);
        if(tbl.getCellEditor()!=null)
            tbl.getCellEditor().stopCellEditing();
    }
}

And this is how you bind this actions:

final String solve = "Solve";
            KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
            table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, solve);
            table.getActionMap().put(solve, new EnterAction());
final String sel = "Sel";
            KeyStroke arrow = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
            table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(arrow, sel);
            table.getActionMap().put(sel, new LeftAction());

Oh,i almost forgot,to select the cell instead of edit on Mouse Click:

public static MouseListener mAdapterTable = new MouseListener()
{
    @Override
    public void mousePressed(MouseEvent e)
    {
        JTable tbl=((JTable)e.getComponent());
        if(tbl.isEditing())
        {
            tbl.getCellEditor().stopCellEditing();
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        JTable tbl=((JTable)e.getComponent());
        if(tbl.isEditing() )
            tbl.getCellEditor().stopCellEditing();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        JTable tbl=((JTable)e.getComponent());
        if(tbl.isEditing() )
            tbl.getCellEditor().stopCellEditing();
    }
};

The EventListner must be added to table like so:

table.addMouseListener(mAdapterTable);
Cioban Andrei
  • 23
  • 1
  • 5
  • 1
    you don't need a mouseListener to prevent starting an edit on clicking: instead implement all editors to return false in isCellEditable(EventObject) when the event is a MouseEvent. For DefaultCellEditor, you can set the clickCountToStart property so high that it effectively disables editing on mouse events – kleopatra Jul 30 '12 at 07:13
  • Well i tried but after returning false in the if statment the `isCellEditable` was not ending and was returning the true value from the wery end of this method.How can this hapen i don't understand but i run it step by step and it is entering if statment returning false after that is jumping at the last line from this method that is returning true; – Cioban Andrei Jul 30 '12 at 08:09

2 Answers2

4

Use Key Bindings for this. Most Look & Feel implementations already bind F2 to the table's startEditing action, but you add a different binding:

tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "startEditing");

This will effectively replace the previous binding of Enter to the table's selectNextRowCell action.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Here is what i would do:

  • First enable the single cell selection for the JTable
  • Create a KeyAdapter or KeyListener for the JTable or for the JPanel, what contains your table.
  • In the KeyAdapter's keyPressed() method enter the edit mode of the selected cell, something like this: http://www.exampledepot.com/egs/javax.swing.table/StopEdit.html

You can check in the keyPressed() method, if the user pressed the right button for editing. I'm not sure, if the normal (double click) editing is disabled in your table, then what happens, if you try to edit it programmatically, but if it doesn't work, then you can enable the editing on the selected cell, when the user presses the edit button, then when he/she finished, disable it again.

Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
  • 1
    no, KeyListener is _not_ the answer - the way the handle keyStrokes in Swing are Key Bindings, as suggested by @trashgod. Always use the highest abstraction available – kleopatra Jul 30 '12 at 07:08