Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我更改焦点时,为什么会在DocumentListener中调用insertUpdate?(Java Swing)_Java_Swing_Document_Listener_Jgoodies - Fatal编程技术网

当我更改焦点时,为什么会在DocumentListener中调用insertUpdate?(Java Swing)

当我更改焦点时,为什么会在DocumentListener中调用insertUpdate?(Java Swing),java,swing,document,listener,jgoodies,Java,Swing,Document,Listener,Jgoodies,我有一个JTextField,上面有一个documentListener。我想在向此文本字段添加或删除字符时更改背景色。我应该使用文档侦听器,对吗?它可以工作,但当我获得或失去对这个JTextfield的关注时,它也会激发,这是不需要的。我没有在这个JTextField上添加焦点侦听器。这是我的代码,有没有关于如何解决问题的建议 numPlotRowsJTextField = BasicComponentFactory.createIntegerField(valueModelN

我有一个JTextField,上面有一个documentListener。我想在向此文本字段添加或删除字符时更改背景色。我应该使用文档侦听器,对吗?它可以工作,但当我获得或失去对这个JTextfield的关注时,它也会激发,这是不需要的。我没有在这个JTextField上添加焦点侦听器。这是我的代码,有没有关于如何解决问题的建议

        numPlotRowsJTextField = BasicComponentFactory.createIntegerField(valueModelNumberPlotRowsJTextField);
        numPlotRowsJTextField.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void removeUpdate(DocumentEvent e) 
        {
        }

        @Override
        public void insertUpdate(DocumentEvent e) 
        {
            numPlotRowsJTextField.setBackground(Color.cyan);
        }

        @Override
        public void changedUpdate(DocumentEvent e) 
        {
        }
    });

还要注意,我使用的是JGoodies绑定,我开始相信这是这个问题的根源。Swing w/o JGoodies不应该通过改变焦点来触发文档侦听器事件…

您必须有一些东西在关注焦点,或者您认为它正在触发,而不是

我用你的代码做了一个完整的例子,它没有你描述的问题

        JFrame frame = new JFrame();
    final JTextField numPlotRowsJTextField = new JTextField(3);
    numPlotRowsJTextField.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void changedUpdate(DocumentEvent e) {
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            numPlotRowsJTextField.setBackground(Color.cyan);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
        }
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(100, 100);
    frame.getContentPane().setLayout(new FlowLayout());
    frame.getContentPane().add(new JTextField(2));
    frame.getContentPane().add(numPlotRowsJTextField);
    frame.setVisible(true);

您是否查看了DocumentEvent以了解它包含哪些信息?它是否确实包含已更改的字符串。或者它只是一个长度为0的字符串事件。如果是后者,也许你可以忽略这个问题。

我想出来了。它100%与JGoodies绑定有关

此代码适用于:

ValueModel valueModelNumberPlotRowsJTextField = adapter.getBufferedModel("numberOfPlotRows");
    valueModelNumberPlotRowsJTextField.addValueChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) 
        {
            numPlotRowsJTextField.setBackground(Color.cyan);
        }
    });
    numPlotRowsJTextField = BasicComponentFactory.createIntegerField(valueModelNumberPlotRowsJTextField);

因为我使用的是JGoodies绑定,所以我有一个支持JTextField的ValueModel。监听器必须设置在那里,而不是JTextField上。

我认为JGoodies绑定不知怎么搞砸了我。它一定是导致DocumentListener激发insertUpdate,即使我只是改变了焦点。这可能与我使用JGoodies缓冲输入和触发器的面部表情有关,该触发器允许我轻松重置输入。有JGoodies绑定专家吗?