0

I am opening a html page on event in JavaFx Webview and also have a recursive call to same method which also closes the opened webview on another even. In each recursive call I have put a gap of 10 seconds.

private static void checkForAPIStatus(String[] args) {
        System.out.println("checkForAPIStatus method started....");
        if (RandomStringFromArray().equals("Unsatisfactory")) {
            if (jframe == false) {
                if (checkForTransactionStatus().equals("END TICKET ")) {
                    BringUpFrame.showBanner(args);
                    jframe = true;
                }
            }
        } else {
            if (jframe == true) {
                 BringUpFrame.disposeBanner();
            }
            jframe = false;

        }
        System.out.println(jframe);
        try {
            System.out.println("Before sleep");
            Thread.sleep(TimeInterval);
            System.out.println("After sleep");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        checkForAPIStatus(args);

    }

public static String RandomStringFromArray() {
    String[] arr = {"Unsatisfactory", "Ok"};
    Random r = new Random();
    int randomNumber = r.nextInt(arr.length);
    System.out.println(arr[randomNumber]);
    return arr[randomNumber];
}

Issue I am facing here is that initially when jframe is false and code goes through else block, it successfully executes sleep call and execute the recursive call but when showBanner method executes inside if condition which calls the below code.After that My Thread.sleep call does not return and application gets stucked there.

public class BringUpFrame extends Application{

    static Logger logger = LoggerFactory.getLogger(BringUpFrame.class);
    private static Stage stage;

    @Override
    public void start(Stage primaryStage) throws Exception {
        int width = 0;
        int height = 0;
        String ScrnResol = getScreenResolution();
        String[] ScreenResolution = ScrnResol.split(",");

        try {
            width = Integer.parseInt(ScreenResolution[0]);
            height = Integer.parseInt(ScreenResolution[1]);

        } catch (NumberFormatException nfe) {
            logger.info("NumberFormatException: " + nfe.getMessage());
        }
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        URL resource = getClass().getClassLoader().getResource("test.htm");
        System.out.println("Resource before load "+resource);
        webEngine.load( resource.toString() );
        Scene scene = new Scene(webView,width,height);
        primaryStage.setScene(scene);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.setFullScreen(true);
        primaryStage.setFullScreenExitHint("");
        primaryStage.show();
        stage = primaryStage;
    }


    public static void showBanner(String[] args) throws IOException {
        launch(args);
    }

    public static void disposeBanner() {
        stage.close();
    }

    public static String getScreenResolution() {

        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int width = gd.getDisplayMode().getWidth();
        int height = gd.getDisplayMode().getHeight();

        String resolution = width + "," + height;
        logger.info("Till Resolution is (width,height) : " + resolution);
        return resolution;

    }
}

Can anyone pointout is there's anything wrong in the above code ?

Thanks,

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Manish
  • 1,274
  • 3
  • 22
  • 59
  • 2
    Does this answer your question? [Why Thread.sleep() doesn't work accordingly in JavaFX?](https://stackoverflow.com/q/54229373/5221149) – Andreas Oct 20 '20 at 15:52
  • @Andreas here i am trying to pause main thread not javafx window. – Manish Oct 20 '20 at 17:08
  • Really? I'm sorry, but I can't see anywhere in the question what shows which thread is calling the `checkForAPIStatus()` method. --- Besides, the main thread *is* the JavaFX event dispatching thread, so I'm not sure what your point is. – Andreas Oct 20 '20 at 17:56
  • CheckApiStatus is calling from main method. You are right that main thread is JavaFX Event dispatching thread which calls BringupFrame JavaFx Element.After that main continues and take a sleep of 10 seconds and recuraively calls checkForApiStatus.My issue is once javaFxElement is triggered main thread is not going forward. Sleep I have put for the main thread if you see the code. – Manish Oct 20 '20 at 18:03
  • Do you 100% have to mix frameworks? Unless it's 100% necessary, you probably should not be doing it. Doing so makes applications more complicated. Also, your code structure and the use of `static` says that you don't truly understand the basic ins-and-outs of `JavaFX`. I would suggest avoid using static and doing some tutorials so that you have a better understanding of the `JavaFX` framework. – SedJ601 Oct 20 '20 at 19:03
  • @Sedrick you are right. This is the first time i am using javafx. Can you please point out some good reference. I would appreciate if you can also pointout why application is not working as expected ? – Manish Oct 21 '20 at 04:15
  • Your issue is likely due to `Thread.sleep`. My guess is that you need to use something from the `Animation` API. Have you tried [`PauseTransition`](https://stackoverflow.com/questions/30543619/how-to-use-pausetransition-method-in-javafx)? That's my guess. – SedJ601 Oct 21 '20 at 04:58
  • @Sedrick but Thread.sleep is in my main thread it's not in javaFx Component. JavaFx componenet is BringupFrame Class that I posted. – Manish Oct 21 '20 at 06:12
  • I am not sure what you are trying to say in your comment, but using `Thread.sleep` on the `ApplicationThread` is always wrong. – SedJ601 Oct 21 '20 at 14:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/223411/discussion-between-manish-and-sedrick). – Manish Oct 21 '20 at 14:11

0 Answers0