0
public static TextArea message_text=new TextArea();

final static String message_text_style="-fx-border-width: 5px;-fx-border-radius: 10px;-fx-border-style: solid;-fx-border-color: #ff7f7f;";

private static int timer;
public static void system_message(String what,int set_timer)
{

    timer=set_timer;

    message_text.setText(what);
    message_text.setStyle("-fx-opacity: 1;"+message_text_style);

    Platform.runLater(new Runnable()
            {

                public void run()
                {

                    try
                    {
                        Thread.sleep(timer);
                    }
                    catch(InterruptedException ex)
                    {

                    }

                    message_text.setStyle("-fx-opacity: 0;"+message_text_style);

                }

            });

}

This code is aimed at a message pop up appearing and then disappearing after a certain amount of time ( so that system events can be communicated to the user ). This is realized by setting the opacity of a TextArea element so that it becomes visible. The runLater block is responsible for switching the opacity back after a set amount of time.

This is an example where I invoke this method:

        Button save_as_pgn_button=new Button();
        save_as_pgn_button.setText("Save as: ");

        save_as_pgn_button.setOnAction(new EventHandler<ActionEvent>() {

            @Override public void handle(ActionEvent e) {

                String path=pgn_name_text.getText();

                if(path.length()>0)
                {

                    MyFile my_file=new MyFile(path);

                    calc_pgn();

                    my_file.content=pgn;

                    my_file.write_content();

                    Javachessgui.system_message("Saved to file: "+path+"\n\nContent: "+my_file.content,3000);

                }
            }

        });

The frightening thing is that it sometimes works, sometimes not. Mostly not, but sometimes when I press the button it works exactly as expected. Or if I run it in debug mode and toggle a breakpoint before system_message() and then step over it, it always works!

What is going on?

javachessgui
  • 147
  • 2
  • 11
  • You've misunderstood what `Platform.runLater(...)` does. It basically does nothing here, because you are already on the FX Application Thread. You should never block the FX Application Thread, which you do with `Thread.sleep(...)`. Use a `PauseTransition` to execute code on the FX Application Thread after a delay. – James_D Jun 06 '15 at 20:36
  • I added my solution as an answer: http://stackoverflow.com/questions/27334455/how-to-close-a-stage-after-a-certain-amount-of-time-javafx/30687638#30687638 – javachessgui Jun 06 '15 at 21:16
  • It just seems like that replaces the three lines in my solution with about 20 lines of code that does exactly the same thing. What am I missing? – James_D Jun 06 '15 at 22:29

0 Answers0