문제

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?

도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top