1

I have my GUI set up so that if the button b1 is pressed:

 public class CubeCalc  {

 static int next = 0;

 public static void MakeTitlePage()
 {
  final JFrame window = new JFrame("Cubic Feet Calculator"); //Creates Frame

  JButton b1 = new JButton("Start");
  b1.setBackground(Color.decode("#5A20DF"));
     b1.setForeground(Color.WHITE);
     /*b1.setLayout(new GridBagLayout());*/
     b1.setPreferredSize(new Dimension(150,50));
  b1.addActionListener(new ActionListener() { // action when button is pressed
            int pressCount=0;
            @Override
            public void actionPerformed(ActionEvent e) {

                window.dispose();
               next = 1;


            }
        });

then it will dispose of the title page and and next will equal one, and on the Event Dispatch Thread, it creates a new page that does other things:

public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() { // launch frame on the Event Dispatch Thread
            @Override
            public void run() {
                 MakeTitlePage();

                 if (next==1)
                 {
                   MakeCalcPage();
                 }

                 System.out.println(next);
            }
        });
 }

The problem is that the variable next remains equal to zero even though I have changed it in the method MakeTitlePage(). How do I change the variable across all the methods, and not just that one?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 2
    First of all, `static` is a really bad idea, instead you should have some kind of model which determines you current location and the next step you want to move to. You would then have a controller which is monitoring for the required events from the UI so it knows when it should ask the model for the next step and present it. Have a look at [Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) for more details – MadProgrammer Feb 29 '16 at 02:20
  • The core issue you have is `JFrame` is non blocking, that is, once it's displayed, the code will continue to run (and hence, `next` is `0`) you could use a [modal dialog](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) instead, but I'd, personally, have some way for the frame to generate an event back to the controller and allow it to make the required decisions – MadProgrammer Feb 29 '16 at 02:21
  • For a conceptual example of all that, have a look at [this example](http://stackoverflow.com/questions/31602113/listener-placement-adhering-to-the-traditional-non-mediator-mvc-pattern/31604919#31604919) – MadProgrammer Feb 29 '16 at 02:23

1 Answers1

0

I think you might have misunderstood how the event dispatch thread works. When you add a listener to a component then you are telling Swing to listen for certain events and invoke the associated listener on the event dispatch thread. If you are using the static variable next to communicate between threads then, firstly, that's not the way to do it and, secondly, you are communicating to the same thread anyway.

If you want the button to close the current window and open a new one then you should do that directly in the actionPerformed method:

public void actionPerformed(ActionEvent e) {
    window.setVisible(false);
    showCalculationFrame();
}
sprinter
  • 27,148
  • 6
  • 47
  • 78