【问题标题】:Start editing in a cell in JTable on gaining focus开始在 JTable 中的单元格中编辑以获得焦点
【发布时间】:2012-08-04 00:17:22
【问题描述】:

我已按以下方式为表中的两列定义了单元格编辑器:

Java 代码:

JComboBox combo = new JComboBox();
//code to add items to the combo box goes here.

JTextField textField = new JTextField();
textField.setHorizontalAlignment(JTextField.RIGHT);

TableColumn column = myJTable.getColumnModel().getColumn(0);
column.setCellEditor(new DefaultCellEditor(combo));

column = myJTable.getColumnModel().getColumn(1);
column.setCellEditor(new DefaultCellEditor(textField));

我面临的问题是,当焦点移动到表格单元格时,该单元格不会自动变为可编辑。因此,当焦点移到第 2 列(具有作为编辑器的文本字段)时,插入符号不会出现,除非双击单元格或用户开始输入。第 1 列(有一个组合框作为编辑器)的情况类似,因为这里组合框不会出现,除非单击单元格。对于使用键盘进行操作的用户来说,这些行为是违反直觉且不受欢迎的。:(

请就如何解决此问题提出建议。

提前致谢。

【问题讨论】:

标签: java swing focus jtable tablecelleditor


【解决方案1】:

如上所述,您可以将 JComboBox 用作渲染器和编辑器。下面是一个非常基本的例子。它还显示DefaultCellEditor.setClickCountToStart() 的使用情况。

import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class ComboBoxEditorDemo {

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("ComboBoxEditorDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTable table = new JTable(
                new Object[][] { { "1", "2" }, { "1", "2" } }, 
                new Object[] {"Col1", "Col2" });

        table.setRowHeight(24);

        TableColumn column = table.getColumnModel().getColumn(1);
        column.setCellRenderer(new MyComboBoxRenderer(new String[] { "1", "2", "3" }));
        column.setCellEditor(new MyComboBoxEditor(new String[] { "1", "2", "3" }));

        DefaultCellEditor editor = new DefaultCellEditor(new JTextField());
        editor.setClickCountToStart(1);
        column = table.getColumnModel().getColumn(0);
        column.setCellEditor(editor);

        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }

    static class MyComboBoxRenderer extends JComboBox implements
            TableCellRenderer {
        public MyComboBoxRenderer(String[] items) {
            super(items);
        }

        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
            if (isSelected) {
                setForeground(table.getSelectionForeground());
                super.setBackground(table.getSelectionBackground());
            } else {
                setForeground(table.getForeground());
                setBackground(table.getBackground());
            }

            setSelectedItem(value);
            return this;
        }
    }

    static class MyComboBoxEditor extends DefaultCellEditor {
        public MyComboBoxEditor(String[] items) {
            super(new JComboBox(items));
        }
    }
}

【讨论】:

    【解决方案2】:
    1. 这个example 覆盖JTable 中的editCellAt(),而DefaultCellEditor 使用JTextField

    2. 您可以将 Space 键绑定到为JTable 定义的startEditing 操作:

      table.getInputMap().put(
          KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "startEditing");
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      相关资源
      最近更新 更多