Finn Eggers
Finn Eggers

Reputation: 945

JavaFX - Object with Constructor

I'm working with JavaFX and my Idea was to have my own JavaFX Object which I can create like this:

public class Main {
    public static void main(String[] args) throws Exception 
        TSS t = new TSS();       
    }  
}

My JavaFX Main class looks like this:

public class TSS extends Application {

    private Scene scene;
    private Stage stage;

    public void redrawGui() throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("tss.fxml"));
        scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        this.stage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("tss.fxml"));
        scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    } 
}

Normally the main method in TSS is called and its working but I want to create my own TSS-Object in its constructor, it creates the Gui.

Does anyone know how to do this?

Upvotes: 1

Views: 3137

Answers (1)

James_D
James_D

Reputation: 209358

In JavaFX, you should (typically) think of the Application subclass as the "main" class (i.e. the application entry point) and its start(...) method as the replacement for the main(...) method in a "traditional" Java Application.

If you want to factor your code out into a class that is separate from the Application subclass (which is generally a good idea), then you can do so, but you need to just reorganize things a little:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        TSS t = new TSS();
        Scene scene = new Scene(t.getView());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    // not really needed in Java 8, but some IDEs need this to execute this class:
    public static void main(String[] args) { launch(args);}
}

And then you can define your own class as follows:

public class TSS {

    private Parent view ;
    private TssController controller ; // controller class specified in FXML

    public TSS() throws Exception {
        load();
    }

    private void load() throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("tss.fxml"));
        view = loader.load();
        controller = loader.getController();
    }

    public Parent getView() {
        return view ;
    }

    public void restartGui() throws Exception {
        Scene scene = view.getScene();
        Stage stage = null ;
        if (scene != null) {
            Window window = scene.getWindow();
            if (window instanceof Stage) {
                stage = (Stage) window ;
            }
        }

        load();
        if (stage != null) {
            stage.setScene(new Scene(view));
        }
    }

    public void doOtherStuff() {
        controller.doSomething();
    }
}

You could also consider implementing the TSS class above using the custom control pattern described in the FXML documentation. I marginally prefer the style I showed here, as it favors composition over inheritance, but it is a minimal difference.

Upvotes: 1

Related Questions