2

How do I go about making a thread within JavaFX? I've looked around and no answers are clear/want I need, which is basicly the same as Java's Thread Runnable, which is not conpatible with JavaFx unless it's for a background task.

The basic start application class I have:

public class Main extends Application {

    private Stage stage;
    private AnchorPane rootLayout;

    @Override
    public void start(Stage stage) {
        this.stage = stage;
        this.stage.setTitle("Main");
        setLayout();
    }

    private void setLayout() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/View.fxml"));
            rootLayout = (AnchorPane) loader.load();

            Scene scene = new Scene(rootLayout);
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

I need a JavaFX Thread that'll keep changing the visibility of a button without freezing/pausing the application while it's running, how do I go about doing this?

Edit: I need this but for a JavaFX app:

public void run(){
                while (true){
                    if (button.isVisible)
                    button.setVisibility(false);
                    else
                        button.setVisibility(true);
                        Thread.sleep(1000);
                }
            }
FOD
  • 333
  • 6
  • 16
  • Please go through [this thread](http://stackoverflow.com/questions/29252167/update-javafx-scene-graph-from-a-thread/29255212#29255212) for handling UI components from another thread and then [this thread](http://stackoverflow.com/questions/28627585/new-thread-application-still-running-after-stage-close/28629484#28629484) for performing a function after each scheduled period of time. – ItachiUchiha Apr 10 '15 at 00:07
  • Try to avoid `while(true)` inside a Thread – ItachiUchiha Apr 10 '15 at 00:11
  • I advise using a [Timeline](http://docs.oracle.com/javase/8/javafx/api/javafx/animation/Timeline.html) to toggle the visibility of your button on and off - you need neither a separate thread nor a loop. With a timeline, your UI will not be frozen and you won't have to deal with potential concurrency issues such as trying to modify the scene graph off of the JavaFX application thread. – jewelsea Apr 10 '15 at 09:36

0 Answers0