问题
I have a custom editor composed of several components. Something like:
class MyCellEditor extends AbstractCellEditor implements TableCellEditor {
    JTextArea textArea;
    JButton button;
    JPanel panel;
    MyCellEditor() {
        textArea = new JTextArea();
        button = new JButton();
        panel = new JPanel(new BorderLayout());
        panel.add(textArea, BorderLayout.CENTER);
        panel.add(button, BorderLayout.EAST);
    }
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
        textArea.setText((String) value);
        return panel;
    }
    public Object getCellEditorValue() {
        return textArea.getText();
    }
}
I want the inner textArea to grab focus when editing starts. It works just fine when I click the cell, but not when I navigate the table with keyboard and start typing in this cell.
How can I fix this?
回答1:
I had the same problem some time ago and took me ages to find a solution. Tried a lot with focuslistener and stuff, but nothing really seemed to work the way I wanted it to until I found this useful article by Santhosh Kumar.
Its well written and should fix your problem.
来源:https://stackoverflow.com/questions/3386759/jtable-compound-editor-focus