JTable在按键上编辑 [英] JTable edit on keypress

查看:94
本文介绍了JTable在按键上编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式开始在按键上的JTable中编辑当前行的第三列.

I am trying to programatically start editing third column of current row in a JTable on a keypress.

我实现了一个KeyListener,它在keyReleased()中包含此代码

I've implemented a KeyListener which in keyReleased() contains this code

if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
    myTab.changeSelection(myTab.getSelectedRow(), 2, true, false);
    myTab.editCellAt(myTab.getSelectedRow(), 2);
}

当我松开Enter键时,单元格确实是可编辑的(我可以在末尾键入),但是没有插入符号.

When I release enter, the cell is indeed editable (I can type at the end), but there is no caret.

当我用鼠标单击时,行为是预期的(我可以编辑并且存在carret).

When I click with mouse, behaviour is as expected (I can edit and carret is present).

此外,我注意到在释放键时,我的celleditor为null,在鼠标单击时为null.

Also, I've noticed that on keyrelease, my celleditor is null, and on mouseclick it is not null.

我在做什么错了?

推荐答案

避免使用KeyListener,在关注焦点和调度事件的顺序时,这是不可靠的(可能在您的侦听器之前消耗了它们的密钥) ,因此您将永远不会收到通知.)

Avoid KeyListener, it is unreliable when it comes to focus and the order in which the events are dispatched (it's possible to have they key consumed BEFORE your listener, so you would never be notified).

改为使用键绑定

InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = table.getActionMap();

KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);

im.put(enterKey, "Action.enter");
am.put("Action.enter", new AbstractAction() {
    public void actionPerformed(ActionEvent evt) {
        table.changeSelection(table.getSelectedRow(), 2, false, false);
        if (!table.editCellAt(table.getSelectedRow(), 2)) {
            JOptionPane.showMessageDialog(table, "Failed to start cell editing");
        }
    }
});

我对这个myTab.changeSelection(myTab.getSelectedRow(), 2, true, false);电话也很怀疑. JavaDoc基本上说...

I'm also suspicious of this myTab.changeSelection(myTab.getSelectedRow(), 2, true, false); call. The JavaDocs basically say...

切换:true,扩展:false.如果选择了指定的单元格,请取消选择它.如果未选中,请选择它.

toggle: true, extend: false. If the specified cell is selected, deselect it. If it is not selected, select it.

向我建议,如果当前已选择该单元格,则该单元格将变为未选中状态.

Which suggest to me, if the cell is currently selected, it will be made unselected.

已更新了工作示例

public class TestTableEditor {

    public static void main(String[] args) {
        new TestTableEditor();
    }

    private JTable table;

    public TestTableEditor() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                table = new JTable(new MyTableModel());
                InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
                ActionMap am = table.getActionMap();

                KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);

                im.put(enterKey, "Action.enter");
                am.put("Action.enter", new AbstractAction() {
                    public void actionPerformed(ActionEvent evt) {
                        table.changeSelection(table.getSelectedRow(), 1, false, false);
                        if (!table.editCellAt(table.getSelectedRow(), 1)) {
                            JOptionPane.showMessageDialog(table, "Failed to start cell editing");
                        }
                    }
                });

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MyTableModel extends AbstractTableModel {

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

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

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Object value = null;
            switch (columnIndex) {
                case 0:
                    value = "Can't edit";
                    break;
                case 1:
                    value = "Edit me";
                    break;
                case 2:
                    value = "Can't edit";
                    break;
            }
            return value;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex == 1;
        }
    }
}

已更新

在所有愚蠢,痛苦,难寻的东西中……

Of all the stupid, painful, hard to find ...

table.setSurrendersFocusOnKeystroke(true);添加到您的代码中...

Add table.setSurrendersFocusOnKeystroke(true); to your code...

设置此JTable中的编辑器在以下情况下是否获得键盘焦点: JTable转发键盘激活了编辑器 单元的事件.默认情况下,此属性为false,而JTable 除非单击该单元格,否则将保持焦点.

Sets whether editors in this JTable get the keyboard focus when an editor is activated as a result of the JTable forwarding keyboard events for a cell. By default, this property is false, and the JTable retains the focus unless the cell is clicked.

这篇关于JTable在按键上编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆