0

I'm very aware this has been asked countless times before but I cannot seem to get my head around this. The overall program is centered on forex and using live data so it needs to update every 20 seconds or so.

I have made a UI which has 2 pages. The first page has a "next page" button that obviously goes to page 2 (this is where the live data is going). I have made a separate class (TestController) with a for loop that sleeps for 20 seconds, for testing the iteration is just set to one.)

public class testController {

    // Sleep for 20 seconds
    static void sleep() throws InterruptedException {

        for (int i = 0; i < 1; i++) {

            Thread.sleep(20000);
        }
        testModel.theModel();

        testView.createSecondPage(null);

This is called from my TestView class, simplified to this:

public class testView extends Application {

    Button button;

    public testView() {}

        public static void launchApplication(String[]args) {
        launch(args);
    }

        @Override
        public void start(Stage stage) throws Exception {

            button = new Button();
            button.setText("Next Page");
…

            Scene scene = new Scene(grid, 1075, 700);
            stage.setScene(scene);
            stage.show();

            // change code here 

            button.setOnAction(e -> {
                try {
                    createSecondPage(stage);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            });
                }
}

and this:

public static void createSecondPage(Stage stage) throws InterruptedException {

testController.sleep();

}

I have removed all content that will be displayed on the UI as there is a lot of it and it is irrelevant to the question. And this is the question: I need to pause/stop/hold the first page until the 'next page' button has been clicked. The code I have currently, badly, written only allows me to click once the first iteration has been completed. So I need something that would say 'once the first page is displayed, wait until the user clicks the next button, then display the second page, then run the sleep thread'. I've looked at wait(), notify() but cannot get understand it; a while loop is considered bad code & an if statement doesn't help as I don't know how to get the software to pause anyway.

It may be simple, but not for me. Thanks in advance.

SteveS
  • 69
  • 7
  • 1
    unrelated: java naming conventions, please! and read up on concurrency in javafx: basically, you need to do the sleeping on a background thread (potentially collecting data as appropriate), the push the collected date into the ui on the fx application thread. Fx has extended support for such a task. – kleopatra Mar 23 '20 at 14:06
  • 2
    See https://stackoverflow.com/questions/30249493/using-threads-to-make-database-requests/30250308#30250308. Also, a [`ScheduledService`](https://openjfx.io/javadoc/13/javafx.graphics/javafx/concurrent/ScheduledService.html) may be useful for periodically retrieving data in a background thread. – James_D Mar 23 '20 at 14:15
  • @James_D, thanks **ScheduledService** is what I needed but had never heard of. – SteveS Mar 24 '20 at 12:45

0 Answers0