So I have the code
import javax.swing.JOptionPane;
import javafx.application.Application;
public abstract class HHGUI extends Application {
JOptionPane yesno = new JOptionPane();
public HHGUI() {
int reply = JOptionPane.showConfirmDialog(null,"Do you want the ground to generate from premade file?","Read Ground From File", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
Manager.plateaucreator();
System.exit(0);
}
else {
Manager.randplateaucreator();
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
}
and I keep getting the error
Exception in Application constructor
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:873)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class HHGUI
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$7(LauncherImpl.java:819)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
... 1 more
Exception running application HHGUI
and I have looked through other questions and cant find anything I am missing to cause this error. I have read up on JOptionPanes but am still unfamiliar with them so help solving this error or help finding what is causing it would be appreciated.
You can't invoke an abstract class. Remove the abstract modifier and implement start method:
import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.stage.Stage;
public class HHGUI extends Application {
JOptionPane yesno = new JOptionPane();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
int reply = JOptionPane.showConfirmDialog(null,"Do you want the ground to generate from premade file?","Read Ground From File", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
System.out.println("Yes");
System.exit(0);
}
else {
System.out.println("NO");
System.exit(0);
}
}
}
Although it is possible to use swing JOptionPane, in JavaFx Alert control should be used :
import java.util.Optional;
import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
public class HHGUI extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Alert yesNo = new Alert(AlertType.CONFIRMATION,
"Do you want the ground to generate from premade file?",
ButtonType.YES,
ButtonType.NO);
yesNo.setTitle("Read Ground From File");
Optional<ButtonType> result = yesNo.showAndWait();
if (result.get() == ButtonType.OK) {
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}