-1

I have my original text field in my first frame and I need it to be displayed in my other jframe. The code is:

JButton btnContinue = new JButton("Continue");
    btnContinue.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            String msg = nameL.getText();

            frame2 fram = new frame2 ();
            fram.setVisible(true);
            frame.dispose();

            new order (msg) .setVisible(true);

'nameL' is the textbox the user enters their name in.

This code describe where it should be displayed:

public order(String para){
        getComponents();


    JLabel lblNewLabel_1 = new JLabel("");
    lblNewLabel_1.setBounds(91, 27, 254, 84);
    contentPane.add(lblNewLabel_1);
    lblNewLabel_1.setText(para);

this is my first class where the user inputs their name

public order(String para){
        getComponents();


    import java.awt.EventQueue;
  import javax.swing.JFrame;
 import javax.swing.JButton;
 import javax.swing.JTextField;
 import javax.swing.JTextArea;
 import java.awt.Color;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 import java.awt.Font;

 public class frame1 {

public JFrame frame;
public JTextField nameL;
public JTextField textField_1;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame1 window = new frame1();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public frame1() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setEnabled(false);
    frame.setResizable(false);
    frame.getContentPane().setBackground(Color.GRAY);
    frame.setForeground(Color.WHITE);
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnContinue = new JButton("Continue");
    btnContinue.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            String msg = nameL.getText();

            frame2 fram = new frame2 ();
            fram.setVisible(true);
            frame.dispose();

            new order (msg) .setVisible(true);

        }
        }

    );
    btnContinue.setBounds(0, 249, 450, 29);
    frame.getContentPane().add(btnContinue);

    JTextArea txtrPleaseEnterYour = new JTextArea();
    txtrPleaseEnterYour.setEditable(false);
    txtrPleaseEnterYour.setBackground(Color.LIGHT_GRAY);
    txtrPleaseEnterYour.setBounds(0, 0, 450, 32);
    txtrPleaseEnterYour.setText("\tPlease enter your name and email below\n If you do not have or want to provide an email, Leave the space blank");
    frame.getContentPane().add(txtrPleaseEnterYour);

    JTextArea txtrEnterYourFull = new JTextArea();
    txtrEnterYourFull.setEditable(false);
    txtrEnterYourFull.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
    txtrEnterYourFull.setBackground(Color.GRAY);
    txtrEnterYourFull.setText("Enter your full name");
    txtrEnterYourFull.setBounds(52, 58, 166, 29);
    frame.getContentPane().add(txtrEnterYourFull);



    nameL = new JTextField();
    nameL.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    }
    );
    nameL.setBackground(Color.LIGHT_GRAY);
    nameL.setBounds(52, 93, 284, 26);
    frame.getContentPane().add(nameL);
    nameL.setColumns(10);


    JTextArea txtroptionalEnterYour = new JTextArea();
    txtroptionalEnterYour.setEditable(false);
    txtroptionalEnterYour.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
    txtroptionalEnterYour.setBackground(Color.GRAY);
    txtroptionalEnterYour.setText("(Optional) Enter your email");
    txtroptionalEnterYour.setBounds(52, 139, 193, 29);
    frame.getContentPane().add(txtroptionalEnterYour);

    textField_1 = new JTextField();
    textField_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    textField_1.setBackground(Color.LIGHT_GRAY);
    textField_1.setBounds(52, 180, 284, 26);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);

 }
  }

my second class where the text field has to be set

import java.awt.EventQueue;

 import javax.swing.JFrame;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.border.EmptyBorder;
 import java.awt.Color;
 import javax.swing.JButton;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 import javax.swing.JLabel;

 public class order extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;
public JPanel contentPane;
/**
 * Launch the application.
 */

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                order frame = new order();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public order() {
    setAlwaysOnTop(false);
    setTitle("Order Details // Finalization");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 451, 523);
    contentPane = new JPanel();
    contentPane.setBackground(Color.LIGHT_GRAY);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnNewButton = new JButton("Submit Order");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setAlwaysOnTop(true);
            JOptionPane.showMessageDialog(null, "Your Order Has Been Confirmed", "enjoy your meal", JOptionPane.YES_NO_OPTION);

        }
    });
    btnNewButton.setBounds(164, 466, 117, 29);
    contentPane.add(btnNewButton);

}

    public order(String para){
        getComponents();    

    JLabel lblNewLabel_1 = new JLabel("");
    lblNewLabel_1.setBounds(91, 27, 254, 84);
    contentPane.add(lblNewLabel_1);
    lblNewLabel_1.setText(para);


    }
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
huaglo
  • 33
  • 1
  • 7
  • 2
    What is your question precisely ? – Arnaud Nov 02 '16 at 14:19
  • the user enters their name in a text field, it brings them to an order screen and when they go to the "receipt" jframe they should have their name appear, i cant find a way to transfer it, tried lots of methods and nothing works. @Berger – huaglo Nov 02 '16 at 14:21
  • It seems that your code is already doing that, what doesn't work ? Maybe add the whole classes so that we can see the problem better. – Arnaud Nov 02 '16 at 14:22
  • okay ill edit my main post and throw in both my first frame class and my order class – huaglo Nov 02 '16 at 14:25
  • @Berger i updated it – huaglo Nov 02 '16 at 14:29
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 3) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 4) Is your question *"How to X?"*? If so, add it as an [edit to the question](). If not, think of a specific question and do the same (edit). – Andrew Thompson Nov 02 '16 at 21:00
  • Can you post code for `getComponents();` ? –  Nov 02 '16 at 23:40

1 Answers1

1

Open both JFrames, have them listen to the EventQueue for a custom "string display" event.

Wrap the string to be displayed in a custom event.

Throw that on the event queue, allowing both the JFrames to receive the event, which they will then display accordingly.

---- Edited with an update ----

Ok, so you're a bit new to Swing, but hopefully not too new to Java. You'll need to understand sub-classing and the "Listener" design pattern.

Events are things that Components listen for. They ask the dispatcher (the Swing dispatcher is fed by the EventQueue) to "tell them about events" and the dispatcher then sends the desired events to them.

Before you get too deep in solving your problem, it sounds like you need to get some familiarity with Swing and its event dispatch, so read up on it here.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • newbie here, i dont know how to write out this code on my own – huaglo Nov 02 '16 at 14:36
  • @e.john Added a bit more to help you get going. It's not hard, and while you start off with Events outside of the Swing EventQueue, you can eventually post them to the EventQueue if you desire. – Edwin Buck Nov 02 '16 at 15:05