Synchronize JTextFields Values by PropertyChangeListener

后端 未结 6 1058
盖世英雄少女心
盖世英雄少女心 2020-12-19 18:12

I want to copy the value of a jTextField - TXTFLD1 to another jTextField -TXTFLD2 when the value at TXTFLD1 changes.
I choose propertych

相关标签:
6条回答
  • 2020-12-19 18:33

    That's because there's no one firing property change events. Read up on How to Write an Action Listener, although in order to trigger an action event, it is required that you press Enter. Otherwise, you'll need to read up on How to Write a Document Listener.

    0 讨论(0)
  • 2020-12-19 18:39

    You should register a document listener to it's document. Taken from JTextField documentation:

    In the JTextComponent based components, changes are broadcasted from the model via a DocumentEvent to DocumentListeners. The DocumentEvent gives the location of the change and the kind of change if desired. The code fragment might look something like:

     DocumentListener myListener = ??;
     JTextField myArea = ??;
     myArea.getDocument().addDocumentListener(myListener);
    
    0 讨论(0)
  • 2020-12-19 18:42

    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();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-19 18:53

    Change the PropertyChangeListener with an ActionListener or DocumentListener.

    0 讨论(0)
  • 2020-12-19 18:55
    DocumentListener yourDocListener=...;
    TXTFLD1.getDocument().addDocumentListener(yourDocListener);
    
    0 讨论(0)
  • 2020-12-19 18:55

    Write this method instead of.

    Replace TXTFLD1.addPropertyChangeListener with this method:

    TXTFLD1.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent arg0) {}
        @Override
        public void keyReleased(KeyEvent arg0) {
            TXTFLD2.setText(TXTFLD1.getText());
        }
        @Override
        public void keyPressed(KeyEvent arg0) {}
    });
    
    0 讨论(0)
提交回复
热议问题