简体   繁体   English

PropertyChangeListener requestFocus问题

[英]PropertyChangeListener requestFocus issue

Completely New Direction 全新方向

Following is the SSCCE of my issue. 以下是我的问题的SSCCE。 I'm trying to get the fields to update as input is being entered, in this case focus MUST be lost for an update to occur. 我正在尝试使字段在输入输入时进行更新,在这种情况下,必须丢失焦点才能进行更新。

package test;
import java.awt.EventQueue;
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.swing.text.NumberFormatter;

public class Test {

    private JFormattedTextField input, input2;
    private NumberFormatter formatter;
    private DocumentListener listener;

    public Test() {
        formatter = new NumberFormatter(NumberFormat.getNumberInstance());
        input = new JFormattedTextField(formatter);
        input2 = new JFormattedTextField(formatter);

        listener = new DocumentListener() {

            private Document source = null;

            protected void update(Document doc) {
                if (source == null) {
                    System.out.println("Update");
                    source = doc;
                    if (source.equals(input.getDocument())) {
                        double temp;
                        temp = converter(((Number)input.getValue()).doubleValue());
                        input2.setValue(temp);
//                        input2.setText(Double.toString(temp));
                    } else if (source.equals(input2.getDocument())) {                       
                        double temp;
                        temp = converterBack(((Number)input2.getValue()).doubleValue());
                        input.setValue(temp);
//                        input.setText(Double.toString(temp));
                    }
                    source = null;
                }
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                update(e.getDocument());
            }

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

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

        input.getDocument().addDocumentListener(listener);
        input2.getDocument().addDocumentListener(listener);
        input.setColumns(4);
        input2.setColumns(4);
        input.setValue(0.0);
        JPanel panel = new JPanel();
        panel.add(input);
        panel.add(input2);

        JOptionPane.showMessageDialog(null, panel);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Test();
            }
        });

    }

    private double converter(double value) {
        value = value * 2;

        return value;
    }
    private double converterBack(double value){
        value = value/2;

        return value;
    }
}

I need to be able to utilize this code with a 'converter' method , so putting the commands into the method Update() isn't an option. 我需要能够通过'converter' method使用此代码,因此不能将命令放入method Update()中。

I have gotten this to work with PropertyChangeListener but apparently it's not a good route to take. 我已经将其与PropertyChangeListener但是显然这不是一个好方法。

A PropertyChangeListener isn't going to work for this problem, as the field's Document won't raise events...The Document is a property of the field, but it's contents is not. PropertyChangeListener无法解决此问题,因为该字段的Document不会引发事件...该Document是该字段的属性,但其内容不是。

A better solution would be to use a DocumentListener . 更好的解决方案是使用DocumentListener This will notify when the underlying document contents has changed. 这将在基础文档内容更改时通知。

You need to be careful with this, as a Document does not like been changed while it's already undergoing a change. 您需要注意这一点,因为Document在进行更改时不喜欢更改。

In this example, I use the source Document of the event and compare it to each fields Document to determine what should be updated... 在此示例中,我使用事件的源Document并将其与每个字段Document进行比较以确定应更新的内容...

例

import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.swing.text.NumberFormatter;

public class Test {

    private JFormattedTextField input, input2;
    private NumberFormatter formatter;
//    private PropertyChangeListener listener;
    private DocumentListener listener;

    public Test() {
        formatter = new NumberFormatter(NumberFormat.getNumberInstance());
        input = new JFormattedTextField(formatter);
        input2 = new JFormattedTextField(formatter);

        listener = new DocumentListener() {

            private Document source = null;

            protected void update(Document doc) {
                if (source == null) {
                    System.out.println("Update");
                    source = doc;
                    if (source.equals(input.getDocument())) {
                        input2.setText(input.getText());
                    } else if (source.equals(input2.getDocument())) {
                        input.setText(input2.getText());
                    }
                    source = null;
                }
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                update(e.getDocument());
            }

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

            @Override
            public void changedUpdate(DocumentEvent e) {
                update(e.getDocument());
            }
        };
//        listener = new PropertyChangeListener() {
//            @Override
//            public void propertyChange(PropertyChangeEvent evt) {
//                convert(evt);
//            }
//        };

        input.getDocument().addDocumentListener(listener);
        input2.getDocument().addDocumentListener(listener);

        input.setColumns(4);
        input2.setColumns(4);
//        input.addPropertyChangeListener("value", listener);
//        input2.addPropertyChangeListener("value", listener);
        input.setValue(0.0);
        JPanel panel = new JPanel();
        panel.add(input);
        panel.add(input2);

        JOptionPane.showMessageDialog(null, panel);
    }

    private void convert(PropertyChangeEvent evt) {
        if (evt.getSource() == input) {
            if (evt.getSource() != null) {
                double temp;
                temp = converter((Double) evt.getNewValue());
                input2.setValue(temp);
            }

        }

    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Test();
            }
        });

    }

    private double converter(double value) {
        value = value * 2;

        return value;
    }

}

Updated 更新

The problem you're having with your conversion is the fact that the value has not yet been "committed", meaning it has not been validated and the value property has not yet been changed. 转换时遇到的问题是,尚未“提交”值,这意味着尚未验证并且value属性尚未更改。

Calling getValue will return the last committed value. 调用getValue将返回最后的提交值。

What you need to do is commit the edit first... 您需要做的是先提交编辑...

if (source.equals(input.getDocument())) {
    try {
        input.commitEdit();
        double temp = ((Number) input.getValue()).doubleValue();
        temp = converter(temp);
        input2.setValue(temp);
    } catch (ParseException ex) {
    }

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

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