2

次の方法で、テーブルの 2 つの列にセル エディターを定義しました。

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 (エディターとしてコンボ ボックスがある) の場合も同様です。ここでは、セルをクリックしない限りコンボ ボックスは表示されません。これらの動作は直感に反し、キーボードで操作するユーザーにとって望ましくありません.:(

これを解決する方法についての指針を提案してください。

前もって感謝します。

4

2 に答える 2

3

上記のように、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));
        }
    }
}
于 2012-08-07T02:32:23.200 に答える
3
  1. このは、 usingを持つ aをオーバーライドeditCellAt()します。JTableDefaultCellEditorJTextField

  2. に対して定義されSpaceたアクションにキーをバインドできます。startEditingJTable

    table.getInputMap().put(
        KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "startEditing");
    
于 2012-08-07T02:23:24.043 に答える