Skip to content

Instantly share code, notes, and snippets.

@julianjupiter
Created November 13, 2017 07:23
Show Gist options
  • Save julianjupiter/7655ec83104208b71930cddaa7016620 to your computer and use it in GitHub Desktop.
Save julianjupiter/7655ec83104208b71930cddaa7016620 to your computer and use it in GitHub Desktop.
Pass string from one JFrame to another.
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class JFrameDemo {
public static void main(String[] args) {
    String name = "Juan";
      String message = "Hello world";
      JFrame frame = new JFrame("My Application");
      JButton button = new JButton("hello");
      button.setPreferredSize(new Dimension(50, 25));
      button.addActionListener(e -> newJFrame2(name, message));
      frame.getContentPane().add(button, BorderLayout.PAGE_START);
      frame.setSize(500, 300);
      frame.setVisible(true);
   }
}
class JFrame2 extends JFrame {
   private static final long serialVersionUID = 333465410872476840L;
   String myName;
   String message;
   JLabel label;
   
public JFrame2(String myName, Stringmessage) {
    this.myName = myName;
      this.message = message;
      initComponents();
   }
   void initComponents() {
      label = new JLabel(message + ", " +myName + ".");
      label.setPreferredSize(new Dimension(50, 25));
      this.getContentPane().add(label, BorderLayout.PAGE_START);
      this.setSize(300, 100);
      this.setVisible(true);
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment