Rinat Hatipov Rinat Hatipov - 4 months ago 17
Java Question

Information from first form to second form swing java

I have 2 forms. In the first form, I create a class object suggestion, then I have to make some action with it and submit it to the second form. How can I open a second window after the filling of the object in the first?

It is my first form

public class FormNum1 extends JFrame {
JButton clearConfermation, conferm;
JLabel dateofDeparture, dateofArrival, cityFrom, cityTo;
JTextField dateofDepartureTextField, dateofArrivalTextField, cityFromTextField, cityToTextField;
static Suggestion suggestion;
Boolean n = false;
JFrame form1 = new JFrame("form1");
ActionListener actionPerformed;
public FormNum1(){
form1.setVisible(true);
form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form1.setSize(1200,250);
//setResizable(false);
form1.setLocationRelativeTo(null);
form1.setLayout(new FlowLayout());
clearConfermation = new JButton("Make clear");
conferm = new JButton("Confirm information ");
dateofDeparture = new JLabel("Date of Departure");
dateofArrival = new JLabel("Date of Arrival");
cityFrom = new JLabel("City From");
cityTo = new JLabel("City to");
dateofDepartureTextField = new JTextField(10);
dateofArrivalTextField = new JTextField(10);
cityFromTextField = new JTextField(10);
cityToTextField = new JTextField(10);
form1.add(clearConfermation);
form1.add(conferm);
form1.add(dateofDeparture);
form1. add(dateofDepartureTextField);
form1.add(dateofArrival);
form1.add(dateofArrivalTextField);
form1.add(cityFrom);
form1.add(cityFromTextField);
form1.add(cityTo);
form1.add(cityToTextField);
actionPerformed = new InformationFromFormOne();
conferm.addActionListener(actionPerformed);
clearConfermation.addActionListener(actionPerformed);
}


class InformationFromFormOne implements ActionListener{
public void actionPerformed(ActionEvent e) {

if (e.getSource() == conferm){
String dateOfDeparture = (dateofDepartureTextField.getText());
String dateOfArrival = (dateofArrivalTextField.getText());
String cityFrom = (cityFromTextField.getText());
String cityTo = (cityToTextField.getText());
suggestion = new Suggestion(dateOfDeparture, dateOfArrival, true, cityFrom, cityTo);

setVisible(false);
form1.dispose();
}

if (e.getSource() == clearConfermation){
dateofArrivalTextField.setText(null);
dateofDepartureTextField.setText(null);
cityFromTextField.setText(null);
cityToTextField.setText(null);
}
}
}


}

My second form is

public class FormNum2 extends JFrame{
static JFrame form2 = new JFrame("form2");

public FormNum2() {
form2.setSize(1200,600);
//frame.setResizable(false);
form2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form2.setLocationRelativeTo(null);
form2.setVisible(true);
}


}

Main class

public class Main extends Thread{
public static void main(String[] args) {
FormNum1 form1 = new FormNum1();
FormNum2 form2 = new FormNum2();
}


}

Answer

After the first frame processing set the second frame to visible, put FormNum2 .setVisible(true); to show the second form.

Comments