Separate thread for GUI

Should I create separate thread for GUI or it will be created automatically? And If I should how I can do this? I don't understand how I can run GUI.

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MyMain extends Application implements  Runnable
{
@Override
public void run()
{

}

@Override
public void start(Stage primaryStage) throws Exception
{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 200, 300));
    primaryStage.setMinWidth(220);
    primaryStage.setMinHeight(340);
    primaryStage.show();
}

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

Upvotes: 0

Views: 192

Answers (1)

Papershine
Papershine

Reputation: 5223

You do not have to create a new thread. Just use this code:

public class MyMain extends Application {

    @Override
    public void start(Stage primaryStage) {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 200, 300));
        primaryStage.setMinWidth(220);
        primaryStage.setMinHeight(340);
        primaryStage.show();
    }

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

The Application class takes care of the threading by itself.

Upvotes: 1

Related Questions