Search code examples
javaswingjlabeljtextfield

How do I transfer the data in my JTextField to a JLabel?


So we're supposed to be making a reservation screen for our Airline. After the user inputs his/her information and clicks on Reserve my code needs to show her the information back in on a separate frame. I have the frame set up, I just need to transfer the information IN the JTextField to the frame on JLabel. I heard I need to make it into a variable so I can use it in multiple places. I'm new to Java so please don't use words I might not be familiar with. Thank you and here is my code. I have 3 classes.

First Class:

import javax.swing.JFrame;
import javax.swing.JPanel;
public class TicketMan {
public static void main(String[] args) {
    JFrame frame = new JFrame ("Airport");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    TicketManBot panel = new TicketManBot();
    frame.getContentPane().add(panel);

    frame.pack();
    frame.setVisible(true); }    }

Second Class:(The class the fields are on.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;


public class TicketManBot extends JPanel {


private JLabel name;
private JLabel dest;
private JLabel depc;
private JLabel dept;
private JLabel seat;
private JButton b;


Random rand = new Random();

private JTextField namef, destf, depcf, deptf;


public TicketManBot()

{



name = new JLabel ("Enter passenger's name:");
dest = new JLabel ("Destination: ");
depc = new JLabel ("Departure city: ");
dept = new JLabel ("Departure time: ");
seat = new JLabel ("Seat Number: " + rand.nextInt(100));

b = new JButton ("Make Reservation");

b.addActionListener (new results());
// the fields I need to move
namef = new JTextField (10);
destf = new JTextField (10);
depcf = new JTextField (10);
deptf = new JTextField (10);        

 add (name);
 add (namef);
 add (dest);
 add (destf);
 add (depc);
 add (depcf);
 add (dept);
 add (deptf);
 add (seat);
 add (b);


 setPreferredSize (new Dimension(200, 300));
 setBackground (Color.gray);         
}
private class results implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
    int  resultLabel;

    String text = namef.getText();      

    JFrame results = new JFrame("Thank you for flying with JJ Air"); 
    results.setVisible(true);
    results.setSize (200, 300);
 // I need to move the fields up there in front of these VVVVVV
    JLabel nr = new JLabel ("Name of the passenger:");
    JLabel dr = new JLabel ("Destination:");
    JLabel depr = new JLabel ("Departure city:");
    JLabel dpr = new JLabel ("Departure time:");
    JLabel sr = new JLabel ("Seat number:");
    JLabel t = new JLabel ("Thank you for flying with us");
    JLabel label = new JLabel ("");
    JPanel panel = new JPanel ();
    results.add(panel);
    panel.add (nr); 
    panel.add (dr);
    panel.add (depr);
    panel.add (dpr);
    panel.add (sr);
    panel.add (t);
}
}
}

Third Class:

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;

public class results extends JPanel {

}

Solution

  • jLabel.setText(jTextField.getText());
    

    Where jLabel is your label and jTextField your text field.

    For example:

    JLabel nr = new JLabel ("Name of the passenger:"+Namef.getText());
    

    You should read up on the Java coding style guidelines by the way, it will help make your code more readable to others.