Reputation: 1181
I've run into a problem where I receive the error: start(Stage) in GUIDirectory cannot be applied.
I basically want to give the user the ability to choose between a Terminal Version of my program, and a GUI version of my program. 1 for Terminal, 2 for GUI. I can easily implement my code when I call the terminal version:
if(option == 1){
myTerminal.printMenu();
}
Easy peesy. Once the user presses 1, it grabs the code and runs it no problem.
However, when I have this code:
if(option == 2){
myGui.start();
}
It throws a fit, this is my Main constructor:
public Main(){
Scanner input = new Scanner(System.in);
int option = input.nextInt();
if(option == 1){
myTerminal.printMenu();
}
if (option == 2){
myGui.start();
}
}
This is the GUIDirectory Class:
public class GUIDirectory extends Application {
public GUIDirectory(){}
public void start(Stage primaryStage) throws Exception {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
yada yada yada ...
All I want to do is give the user access to launch the GUI version by pressing 2. Is there no way to implement this? Or should I just simply create two separate programs?
SOLVED credit to iMan.
if(option == 2){
try{
myGui.launch(GUIDirectory.class);
} catch (Exception e) {
e.printStackTrace();
}
}
I'll have to look further into why this works, but thank you again guys!
Upvotes: 0
Views: 1544
Reputation: 1918
If this is a javafx application, you shouldnt be calling the start(Stage stage)
method yourself. Instead you should call launch(String args)
from within your GuiDirectory class.(since that method is caller sensitive). If you dont, bad things will happen, and all kind of exceptions will be thrown since javafx isnt initialized by calling start()
. So my Application class would look something like:
public class GUIDirectory extends Application {
public GUIDirectory(){}
@Override
public void start(Stage primaryStage) throws Exception
{
// build application etc.
}
Public static void launchApplication(String... args)
{
launch(args);
}
Main method:
Public static void main(String... args)
{
// do stuf, and call this if the Gui should be openend.
GUIDirectory.launchApplication(args);
}
Edit:
Following iMan's suggestion, there is another way to launch the application without the need to create a separate method for it in your Application class. That is by calling Application.launch(GUIDirectory.class, args);
, where the args parameter stands for an array of Strings (like in the other method).
Upvotes: 2