0

As part of my efforts to familiarize myself with the MVC model, I am attempting to write a program that loops through a set of code calculations while occasionally pausing to prompt the user for input with custom windows created in JavaFX, using that input to continue the calculations until a certain result is reached to break the loop. The idea is that for each window, the user selects an option, the window closes, and then the logic continues until the next user input is needed. Then the next window opens and the process continues. Based on what the user inputs, the choices included in the next window can change. For my starting example, the window has four buttons with the numbers 1-4 and prompts the user to choose a number. I boiled down my code to something really basic:

public class LogicLoop()
{

public static AtomicInteger CHOICE; //Global variable to act as the user's current choice, as there is only ever one choice made at a time
public static boolean loopDone = false;

public static void main(String [] args)
{
    new Start().startLoop(); //Calls the class that extends Application to call the launch() command. Since launch() can only be called once, it needs to be outside the loop 
    logicLoop();
}

public void logicLoop()
{
    while(!loopDone)
    {
        Chooser chooser = new Chooser();
        chooser.show();
        int choice = CHOICE.get();
        CHOICE = null;
        doSomething(choice);
    }

    public doSomething(int choice)
    {
        //Set some other variables to modify how the next window will look
        //Potentially set loopDone to true;
    }

}

}

And then my Chooser class:

public class Chooser()
{
    public int show()
    {
        Stage newStage = new Stage();
        createWindow(newStage); //method that sets up window based on global variables and calls show() on newStage. Each button in the window will close the stage. The CHOICE value will also be set based on the user's input
    }
}

The window created by createWindow() has buttons with the following handle() method:

public void handle(ActionEvent event) {
            CHOICE = new AtomicInteger(btn.getValue()); //I made my own Button class that has a value variable and a getter() for it
            Stage stage = (Stage) btn.getScene().getWindow();
            stage.close();
}

The issue I'm facing is that when my code goes to execute, the logic doesn't wait for the user's input before continuing right past the show() method. So what happens is the code starts the loop, and immediately brings up the first window. At the same time, a NullPointerException is thrown because my logic is already trying to call CHOICE.get() before the user even has time to input any value. So after choosing a button and closing the first window, nothing else happens.

So far I've tried making use of the showAndWait() method in my createWindow() method, but it doesn't appear to make any difference. I've tried calling the wait() command after chooser.show(), but it just adds a delay and then I see the same results. Based on information I've been able to find on Stack Overflow, I've seen a lot of recommendation to use the Platform.runLater() method, but I can't see how it would be effective here. If the GUI runs later, the logic will still just stampede ahead without it. I need the logic loop to pause until the window closes, which seems to be what showAndWait() is designed to do. What confuses me the most is a statement I came across while researching an answer that said that implementing a basic MVC framework does not require multithreading and that it is not advisable for a beginner to make use of it. At this stage, I'm not certain how that's possible.

What am I missing here?

MagerBlutooth
  • 23
  • 1
  • 3
  • 1
    get acquainted with the multithreaded programming in java. when it comes to gui, a single thread is usually used to handle the user interface. in another thread the calculations are exported. the process of waiting data from one thread to another is called synchronization. java has many libraries that facilitate multi-threaded programming, but it's still advisable to know how things really work – mr mcwolf Sep 24 '19 at 17:00
  • 1
    Given that you describe a process where you show a window, get user input, close the window, then show the next window, get more user input, and so on, using `showAndWait()` seems like it should work perfectly. If it doesn't, please [edit] your question to add a [mre] demonstrating the issue—see [ask]. Also, look into [event-driven programing](https://en.wikipedia.org/wiki/Event-driven_programming) and the [state pattern](https://en.wikipedia.org/wiki/State_pattern). – Slaw Sep 25 '19 at 05:34
  • I didn't read your whole question, but maybe it is similar to [Can I pause a background Task / Service?](https://stackoverflow.com/questions/14941084/javafx2-can-i-pause-a-background-task-service) which defines a [prompting task demo](https://gist.github.com/jewelsea/4989970). – jewelsea Sep 26 '19 at 08:15

0 Answers0