e. john e. john - 1 year ago 86
Java Question

passing information from one jframe to another

To begin with, I know that using multiple jframes is frowned upon, unfortunately i have gotten myself to deep into this project to start over. my issue is i cannot find a way to transfer data (user input) from one frame to another, i will provide the code i need to be transferred from frame 1 to another frame

this is my code for the name and email they have to input

JTextArea txtrEnterYourFull = new JTextArea();
txtrEnterYourFull.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
txtrEnterYourFull.setText("Enter your full name");
txtrEnterYourFull.setBounds(52, 58, 166, 29);

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

nameL.setBounds(52, 93, 284, 26);

JTextArea txtroptionalEnterYour = new JTextArea();
txtroptionalEnterYour.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
txtroptionalEnterYour.setText("(Optional) Enter your email");
txtroptionalEnterYour.setBounds(52, 139, 193, 29);

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


here is the button code to go to the new frame

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

frame2 fram = new frame2 ();

i am new to swing and i dont need someone to complete my program. i just need to know how to carry it over to a new text box on a new frame.

Answer Source

Doing this is fairly easy. All you need to do is to set a constructor in which you are passing your frame with the values you need over to your new frame.

For example, I have a LoginScreen.java and DoctorScreen.java. If my user successfully entered his details and logs in, I transfer an ArrayList of Doctors from one JFrame to another JFrame, or more precisely, from one java class to another by creating a new instance of that class

Example here

Passing an arraylist from LoginScreen.java to DoctorScreen.java

DoctorScreen dScreen = new DoctorScreen(frame, docList,d);

Now Taking those values passed from LoginScreen.java and setting them in DoctorScreen.java

public DoctorScreen(JFrame frame, ArrayList<Doctor> docList, Doctor d) {
    // TODO Auto-generated constructor stub
    dialog = new JFileChooser(System.getProperty("user.dir"));
    this.frame = frame;
    this.docList = docList;
    this.d = d;

Now, you can change the DoctorScreen Constructor to fit into whatever project you are trying to do.

The steps I would advise you to take is to create a Java file for handling your input, and the second Java file to display what you entered in the first file


JButton btnContinue = new JButton("Continue");
btnContinue.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       String field1 = txtrEnterYourFull.getText();
       String name = nameL.getText();
       String field2 = txtroptionalEnterYour.getText();

       Display display = new Display(name, field1, field2);//using this as example


Then in your display.java, call your constructor taking in these fields and display them either in a textfield/textarea or in a JLabel in your frame

 String name, field1, field2;

 public Display(String name, String field1, String field2){

     this.name = name;
     this.field1 = field1;
     this.field2 - field2;

Please take note that these variables have already been declared and I am merely using this for demonstration purposes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download