0

I am trying to launch in my public class an application defined in another class, earning the "Unable to construct Application instance"-Exception.The example (below) is quite minimalistic. What am I missing?---Clues would be very much appreciated.

This question is different from the question

Unable to construct javafx.application.Application instance

as I would like to have the definition of an application and its launch in separate classes.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class Main {
    public static void main(String[] args) {
        Application.launch(OKButton.class, args);
    }
}

class OKButton extends Application {
    @Override
    public void start(Stage stage) {
        Button btn = new Button("OK");
        Scene scene = new Scene(btn, 200, 250);
        stage.setScene(scene);
        stage.show();
    }
}
Clyde Frog
  • 483
  • 1
  • 6
  • 15

1 Answers1

5

The problem is, that the constructor of your OKButton class is not accessible for JavaFX. You have to either put your OKButton class in a separate file or use it this way:

public class Main {
    public static void main(String[] args) {
        Application.launch(OKButton.class, args);
    }

    public static class OKButton extends Application {

        @Override
        public void start(Stage stage) {
            Button btn = new Button("OK");
            Scene scene = new Scene(btn, 200, 250);
            stage.setScene(scene);
            stage.show();
        }
    }
}
Halko Karr-Sajtarevic
  • 2,248
  • 1
  • 16
  • 14