Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTable editor issue

843806Jan 27 2009 — edited Aug 12 2009
Hi guys

I'm sure this question has been asked before, but I must've been searching with the wrong keywords. I have a custom component containing a JTextField, and a JButton. I have a cell editor that should use an instance of my component for editing. The problem is, when a cell is selected (not editing) and I start typing I want whatever I typed to be inserted into the text field, and put the focus on the text field (basically the same behaviour as a DefaultCellEditor with a JTextField). I tried overriding request focus on my component, as I thought that might be called. Can anyone tell me how to get the behaviour that I want? The following (rather messy) code illustrates the problem.

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;

public class JTableEditorTest {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Editor Test");
        frame.setBounds(20, 20, 400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(1, 1));
        
        JTable table = new JTable(new Object[][] {{"", ""}}, new Object[] {"col1", "col2"});
        
        table.getColumnModel().getColumn(0).setCellEditor(new CustomComponentCellEditor(new CustomComponent()));
        
        frame.add(new JScrollPane(table));
        
        frame.setVisible(true);
    }

}

class CustomComponent extends JComponent {
    
    protected JTextField textField;
    protected JButton button;
    
    public CustomComponent() {
        textField = new JTextField();
        button = new JButton();
        
        button.setPreferredSize(new Dimension(25, 25));
        textField.setPreferredSize(new Dimension(125, 25));
        
        this.setLayout(new BorderLayout());
        
        this.add(button, BorderLayout.EAST);
        this.add(textField, BorderLayout.CENTER);
    }

    @Override
    public void requestFocus() {
        System.out.println("Called requestFocus()");
        this.textField.requestFocusInWindow();
    }
}

class CustomComponentCellEditor extends AbstractCellEditor implements TableCellEditor {
    
    private CustomComponent component;

    public CustomComponentCellEditor(CustomComponent component) {
        this.component = component;
    }
    
    public Object getCellEditorValue() {
        return this.component.textField.getText();
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        System.out.println("Getting table cell editor component");
        this.component.requestFocus();                  //Tried this
        this.component.textField.setCaretPosition(0);   //and this
        return this.component;
    }
}
Thanks a lot,
Regards
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 9 2009
Added on Jan 27 2009
9 comments
697 views