3

我有一个包含一个小按钮的单元格编辑器,然后是一个可用于编辑内联值的文本字段

我使用 setSurrendersFocusOnKeystroke(true) 和焦点侦听器以允许用户立即从键盘开始编辑,但问题是按下的第一个键似乎被消耗而不是被添加到文本字段中,我该如何防止这种情况?

下面是完整的自包含示例

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class PanelTableEditorTest extends JFrame {

    private JTable table;

    public PanelTableEditorTest() {
        this.setLayout(new BorderLayout());
        table = new JTable(10, 10);
        table.getSelectionModel().setSelectionMode(
            ListSelectionModel.SINGLE_SELECTION);
        table.setCellSelectionEnabled(true);
        table.setDefaultEditor(Object.class, new SimpleMultiRowCellEditor());
        table.setSurrendersFocusOnKeystroke(true);
        table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0),
            "none");
        table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER, 0),
            "startEditing");
        this.add(table.getTableHeader(), BorderLayout.NORTH);
        this.add(table, BorderLayout.CENTER);
        pack();
        setVisible(true);
    }

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

            public void run() {
                new PanelTableEditorTest();
            }
        });
    }

    public class SimpleMultiRowCellEditor extends DefaultCellEditor {

        final JPanel panel;
        private final JButton rowCount;

        public SimpleMultiRowCellEditor() {
            super(new JTextField());
            this.setClickCountToStart(1);

            rowCount = new JButton();
            rowCount.setVisible(true);
            panel = new JPanel();
            panel.setOpaque(false);
            panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
            panel.add(rowCount);
            panel.add(editorComponent);
            panel.addFocusListener(new PanelFocusListener());
        }

        public Component getTableCellEditorComponent(
            final JTable table,final Object val, final boolean isSelected,
            final int row, final int column) {
            rowCount.setText("1");
            delegate.setValue(val);
            editorComponent.requestFocusInWindow();
            return panel;
        }

        class PanelFocusListener implements FocusListener {

            public void focusGained(FocusEvent e) {
                editorComponent.requestFocusInWindow();
            }

            public void focusLost(FocusEvent e) {
            }
        }
    }
}
4

2 回答 2

2

所以我找到了一个解决方案,感谢这篇文章http://jroller.com/santhosh/entry/keyboard_handling_in_tablecelleditor以及一些关于此的有用讨论以及如何将其应用于http://forums.java.net上的其他组件/jive/thread.jspa?messageID=482236宼

不完全理解解决方案 这整个区域似乎是一个雷区

我还添加了这个解决方案在 JTable 中使用 java DefaultCellEditor 获得正确的编辑行为,这样当您开始使用键盘编辑字段时,现有值会被替换,而不是当您双击该字段时。

我的一个困惑是,我没有收到预期的关键事件,但只是空,所以我不得不考虑这一点。

我已经不再使用 setSurrenderKeystrokes(true) 了,因为这会导致其他编辑器出现问题,例如直接的 textfieldeditor

import javax.swing.*;
import javax.swing.text.Caret;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.EventObject;

public class PanelTableEditorTest extends JFrame
{

    private JTable table;

    public PanelTableEditorTest()
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e)
        {

        }
        this.setLayout(new BorderLayout());
        table = new JTable(4, 4);
        table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setCellSelectionEnabled(true);
        table.setSurrendersFocusOnKeystroke(false);
        table.setDefaultEditor(Object.class,new SimpleMultiRowCellEditor());
        table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(java.awt.event.
                        KeyEvent.VK_F2, 0), "none");
        table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(java.awt.event.
                        KeyEvent.VK_ENTER, 0), "startEditing");

        this.add(table.getTableHeader(), BorderLayout.NORTH);

        this.add(table, BorderLayout.CENTER);
        pack();
        setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new PanelTableEditorTest();

            }
        });
    }

    public class SimpleMultiRowCellEditor extends DefaultCellEditor
    {
        private EventObject event;
        final JPanel panel;
        private final JButton rowCount;

        public SimpleMultiRowCellEditor()
        {
            super(new JTextField());
            this.setClickCountToStart(1);

            rowCount = new JButton();
            rowCount.setVisible(true);
            panel = new TableEditorPanel();
            panel.setRequestFocusEnabled(true);
            panel.setOpaque(false);
            panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
            panel.add(rowCount);
            panel.add(editorComponent);
        }

        public boolean isCellEditable(EventObject anEvent)
        {
            event=anEvent;
            return super.isCellEditable(anEvent);
        }

        public Component getTableCellEditorComponent(final JTable table, final Object val, final boolean isSelected, final int row, final int column)
        {
            rowCount.setText("1");
            delegate.setValue(val);
            if(event instanceof KeyEvent || event==null)
            {
                final Caret caret = ((JTextField)editorComponent).getCaret();
                caret.setDot(0);
                ((JTextField)editorComponent).setText("");                
            }
            return panel;
        }

        class TableEditorPanel extends JPanel
        {

            public void addNotify(){
                super.addNotify();
                editorComponent.requestFocus();
            }

            protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed){
                InputMap map = editorComponent.getInputMap(condition);
                ActionMap am = editorComponent.getActionMap();

                if(map!=null && am!=null && isEnabled()){
                    Object binding = map.get(ks);
                    Action action = (binding==null) ? null : am.get(binding);
                    if(action!=null){
                        return SwingUtilities.notifyAction(action, ks, e, editorComponent,
                                e.getModifiers());
                    }
                }
                return false;
            }
        }
    }
}
于 2010-10-22T14:09:38.923 回答
0

添加一个

rowCount.setFocusable(false);

在 SimpleMultiRowCellEditor 构造函数中,防止按钮检索焦点,使 JTextfield 是唯一的如何可以对单元格编辑有焦点

于 2010-10-22T11:07:55.523 回答