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 3281602
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:50:01+00:00 2026-05-17T19:50:01+00:00

I have a cell editor that contains a little button and then a textfield

  • 0

I have a cell editor that contains a little button and then a textfield that can be used to edit the value inline

I use setSurrendersFocusOnKeystroke(true) and a focus listener in order to allow a user to start editing immediately from the keyboard, but the trouble is the fisrt key pressed seems to get consumed rather being added to the text field, how can I prevent this ?

Full self contained example below

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class PanelTableEditorTest extends JFrame {

    private JTable table;

    public PanelTableEditorTest() {
        this.setLayout(new BorderLayout());
        table = new JTable(10, 10);
        table.getSelectionModel().setSelectionMode(
            ListSelectionModel.SINGLE_SELECTION);
        table.setCellSelectionEnabled(true);
        table.setDefaultEditor(Object.class, new SimpleMultiRowCellEditor());
        table.setSurrendersFocusOnKeystroke(true);
        table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0),
            "none");
        table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER, 0),
            "startEditing");
        this.add(table.getTableHeader(), BorderLayout.NORTH);
        this.add(table, BorderLayout.CENTER);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PanelTableEditorTest();
            }
        });
    }

    public class SimpleMultiRowCellEditor extends DefaultCellEditor {

        final JPanel panel;
        private final JButton rowCount;

        public SimpleMultiRowCellEditor() {
            super(new JTextField());
            this.setClickCountToStart(1);

            rowCount = new JButton();
            rowCount.setVisible(true);
            panel = new JPanel();
            panel.setOpaque(false);
            panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
            panel.add(rowCount);
            panel.add(editorComponent);
            panel.addFocusListener(new PanelFocusListener());
        }

        public Component getTableCellEditorComponent(
            final JTable table,final Object val, final boolean isSelected,
            final int row, final int column) {
            rowCount.setText("1");
            delegate.setValue(val);
            editorComponent.requestFocusInWindow();
            return panel;
        }

        class PanelFocusListener implements FocusListener {

            public void focusGained(FocusEvent e) {
                editorComponent.requestFocusInWindow();
            }

            public void focusLost(FocusEvent e) {
            }
        }
    }
}
  • 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-05-17T19:50:02+00:00Added an answer on May 17, 2026 at 7:50 pm

    So I have found a solution, thanks to this article http://jroller.com/santhosh/entry/keyboard_handling_in_tablecelleditor , and some useful discussion abou this and how it can be applied to other components at http://forums.java.net/jive/thread.jspa?messageID=482236&#482236

    Don’t fully understand the solution this whole area seems to be rather a minefield

    I’ve also added this solution Get correct editing behaviour in JTable using java DefaultCellEditor into this so that when you start editing a field using the keyboard the existing value is replaced, but not when you double click o the field.

    My one confusion is that I’m not receiving a Key Event as I’d expect but just null so I’ve had to account for that.

    Ive gone back from using setSurrenderKeystrokes(true) because this causes problems with others editors such as the straightforward textfieldeditor

    import javax.swing.*;
    import javax.swing.text.Caret;
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.util.EventObject;
    
    public class PanelTableEditorTest extends JFrame
    {
    
        private JTable table;
    
        public PanelTableEditorTest()
        {
            try
            {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }
            catch(Exception e)
            {
    
            }
            this.setLayout(new BorderLayout());
            table = new JTable(4, 4);
            table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.setCellSelectionEnabled(true);
            table.setSurrendersFocusOnKeystroke(false);
            table.setDefaultEditor(Object.class,new SimpleMultiRowCellEditor());
            table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(java.awt.event.
                            KeyEvent.VK_F2, 0), "none");
            table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(java.awt.event.
                            KeyEvent.VK_ENTER, 0), "startEditing");
    
            this.add(table.getTableHeader(), BorderLayout.NORTH);
    
            this.add(table, BorderLayout.CENTER);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    new PanelTableEditorTest();
    
                }
            });
        }
    
        public class SimpleMultiRowCellEditor extends DefaultCellEditor
        {
            private EventObject event;
            final JPanel panel;
            private final JButton rowCount;
    
            public SimpleMultiRowCellEditor()
            {
                super(new JTextField());
                this.setClickCountToStart(1);
    
                rowCount = new JButton();
                rowCount.setVisible(true);
                panel = new TableEditorPanel();
                panel.setRequestFocusEnabled(true);
                panel.setOpaque(false);
                panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
                panel.add(rowCount);
                panel.add(editorComponent);
            }
    
            public boolean isCellEditable(EventObject anEvent)
            {
                event=anEvent;
                return super.isCellEditable(anEvent);
            }
    
            public Component getTableCellEditorComponent(final JTable table, final Object val, final boolean isSelected, final int row, final int column)
            {
                rowCount.setText("1");
                delegate.setValue(val);
                if(event instanceof KeyEvent || event==null)
                {
                    final Caret caret = ((JTextField)editorComponent).getCaret();
                    caret.setDot(0);
                    ((JTextField)editorComponent).setText("");                
                }
                return panel;
            }
    
            class TableEditorPanel extends JPanel
            {
    
                public void addNotify(){
                    super.addNotify();
                    editorComponent.requestFocus();
                }
    
                protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed){
                    InputMap map = editorComponent.getInputMap(condition);
                    ActionMap am = editorComponent.getActionMap();
    
                    if(map!=null && am!=null && isEnabled()){
                        Object binding = map.get(ks);
                        Action action = (binding==null) ? null : am.get(binding);
                        if(action!=null){
                            return SwingUtilities.notifyAction(action, ks, e, editorComponent,
                                    e.getModifiers());
                        }
                    }
                    return false;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cell editor that contains a little button that can be double
I have a datatable that uses the cell editor function. In each row, I
I have a cell editor that is composed of several components on a JPanel.
I have an HTML page (PHP, really) with a table cell that contains a
I have recently started to use the code cell function in MATLAB's editor and
How can I use jQuery to click on a table cell and edit its
I have a cell that I am inserting to the top of a UITableView.
i have a custom cell i want to delete it when a button is
I have a table cell that isn't entirely filled with text so the bg
I have a few worksheets that use sql queries to retrieve data. I would

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.