Skip to content

Trying to figure out how to print what user picks from this GUI code on to another GUI

Trying to figure out how to print what the user picks from this GUI onto another GUI and make it look like an airline ticket or receipt. I’m pretty sure I can design the airline ticket GUI. I’m having trouble coming up with code that would take whatever the user inputs and picks from the GUI code below and print it to another GUI.

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class GUI implements ActionListener {

    private static JLabel firstNamelabel, lastNamelabel, fromLabel, toLabel, adultLabel, childrenLabel, bookingDate, LDate, classLabel, passengerDetails;
    private static JTextField firstNameuserText, lastNameuserText, bookingDateText;
    private static JComboBox fromCombo, toCombo, adultCombo, childrenCombo, classCombo;
    private static JButton button;

    public static void main(String[] args) {

        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        frame.add(panel);

        panel.setLayout(null);


        String[] sItem1 = { "New York" };
        String[] sItem2 = { "Florida ", "California ", "Texas ", "Chicago ", "Nevada ", "Ohio ", "Washington ","Georgia " };
        String[] sItem3 = { "Economic", "Business" };
        String[] item4 = { "1", "2", "3", "4", "5", "6" };
        String[] item5 = { "0", "1", "2", "3", "4" };

        passengerDetails = new JLabel("Passenger Details");
        passengerDetails.setBounds(10, 1, 150, 25);
        passengerDetails.setForeground(Color.blue);
        panel.add(passengerDetails);

        firstNamelabel = new JLabel("First Name");
        firstNamelabel.setBounds(10, 40, 80, 25); // x,y,width,height
        panel.add(firstNamelabel);

        firstNameuserText = new JTextField(20);
        firstNameuserText.setBounds(100, 40, 165, 25);
        panel.add(firstNameuserText);

        lastNamelabel = new JLabel("Last Name");
        lastNamelabel.setBounds(10, 65, 80, 25);
        panel.add(lastNamelabel);

        lastNameuserText = new JTextField(20);
        lastNameuserText.setBounds(100, 65, 165, 25);
        panel.add(lastNameuserText);

        fromLabel = new JLabel("From");
        fromLabel.setBounds(10, 110, 80, 25);
        panel.add(fromLabel);

        fromCombo = new JComboBox(sItem1);
        fromCombo.setBounds(50, 110, 125, 25);
        panel.add(fromCombo);

        toLabel = new JLabel("To");
        toLabel.setBounds(150, 110, 80, 25);
        panel.add(toLabel);

        toCombo = new JComboBox(sItem2);
        toCombo.setBounds(175, 110, 125, 25);
        panel.add(toCombo);

        adultLabel = new JLabel("Adult(12+)");
        adultLabel.setBounds(10, 150, 80, 25);
        panel.add(adultLabel);

        adultCombo = new JComboBox(item4);
        adultCombo.setBounds(10, 175, 80, 25);
        panel.add(adultCombo);

        childrenLabel = new JLabel("Children(2-11)");
        childrenLabel.setBounds(150, 150, 100, 25);
        panel.add(childrenLabel);

        childrenCombo = new JComboBox(item5);
        childrenCombo.setBounds(150, 175, 80, 25);
        panel.add(childrenCombo);

        bookingDate = new JLabel("Booking Date");
        bookingDate.setBounds(10, 225, 100, 25);
        panel.add(bookingDate);

        bookingDateText = new JTextField(20);
        bookingDateText.setBounds(100, 225, 140, 25);
        panel.add(bookingDateText);

        LDate = new JLabel("(MM/DD/YYYY)");
        LDate.setBounds(250, 225, 300, 25);
        LDate.setForeground(Color.red);
        panel.add(LDate);

        classLabel = new JLabel("Class");
        classLabel.setBounds(10, 275, 80, 25);
        panel.add(classLabel);

        classCombo = new JComboBox(sItem3);
        classCombo.setBounds(60, 275, 125, 25);
        panel.add(classCombo);

        button = new JButton("Make Reservation");
        button.setBounds(120, 325, 175, 25);
        button.addActionListener(new GUI());
        panel.add(button);

        panel.setBackground(Color.lightGray);

        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String FirstName = firstNameuserText.getText();
        String LastName = lastNameuserText.getText();

    }

}

Answer

Probably you should first create a class to hold the information get from the user inputs (TicketRequest.class) and handle an object with the data to the new GUI.

Code idea:

 button.addActionListener(e -> {
            TicketRequest tr = new TicketRequest();
            tr.setName(firstNameuserText.getText());
            // ...
            GUInew guiNew = new GUInew(tr);
            guiNew.setVisible(true);
            
        
        });

The GUInew class may extend a JFrame.