Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

java - Synchronize JTextFields Values by PropertyChangeListener

I want to copy the value of a jTextField - TXTFLD1 to another jTextField -TXTFLD2 when the value at TXTFLD1 changes.
I choose propertychangelistener because i cannot detect when the value at TXTFLD1 is changed, Because it is changed by some external code which i cannot modify now.

The test code is as follows :


public class TxtFldSync extends JFrame {
    private JButton BTN1 = null;
    private JTextField TXTFLD1 = null;
    private JTextField TXTFLD2 = null;

    public static void main(String[] args) {
            TxtFldSync thisClass = new TxtFldSync();
            thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            thisClass.setVisible(true);
    }
    public TxtFldSync() {
        super();
        this.setSize(300, 200);
        BTN1 = new JButton();
        BTN1.setBounds(new Rectangle(178, 38, 67, 17));
        TXTFLD1 = new JTextField();
        TXTFLD1.setBounds(new Rectangle(32, 42, 83, 20));

        TXTFLD2 = new JTextField();
        TXTFLD2.setBounds(new Rectangle(30, 78, 83, 20));

        //listeners
        TXTFLD1.addPropertyChangeListener("value", new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent arg0) {
                TXTFLD2.setText(TXTFLD1.getText()+"set by change listener");
                //this doesnot work why ?
            }
        });
        BTN1.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                TXTFLD1.setText("Action Performed");
                //i what to set same value to TXTFLD2 using property change listener
            }
        });

        this.setContentPane(new Container());
        this.getContentPane().add(BTN1);
        this.getContentPane().add(TXTFLD1);
        this.getContentPane().add(TXTFLD2);
    }
}

Why the property change listener is not working. What are the other alternatives solution for this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For example, as StanislavL +1 suggested by using DocumentListener:

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class TextLabelMirror {

    private JPanel mainPanel = new JPanel();
    private JTextField field = new JTextField(20);
    private JTextField field1 = new JTextField(20);

    public TextLabelMirror() {
        field.getDocument().addDocumentListener(new DocumentListener() {

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

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateLabel(e);
            }

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

            private void updateLabel(DocumentEvent e) {
                java.awt.EventQueue.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        field1.setText(field.getText());
                    }
                });
            }
        });

        mainPanel.setLayout(new GridLayout(1, 0, 10, 0));
        mainPanel.add(field);
        mainPanel.add(field1);
    }

    public JComponent getComponent() {
        return mainPanel;
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("TextLabelMirror");
        frame.getContentPane().add(new TextLabelMirror().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...