在基于JTable面板的单元格编辑器中丢失第一个字符 [英] Losing first character in JTable panel based cell editor

查看:135
本文介绍了在基于JTable面板的单元格编辑器中丢失第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a cell editor that contains a little button and then a textfield that can be used to edit the value inline

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

I use setSurrendersFocusOnKeystroke(true) and a focus listener in order to allow a user to start editing immediately from the keyboard, but the trouble is the fisrt key pressed seems to get consumed rather being added to the text field, how can I prevent this ?

下面的完整自包含示例

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) {
            }
        }
    }
}


推荐答案

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

So I have found a solution, thanks to this article http://jroller.com/santhosh/entry/keyboard_handling_in_tablecelleditor , and some useful discussion abou this and how it can be applied to other components at http://forums.java.net/jive/thread.jspa?messageID=482236&#482236

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

Don't fully understand the solution this whole area seems to be rather a minefield

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

I've also added this solution Get correct editing behaviour in JTable using java DefaultCellEditor into this so that when you start editing a field using the keyboard the existing value is replaced, but not when you double click o the field.

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

My one confusion is that I'm not receiving a Key Event as I'd expect but just null so I've had to account for that.

我已经从使用setSurrenderKeystrokes(true)回来了,因为这会导致其他编辑器出现问题,例如直截了当的textfieldingitor

Ive gone back from using setSurrenderKeystrokes(true) because this causes problems with others editors such as the straightforward 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;
            }
        }
    }
}

这篇关于在基于JTable面板的单元格编辑器中丢失第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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