Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8297325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:22:37+00:00 2026-06-08T15:22:37+00:00

I have a JTable with editable cells. When I click in a cell, it

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:22:40+00:00Added an answer on June 8, 2026 at 3:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable which has certain cells editable. If the user enters in
I have an editable JTable . If a user is editing a cell when
If an editable cell is selected in a JTable (selected by using the arrows
In my JTable, I have two editable numeric columns. The editor for both columns
In my implementation of JTable . I have to keep some columns editable and
I have a JTable which I want to have left-click and right-click JPopupMenu on
I have a JTable that is using a TableColumnModelListener() to detect when the column
I have a jTable as from the attached picture Right click on a row
I have a JTable using a custom DefaultTableModel which has some Booleans in the
I have a JTable and i want a cell (or its row) painted in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.