Letfar
Letfar

Reputation: 3503

Few JavaFx windows

How to run few javaFx windows if they are in separeted classes?

For example, like in this case:

public class Main {
    public static void main(String[] args) {
        Form1 form1 = new Form1();
        Form2 form2 = new Form2();

        // run form1
        // run form2
    }

    public static class Form1 extends Application {
        @Override
        public void start(Stage primaryStage) throws Exception {
            Stage stage = new Stage();
            stage.setScene(new Scene(new Group(new Button("Window 1"))));
            stage.show();
        }
    }

    public static class Form2 extends Application {
        @Override
        public void start(Stage primaryStage) throws Exception {
            Stage stage = new Stage();
            stage.setScene(new Scene(new Group(new Button("Window 2"))));
            stage.show();
        }
    }
}

I only need to show two windows in the same time, but can't find any simple example.

Upvotes: 0

Views: 93

Answers (1)

Collins Abitekaniza
Collins Abitekaniza

Reputation: 4578

In javaFx window are considered to be stages so t create multiple windows,you can consider using the following code as an example

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    form1().show();
    form2().show();
}


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

private Stage form1(){
    Stage stage=new Stage();
    stage.setTitle("Window 1");
    stage.setScene(new Scene(new Group(new Button("Window 1"))));
    return stage;
}
private Stage form2(){
       Stage stage=new Stage();
       stage.setTitle("Window 2");
       stage.setScene(new Scene(new Group(new Button("Window 2"))));
       return stage;
   }



}

Upvotes: 2

Related Questions