简体   繁体   English

更改焦点时,为什么在DocumentListener中调用insertUpdate? (Java秋千)

[英]Why does insertUpdate get called in my DocumentListener when I change focus? (Java Swing)

I have a JTextField with a documentListener on it. 我有一个JTextField,上面有一个documentListener。 I want to change the background color when I add or remove characters to this textfield. 我想在此文本字段中添加或删除字符时更改背景颜色。 I should be using a document listener correct? 我应该使用文档监听器正确吗? It works, but it also fires when I gain and lose focus on this JTextfield, which is undesired. 它可以工作,但是当我对这个JTextfield失去关注时,它也会触发。 I do not add a focus listener on this JTextField. 我没有在此JTextField上添加焦点侦听器。 Here is my code, any suggestions on how I can fix my problem? 这是我的代码,关于如何解决问题的任何建议?

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

Also note that I am using JGoodies Binding which I am starting to believe is the root of this problem. 还请注意,我正在使用JGoodies Binding,我开始相信这是此问题的根源。 Swing w/o JGoodies shouldn't be firing off document listener events by changing focus... 不使用JGoodies摇摆不应该通过更改焦点来触发文档侦听器事件...

You must have something looking at the focus or you think it is firing and it is not. 您必须有一些东西在关注焦点,否则您就认为它正在点火,而事实并非如此。

I took your code and made a complete example and it does not have the problem you describe. 我接受了您的代码并制作了一个完整的示例,它没有您描述的问题。

        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);

Have you looked at the DocumentEvent to see what information it contains? 您是否查看过DocumentEvent来查看其中包含的信息? Does it actually contain a string that has changed. 它实际上是否包含已更改的字符串。 Or is it just an event with a 0 length string. 还是仅仅是一个长度为0的字符串的事件。 If it is the latter then maybe you can just ignore that case. 如果是后者,那么也许您可以忽略这种情况。

I figured it out. 我想到了。 It 100% had to do with JGoodies Binding. 100%与JGoodies Binding有关。

This code works: 此代码有效:

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

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

Since I am using JGoodies Binding, I have a ValueModel backing my JTextField. 因为我使用的是JGoodies绑定,所以我有一个ValueModel支持我的JTextField。 The listener has to be set there and not on the JTextField. 侦听器必须在此处设置,而不是在JTextField上设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM