-1

jframe 1: the user will input the information that is asked on the text fields and when they click the get started button, the input should show in another jframe.

jframe 2: the information that was inputted in the text fields on jframe1 should appear on the jlabels.

4 Answers4

1
  • Is it considered good practice to use two multiple frames? NO
  • Are there alternatives to using two frames? Yes
  • What are the alternatives? CardLayout, modal JDialog, among others
  • Will I still help you solve this query? Sure, why not?

Have an instance of the second frame in the first frame. Pass the information to the second JFrame constructor. It's that simple.

public class Frame1 extends JFrame {
    String text1;
    String text2;
    Frame2 frame2;       <--- second frame

    ....

    public void actionPerformed(ActionEvent e) {
        text1 = textField1.getText();
        text2 = textField2.getText();
        frame2 = new Frame2(text1, text2);
    }
}

public class Frame2 extends JFrame {
    String text1;
    String text2;

    public Frame2(String text1, String text2){
        this.text1 = text1;
        this.text2 = text2;
    }
}

When the second frame is instantiated in the actionPerformed, It will make the second frame appear. It is also being passed the information from the text fields. You may also want to dispose of the first frame.


A modal JDialog is just as easy to make as a JFrame it's the same structure. Only difference is you can set it up to be modal (meaning nothing else that it not the JDialog) can be accessed. See this answer to see hoe to set up a JDialog for a login

Here's a very simple program using a JDialog with your requirements

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class JDialogDemo {
    MyDialog dialog;
    JLabel label;
    JFrame frame;

    public JDialogDemo() {

        label = new JLabel("            ");
        frame = new JFrame("Hello World");
        frame.add(label);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();

        dialog = new MyDialog(frame, true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new JDialogDemo();
            }
        });
    }

    class MyDialog extends JDialog {
        private JButton button = new JButton("Submit");
        private JTextField jtf1;
        private JTextField jtf2;


        public MyDialog(final Frame frame, boolean modal) {
            super(frame, true);

            jtf1 = new JTextField(15);
            jtf2 = new JTextField(15);

            add(button, BorderLayout.SOUTH);
            add(jtf1, BorderLayout.CENTER);
            add(jtf2, BorderLayout.NORTH);

            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    String text1 = jtf1.getText();
                    String text2 = jtf2.getText();
                    label.setText(text1 + " " + text2);

                    dispose();
                    frame.revalidate();
                    frame.repaint();
                    frame.setVisible(true);

                }
            });

            pack();
            setVisible(true);
        }

    }

}

It should be fairly easy to follow. Let me know if you have any questions about it

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

If jframe 1 call jframe 2, then u can create a constructor in jframe 2, and pass this values.

Or you can do that by haveing a object contains jframe 1, then get in the second frame, get them by getter methods.

But for a good practice you should use single jframe and dialogs.

Salah
  • 8,567
  • 3
  • 26
  • 43
0
    JFrame frame1 = new JFrame("frame1");
    JPanel panel = new JPanel();
    final JTextField textField = new JTextField("Text Here");
    JButton button = new JButton("Copy label to other frame");
    panel.add(textField);
    panel.add(button);
    frame1.add(panel);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.pack();
    frame1.setLocationByPlatform(true);
    frame1.setVisible(true);

    JFrame frame2 = new JFrame("frame2");
    final JLabel label2 = new JLabel("Label 2 Text");
    frame2.add(label2);
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.pack();
    frame2.setLocationRelativeTo(null);
    frame2.setVisible(true);

    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            label2.setText(textField.getText());
        }
    });

Of course, your question isn't worded very well to show what you're really trying to accomplish or better yet, what you've already tried. Nevertheless, this code does what your specifications ask for.

ryvantage
  • 13,064
  • 15
  • 63
  • 112
0

If you want to put on jlabels the text that the user has inputted, you must create first the jframe1, and then jframe1 will create jframe2 passing it the text. The jframe2 will create the jlabels setting the text in the constructor.

If you want that jframe1 desappears, I think you can't close it (because if you close it, like jframe1 created jframe2, this one will be closed too). You can hide the jframe1 setting it as not visible when the user has clicked the get started button.

jframe1.setVisible(false);
carexcer
  • 1,407
  • 2
  • 15
  • 27