-1

I have 2 forms. In the first form, I create an object suggestion. Then I have to do something with it and submit it to the second form. How can I open a second window after filling out the first form?

This 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();
    }
}
ragingasiancoder
  • 616
  • 6
  • 17
  • "Confirm" is spelled as such, not as "conferm". Same goes for confirmation. Didn't want to change it in case it was used elsewhere – ragingasiancoder Jul 18 '16 at 22:55
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 18 '16 at 23:04
  • `form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` (sigh) You ***really*** should check the Java docs for methods used, rather than just 'flying blind' as it seems is currently happening.. – Andrew Thompson Jul 18 '16 at 23:07

2 Answers2

0

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

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
  • @AndrewThompson The Answer was accepted by OP so it helped him solve his problem, don't know why I got downvoted though. – Gherbi Hicham Jul 20 '16 at 16:35
  • Actually I didn't respond because OP was pretty fast in accepting answer, so didn't feel the need to respond after that, I used to have the same problem as OP so I answered immediately after reading the code. – Gherbi Hicham Jul 20 '16 at 16:52
-1

If you want to open the second window after the filling of the object in the first then you should call FormNum2 form2 = new FormNum2() after the object is filled. It seems like you would want to add this line after form1.dispose().

  • Did *you* try that? – Andrew Thompson Jul 18 '16 at 23:07
  • I didn't try it my self, but by looking at your code it should work. Did you try it? Did it fix your problem? Also @GherbiHicham's answer should also work. Just make sure to remove form2.setVisible(true); from the constructor in Form2. – monsterman Jul 20 '16 at 13:56
  • Think carefully about the effect of `form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` & `form2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` .. What will happen when ***either*** window is closed? *"by looking at your code it should work"* You seem to be under the impression you are replying to the person who asked the original question. I can be seen [here](http://stackoverflow.com/tags/swing/topusers) .. – Andrew Thompson Jul 20 '16 at 14:13