Java Swing How to - Pass information between JFrames








Question

We would like to know how to pass information between JFrames.

Answer

import java.awt.BorderLayout;
/* w w w .j  av  a 2 s .c  o m*/
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JSlider;
import javax.swing.JTextField;

public class Main extends javax.swing.JFrame {
  JSlider slider = new JSlider();
  JButton btnNewButton = new JButton("Do");
  JDialog d = new JDialog();
  JTextField text = new JTextField();
  public Main() {    
    btnNewButton.addActionListener(e -> d.setVisible(true));
    getContentPane().add(btnNewButton, BorderLayout.CENTER);
    
    slider.addChangeListener(e -> text.setText(Integer.toString(slider
        .getValue())));

    getContentPane().add(slider, BorderLayout.NORTH);
    d.getContentPane().add(text);
    d.pack();

    pack();
    setVisible(true);
  }

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