Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

java - Change behavior of JTable key actions

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...