Java: How to start a standalone application from the current when both are in the same package?

It seems like this should be easy, so I should miss something obvious: I have 4 standalone applications in one package, us.glenedwards.myPackage,

  • myClass1 extends the application
  • myClass2 extends the application

etc...

I need each class to act as its own standalone application. However, I want to be able to run the other 3 classes from the one I'm in by clicking on the link. Android allows me to do this with intent:

Intent intent = new Intent(this, EditData.class);
overridePendingTransition(R.layout.edit_data_scrollview, R.layout.state);
startActivity(intent);

I tried to run myClass2 from myClass1 using

myClass2.launch("");

: " ". , "extends application", start() myClass2, , myClass2 .

myClass2, myClass3 myClass4 myClass1, 4 ?

+1
3

, start(...) Application, start(...) . ( : , start Application, , - .)

start main Java. , main, () , .

, , :

public class FirstModule {

    // can be any Parent subclass:
    private BorderPane view ;

    public FirstModule() {

        // create view; you could also just load some FXML if you use FXML
        view = new BorderPane();

        // configure view, populate with controls, etc...

    }

    public Parent getView() {
        return view ;
    }

    // other methods as needed...
}

, ,

public class SecondModule {

    private GridPane view ;

    public SecondModule {

        view = new GridPane();
        // etc etc
    }

    public Parent getView() {
        return view ;
    }
}

-

FirstModule firstModule = new FirstModule();
Scene scene = new Scene(firstModule.getView());
Stage stage = new Stage();
stage.setScene(scene);
stage.show();

. , :

public class FirstApplication extends Application {

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

:

public class CompositeModule {

    private HBox view ;

    public CompositeModule() {

        Button first = new Button("First Module");
        first.setOnAction(e -> {
            Parent view = new FirstModule().getView();
            Scene scene = new Scene(view);
            Stage stage = new Stage();
            stage.initOwner(first.getScene().getWindow());
            stage.setScene(scene);
            stage.show();
        });

        Button second = new Button("Second Module");
        second.setOnAction(e -> {
            Parent view = new SecondModule().getView();
            Scene scene = new Scene(view);
            Stage stage = new Stage();
            stage.initOwner(second.getScene().getWindow());
            stage.setScene(scene);
            stage.show();
        });

        HBox view = new HBox(10, first, second);
        view.setAlignment(Pos.CENTER);

    }

    public Parent getView() {
        return view ;
    }
}

public class CompositeApplication extends Application {
    @Override
    public void start(Stage primaryStage) {

        Scene scene = new Scene(new CompositeModule().getView(), 360, 150);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

, Application . , - JVM, , . , .

+4

?

Runtime.getRuntime().exec("myClass1 [args]"); //put all args as you used in command

, / .

0

I was right; that was not easy. This is what I get to write code for 4 hours of sleep:

myClass2 class2 = new myClass2(); 
try { 
 class2.start(stage); 
} catch (Exception e) { e.printStackTrace(); } }
0
source

All Articles