Fredrik Sander
Fredrik Sander

Reputation: 31

Annoying issue with WindowBuilder

I've recently started programming with the WindowBuilder for Java and I've ran into a very annoying issue which I can't seem to resolve.

The problem is that the debug (System.out.println(answer); at line 133) gets executed before the YesOrNo method finished (Line 131) completes. So it runs "ahead" of my program.

My code:

main.java

package me.fractional.main;

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

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

public class main {

GUI go = new GUI();
Studio so = new Studio();

private JFrame frmFractionalc;

static JLabel logo = new JLabel("");
static JButton InputNext = new JButton("Continue");
static JLabel OutputMainText = new JLabel("");
static JTextField InputText = new JTextField();
static JButton InputYes = new JButton("Yes");
static JButton InputNo = new JButton("No");

public static int counter = 1;
public int answer = 2;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                main window = new main();
                window.frmFractionalc.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public main() {
    initialize();
    intro();
}

public void Tutorial()
{
    System.out.println("Tutorial");
    OutputMainText.setVisible(true);
    go.OutputMainText("Welcome to! Click Next Turn to Continue.");
}
public void MainMenu()
{
    System.out.println("MainMenu");
}

private void initialize() {
    frmFractionalc = new JFrame();
    frmFractionalc.getContentPane().setBackground(new Color(176, 224, 230));
    frmFractionalc.setBackground(new Color(0, 128, 128));
    frmFractionalc.setTitle("Fractional © - GameDevStory");
    frmFractionalc.setBounds(100, 100, 500, 300);
    frmFractionalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmFractionalc.getContentPane().setLayout(null);

    logo.setHorizontalAlignment(SwingConstants.CENTER);
    logo.setBounds(42, 44, 400, 150);

    frmFractionalc.getContentPane().add(logo);
    frmFractionalc.getContentPane().add(InputNext);
    OutputMainText.setHorizontalAlignment(SwingConstants.CENTER);
    OutputMainText.setBounds(10, 69, 464, 105);

    frmFractionalc.getContentPane().add(OutputMainText);
    frmFractionalc.getContentPane().add(InputText);

    frmFractionalc.getContentPane().add(InputYes);

    frmFractionalc.getContentPane().add(InputNo);



}

public void intro()
{

    logo.setIcon(new ImageIcon(main.class.getResource("/me/fractional/main/logo.png")));
    InputNext.setForeground(new Color(0, 0, 0));
    InputNext.setBounds(200, 239, 89, 23);  

    InputNext.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            switch(counter)
            {
                case 1:
                    logo.setVisible(false);
                    go.OutputMainText("Welcome to GameDevStudio!");
                    counter = 2;
                    break;
                case 2:
                    go.OutputMainText("Enter a name for your game studio below [10 characters max!]");
                    InputText.setBounds(162, 174, 154, 20);
                    InputText.setColumns(10);
                    InputText.setVisible(true);
                    counter = 3;
                    break;
                case 3:
                    so.setName(InputText.getText());
                    if(so.getName().length() < 1 || so.getName() == null)
                    {
                        go.OutputMainText("Invalid name!");
                        InputText.setVisible(false);
                        counter = 2;
                    }
                    else
                    {
                        go.OutputMainText(so.getName() + ", that's a name I'd use!");
                        InputText.setVisible(false);
                        counter = 4;
                    }
                    break;
                case 4:
                    InputNext.setVisible(false);
                    go.OutputMainText("Would you like to run the tutorial?");

                    answer = go.YesOrNo(1);

                    System.out.println(answer);

                    break;
            }
        }
    });
}

}

GUI.java

package me.fractional.main;

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

import me.fractional.main.main;

public class GUI {

static int returnvalue;

public void OutputMainText(String text)
{
    main.OutputMainText.setText(text);
    main.OutputMainText.setVisible(true);
}
public int YesOrNo(final int option)
{

    main.InputYes.setBounds(112, 239, 89, 23);
    main.InputNo.setBounds(287, 239, 89, 23);


    main.InputYes.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            returnvalue = 1;
            main.InputYes.setVisible(false);
            main.InputNo.setVisible(false);

            if(option == 1)
            {
                main.OutputMainText.setText("");
            }
        }
    });

    main.InputNo.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            returnvalue = 0;
            main.InputYes.setVisible(false);
            main.InputNo.setVisible(false);

            if(option == 1)
            {
                main.OutputMainText.setText("");
            }
        }
    });

    return returnvalue;

}

}

Studio.java

package me.fractional.main;

public class Studio {

String s_name;

public String getName()
{
    return s_name;
}
public void setName(String name)
{
    s_name = name;
}
}

Upvotes: 2

Views: 230

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

Swing is an event driven environment, this means that you program produces input for the user, which they can interact with as they please, in any sequence they please.

Instead of a linear input process like a console program, you are notified of the user interactions via events.

This means that your YesOrNo methos will return before any of the actionPerformed methods are called.

If you need to stop the application flow for some reason, you only choice is to use a dialog of some kind.

Take a look at How to use dialogs for more details

Ps- while you're there, you might like to check out How to use layouts, they will make yr life easier in the long wrong

Upvotes: 1

Related Questions