Reputation: 56
When I am running the below mentioned code it is working
import javafx.application.Application;
public class Client {
public static void main(String[] args){
Test t2 = new Test();
Application.launch(t2.getClass(),args);
}
}
where the test class is
package com.temp.com.serverclient;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("No Main");
StackPane root = new StackPane();
root.getChildren().add(new Label("It worked!"));
primaryStage.setScene(new Scene(root, 300, 120));
primaryStage.show();
}
}
But if I am trying to add constructor,it is getting Exception in Application constructor,Error. The code is
package com.temp.com.serverclient;
import javafx.application.Application;
public class Client {
public static void main(String[] args){
Test t1 = new Test("Pass this String to Constructor");
Application.launch(t1.getClass(),args);
}
}
Test class
package com.temp.com.serverclient;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application {
String str;
public Test(String str) {
this.str = str;
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("No Main");
StackPane root = new StackPane();
root.getChildren().add(new Label("It worked!"));
primaryStage.setScene(new Scene(root, 300, 120));
primaryStage.show();
}
}
How Can I sovle the problem? I need to pass the String to collect information from previous class.
Upvotes: 0
Views: 605
Reputation: 82531
Application.launch
always uses a public
parameterless constructor to create a instance of the application class to launch. (It does not provide any benefit to create a instance in the main method BTW. Simply pass the class without creating a instance, i.e. Application.launch(Test.class, args);
.)
In fact you can only pass String
parameters to the new instance of your application class without using static
members and it's done via the args
parameter of Application.launch
:
public class Client {
public static void main(String[] args) {
Application.launch(Test.class, "Pass this String to Constructor");
}
}
public class Test extends Application {
String str;
@Override
public init() {
this.str = getParameters().getRaw().get(0);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("No Main");
StackPane root = new StackPane();
root.getChildren().add(new Label("It worked!"));
primaryStage.setScene(new Scene(root, 300, 120));
primaryStage.show();
}
}
Note that accessing the parameters
property is also possible for the start method.
JavaFX 9 introduced a new possiblility: using Platform.startup
but you need to handle the lifecycle of the application class yourself:
Application app = new Test("Pass this String to Constructor");
app.init();
Platform.startup(() -> {
Stage stage = new Stage();
try {
app.start(stage);
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
});
This does not properly call the Application.stop
method though. Also the parameters are not assigned.
Upvotes: 1
Reputation: 3118
AFAIK, Application.launch
creates a new instance of Test
. Therefore, you need another way to get the value to the instance, e. g. using a static getter method in your Client
class which is called from Test
Upvotes: 0