Can you link values for two JFormattedTextFields?

你说的曾经没有我的故事 提交于 2019-12-02 08:53:35

I need the values (not just the displayed text) to be identical. Ideally they should both be editable, with a change in one being mirrored in the other.

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

You can use a key listener. You simply add a key listener to both fields using the below code. the reason you need the other events is it will throw errors unless you have them in the code.

import javax.swing.*;   
import java.awt.*;  
import java.awt.event.*;

public class CreateGrid 
{    
    JFrame thisframe;
    JFormattedTextField jFormattedTextField1, jFormattedTextField2;

    public CreateGrid()
    {
        GridLayout thislayout = new GridLayout(0,2);
        thisframe = new JFrame();
        thisframe.setLayout(thislayout);

        jFormattedTextField1 = new JFormattedTextField();
        jFormattedTextField2 = new JFormattedTextField();
        jFormattedTextField1.addKeyListener(new KeyAdapter()
        {
            public void keyReleased(KeyEvent e) 
            {                
                JFormattedTextField textField = (JFormattedTextField) e.getSource();
                String text = textField.getText();
                jFormattedTextField2.setText(text);
            }
            public void keyTyped(KeyEvent e) 
            {
            }
            public void keyPressed(KeyEvent e) 
            {
            }            
        });
        jFormattedTextField2.addKeyListener(new KeyAdapter()
        {
            public void keyReleased(KeyEvent e) 
            {                
                JFormattedTextField textField = (JFormattedTextField) e.getSource();
                String text = textField.getText();
                jFormattedTextField1.setText(text);
            }
            public void keyTyped(KeyEvent e) 
            {
            }
            public void keyPressed(KeyEvent e) 
            {
           }            
       });
       thisframe.add(jFormattedTextField1);
       thisframe.add(jFormattedTextField2);
       thisframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       thisframe.setVisible(true);
       thisframe.pack();
   }

public static void main(String args[])
{
    new CreateGrid();
}    

} I have tested this out and it works perfectly what ever you type into one field will show up in the other as you type it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!