2

我有一个JTablewith a customTableCellRenderer和一个 custom TableCellEditor。默认情况下,第一次单击表格行从渲染器切换到编辑器,第二次单击选择行。

有什么办法可以让我单击一下就选中行并切换到编辑器)?

我曾尝试使用:

table.getSelectionModel().setSelectionInterval(row, row);

在我的getTableCellEditorComponent但它不起作用,如果我将它添加到我的getTableCellRendererComponent它会起作用,但只是有时。

这是一个完整的例子:

public class SelectRowDemo extends JFrame {

    public SelectRowDemo() {
        CellRendererAndEditor rendererAndEditor = new CellRendererAndEditor();
        StringTableModel model = new StringTableModel();
        JTable table = new JTable(model);
        table.setDefaultEditor(String.class, rendererAndEditor);
        table.setDefaultRenderer(String.class, rendererAndEditor);

        model.addElement("");
        model.addElement("");
        model.addElement("");

        add(new JScrollPane(table));
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

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

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                new SelectRowDemo();
            }

        });
    }

    class CellRendererAndEditor extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {

        private final JLabel renderer = new JLabel();
        private final JLabel editor = new JLabel();

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

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {

            String str = "renderer ";
            str += (isSelected) ? "selected" : "not selected";

            renderer.setText(str);
            return renderer;
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {

            table.getSelectionModel().setSelectionInterval(row, row);

            String str = "editor ";
            str += (isSelected) ? "selected" : "not selected";

            editor.setText(str);
            return editor;
        }

    }

    class StringTableModel extends AbstractTableModel {

        private final List<String> data = new ArrayList<String>();

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public int getRowCount() {
            return data.size();
        }

        @Override
        public Object getValueAt(int row, int column) {
            return data.get(row);
        }

        @Override
        public Class<?> getColumnClass(int column) {
            return String.class;
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return true;
        }

        @Override
        public void setValueAt(Object aValue, int row, int column) {
            if(aValue instanceof String) {
                data.set(row, (String)aValue);
                fireTableRowsUpdated(row, column);
            } else throw new IllegalStateException("aValue is not a String");
        }

        public void addElement(String s) {
            data.add(s);
        }

    }

}
4

1 回答 1

2

发生的事情是表格 UI 在更改选择之前开始编辑单元格。如果在 getTableCellEditor() 和 shouldSelectCell() 中放置 println,则首先调用编辑器,其中 isSelected == false,然后调用 shouldSelectCell 并更改选择。

您可以在 BasicTableUI.adjustSelection(MouseEvent) 中确切地看到它发生的位置。

  boolean dragEnabled = table.getDragEnabled();

            if (!dragEnabled && !isFileList && table.editCellAt(pressedRow, pressedCol, e)) {
                setDispatchComponent(e);
                repostEvent(e);
            }

            CellEditor editor = table.getCellEditor();
            if (dragEnabled || editor == null || editor.shouldSelectCell(e)) {
                table.changeSelection(pressedRow, pressedCol, 
                        BasicGraphicsUtils.isMenuShortcutKeyDown(e), 
                        e.isShiftDown());
            }

至于渲染目的,我只是将它渲染为好像 selected == true 一样,因为它会在该事件完成处理之前。

于 2012-01-03T16:23:12.273 回答