Java JTextField get and set Document as model

Description

Java JTextField get and set Document as model

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.Document;

public class Main extends JFrame {
  public Main() {
    super("JButton");

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    JLabel nameLabel = new JLabel("Name:");
    JLabel mirroredNameLabel = new JLabel("Mirrored Name:");
    JTextField name = new JTextField(20);
    JTextField mirroredName = new JTextField(20);

    getContentPane().add(nameLabel);//from   w w  w  .j a va2 s  .c om
    getContentPane().add(name);
    getContentPane().add(mirroredNameLabel);
    getContentPane().add(mirroredName);

    Document nameModel = name.getDocument();
    mirroredName.setDocument(nameModel);

  }

  public static void main(String[] args) {
    Main frame = new Main();
    frame.pack();
    frame.setVisible(true);
  }
}



PreviousNext

Related