0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.