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

  • Home
  • SEARCH
  • 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 6792175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:56:13+00:00 2026-05-26T17:56:13+00:00

I have a cell editor that is composed of several components on a JPanel.

  • 0

I have a cell editor that is composed of several components on a JPanel. When my custom cell editor stops editing, the table loses focus instead of transfering focus to the next cell.

Here’s a simple example. Type into each cell and tab through the table. Notice that after visiting the 3rd column, the table loses focus to another text field on the panel.

Update: This issue appears to be fixed in Java7. The example must be run with Java 6 to see the focus lost behavior.

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;
import javax.swing.text.JTextComponent;


public class TableEditorFocusExample extends JFrame
{

   private JTable m_table;
   private TableModel tableModel;


   public TableEditorFocusExample()
   {
       setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

       Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener()
       {

           @Override
           public void eventDispatched( AWTEvent event )
           {
               System.out.println( "FOCUS " + 
                                   event + 
                                   "\n   source=" + 
                                   event.getSource() );
           }
       }, AWTEvent.FOCUS_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK );

       tableModel = new DefaultTableModel( 4, 4 );
       m_table = new JTable( tableModel )
       {
           @Override
           public void changeSelection(
                   int row,
                   int column,
                   boolean toggle,
                   boolean extend )
           {
               super.changeSelection( row, column, toggle, extend );

               if ( editCellAt( row, column ) )
               {
                   Component editor = getEditorComponent();
                   editor.requestFocusInWindow();
                   if ( editor instanceof JTextComponent )
                   {
                       ( (JTextComponent)editor ).selectAll();
                   }
               }
           }

       };

       m_table.setModel( tableModel );
       m_table.setSurrendersFocusOnKeystroke( true );
       m_table.putClientProperty( "terminateEditOnFocusLost", Boolean.TRUE ); //$NON-NLS-1$    

       DefaultCellEditor textFieldCellEditor = new DefaultCellEditor( new JTextField() );
       textFieldCellEditor.setClickCountToStart( 1 );

       TableCellEditor panelBasedCellEditor = new PanelCellEditor();

       m_table.getColumnModel().getColumn( 0 ).setCellEditor( textFieldCellEditor );
       m_table.getColumnModel().getColumn( 1 ).setCellEditor( textFieldCellEditor );
       m_table.getColumnModel().getColumn( 2 ).setCellEditor( panelBasedCellEditor );
       m_table.getColumnModel().getColumn( 3 ).setCellEditor( textFieldCellEditor );
       m_table.setColumnSelectionAllowed( true );

       final JButton ok = new JButton( "reset" );

       JPanel panel = new JPanel();
       panel.add( m_table );

       // add a component to grab focus when the table editor loses focus 
       final JTextField textField = new JTextField( 8 );
       final Color origTextColor = textField.getBackground();
       textField.addFocusListener( new FocusAdapter()
       {
           @Override
           public void focusGained( FocusEvent e )
           {
               System.err.println( "focus gained from: " + e.getSource() );
               textField.setBackground( Color.red );
           }
       } );

       // reset the text field background color to the pre-focus color
       ok.addActionListener( new ActionListener()
       {
           @Override
           public void actionPerformed( ActionEvent e )
           {
               textField.setBackground( origTextColor );
           }
       } );

       panel.add( textField );
       panel.add( ok );

       getContentPane().add( panel );
   }


   public class PanelCellEditor extends AbstractCellEditor implements
           TableCellEditor
   {
       public PanelCellEditor()
       {
           m_textfield.setBackground( Color.green );

           m_panel = new JPanel( new GridLayout() )
           {
               @Override
               public boolean requestFocusInWindow()
               {
                   // when the table transfers focus to the editor,
                   // forward focus onto the text field.
                   return m_textfield.requestFocusInWindow();
               }
           };

           m_panel.add( m_textfield );
       }


       @Override
       public Object getCellEditorValue()
       {
           return m_textfield.getText();
       }


       @Override
       public Component getTableCellEditorComponent(
               JTable table,
               Object value,
               boolean isSelected,
               int row,
               int column )
       {
           m_textfield.setText( value == null ? "" : value.toString() );
           return m_panel;
       }


       private JPanel m_panel;
       private JTextField m_textfield = new JTextField( 5 );
   }


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

           @Override
           public void run()
           {
               TableEditorFocusExample test = new TableEditorFocusExample();
               test.setSize( 600, 300 );
               test.setVisible( true );
           }
       } );
   }
}

I found a similar question here, but the solution seems to be incomplete since the text field on the custom editor doesn’t have focus, its cursor doesn’t display making it unclear to the user that the field is available for text entry.

Anyone have a better solution?

  • 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-26T17:56:13+00:00Added an answer on May 26, 2026 at 5:56 pm

    nice digging 🙂

    For jdk6 you might consider using SwingX and its JXTable which has that problem fixed (just checked, forgot we had worked around some focus issues :-). Or, if that’s not an option, look at its code and copy the overridden transferFocus (and related) methods and the improved EditorRemover.

    And don’t forget to make your editor comply to its contract:

           Action action = new AbstractAction() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                stopCellEditing();
            }
    
           };
           m_textfield.setAction(action);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datatable that uses the cell editor function. In each row, I
I have an HTML page (PHP, really) with a table cell that contains a
I have a cell editor that contains a little button and then a textfield
I have a cell editor that contains a little button that can be double
i'm using a JSpinner like a table cell editor, i have one annoying problem:
i have custom cell with 2 buttons(the function of these buttons is just to
I have a cell that I am inserting to the top of a UITableView.
Imagine I have a cell that I want to be red if the value
I have a custom NSTableView subclass filled with several custom NSTextFieldCell subclasses. I would
I have a custom EditButton that shows a dialog for the user to select

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.