0

I'm using IntelliJ IDEA, and I have created two GUI forms I want to use as screens for my application. However I am stuck on how to get my separate main-class to incorporate the two JFrames, and how to control the switch from one JFrame to another. In the code below in the main-class, I'm first initializing an object of both GUI forms, afterwards I'm running the setup-frames 'run' method, which sets the frame visible. This step works.

Now I want an actionListener on my button 'udførButton' which gets the values typed in it's TextFields, so that I can use them as parameters to initialize an instance of the 'Billetautomat' class. Furthermore, I want the button to close the 'setup' JFrame and to start up the 'gui' JFrame with the gui.run() method.

My intended flow in the program is:

  1. Run 'setup' gui, where I can type in three names and three ints
  2. Run 'gui' (not done designing yet), where I can press multiple buttons to trigger different actions
  3. Run other JFrames for non-specified actions and return to 'gui' (step 2)

Problem is, that the actionListener won't work as I want it to... If I change the code inside the actionListener to billet1Label.setText("HELLO"); it indeed changes the Label's text to display HELLO, but I can't make it work as I intend... I think the problem is that the program doesn't stop and wait for my button click, but just races through the main code... Do I need some sort of check to see if the button has been pressed, or have you got any expert-advise? This is my first time working with Swing in Java, so please be patient with me...

The final and the [] on the variables was suggested by IntelliJ....

BenytBilletautomat_GUI.java (main-class)

package automat;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class BenytBilletautomat_GUI {

public static void main(String[] args) {

    final int[] pris1 = new int[1];
    final int[] pris2 = new int[1];
    final int[] pris3 = new int[1];
    final String[] navn1 = new String[1];
    final String[] navn2 = new String[1];
    final String[] navn3 = new String[1];

    Setup_GUI setup = new Setup_GUI();
    Billetautomat_GUI gui = new Billetautomat_GUI();

    setup.udførButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            navn1[0] = setup.getBillet1Text();
            pris1[0] = setup.getBilletPris1();
            navn2[0] = setup.getBillet1Text();
            pris2[0] = setup.getBilletPris1();
            navn3[0] = setup.getBillet1Text();
            pris3[0] = setup.getBilletPris1();
            Billetautomat automat = new Billetautomat(navn1[0],pris1[0],navn2[0],pris2[0],navn3[0],pris3[0]);
            setup.frame.setVisible(false);
            setup.frame.dispose();
            gui.run();
        }
    });

    setup.run();
    }
}

Setup_GUI.java (setup gui class)

package automat;
import javax.swing.*;

public class Setup_GUI {

    private JPanel mainPanel;
    private JLabel gui_title;
    private JTextField billet1Text;
    private JTextField billet2Text;
    private JTextField billet3Text;
    private JLabel billet1Label;
    private JLabel billet2Label;
    private JLabel billet3Label;
    private JLabel setupLabel;
    private JTextField billetPris1;
    private JTextField billetPris2;
    private JTextField billetPris3;

    public JButton udførButton;

    JFrame frame = new JFrame("Setup");

    public void run() {
        frame.setContentPane(new Setup_GUI().mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }



    public String getBillet1Text() {
        return billet1Text.toString();
    }

    public String getBillet2Text() {
        return billet2Text.toString();
    }

    public String getBillet3Text() {
        return billet3Text.toString();
    }

    public int getBilletPris1() {
        return Integer.parseInt(billetPris1.toString());
    }

    public int getBilletPris2() {
        return Integer.parseInt(billetPris2.toString());
    }

    public int getBilletPris3() {
        return Integer.parseInt(billetPris3.toString());
    }
}

Billetautomat_GUI.java (gui class, not done yet)

package automat;
import javax.swing.*;
import java.awt.event.ComponentAdapter;

public class Billetautomat_GUI {

    JFrame frame = new JFrame("Billetautomat_GUI");
    private JPanel mainPanel;

    public void run() {
        frame.setContentPane(new Billetautomat_GUI().mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Mar 12 '17 at 11:18
  • Have already read it, but regardless of what I'm trying to do is a good or bad idea, it doesn't give a way to create a programflow with multiple JFrames... I'm sure it can be done, and it might be better with CardLayout, but I'm still interested in finding out how this 'bad practice of having multiple JFrames' is done :-) –  Mar 12 '17 at 11:27
  • 1
    Instead of multiple frames, use _modeless_ dialogs. I don't know IntelliJ, but you may be able to adapt this [approach](http://stackoverflow.com/a/2561540/230513) used with NetBeans GUI editor. – trashgod Mar 12 '17 at 12:01
  • 1
    @trashgod *"Instead of multiple frames, use modeless dialogs."* Great idea, and one of the many offered in my answer to the "multiple frames are bad ..mmkay?" thread. OP: There are so many better solutions to this conundrum, that few will be motivated to try and make a poor approach work. – Andrew Thompson Mar 12 '17 at 12:05
  • 1
    @AndrewThompson is correct. LucasS should not let the GUI editor dictate the program's design, either visually or internally. – trashgod Mar 12 '17 at 12:24
  • Than you for your advice! I've researched some more, and will continue to do so. Haven't gotten it working yet, but hopefully it will in the coming week :-) I'm considering trying out some CardLayout instead, so I'll try to find a tutorial of some sort... –  Mar 13 '17 at 16:09
  • Tip: Add @trashgod (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Mar 13 '17 at 22:57

0 Answers0