Reputation: 652
I have a programm with an optional gui, coltrolled by command line args. The programm has a database connection which is needed in both parts, gui and non-gui part.
So how can I create the connection, then create a stage and give the connection to the scenen controller? Either the controller must know the main class, or the main class must know the controller.
I could do this with static variables, but this seems to be ugly.
I also could always start the programm over the start(Stage stage)
-method. But then I create a stage even when I have a console only programm.
Upvotes: 0
Views: 216
Reputation: 209330
It seems that the Application
class forces the FX toolkit to start as soon as it is loaded. So I think you need to structure it something like this:
public class DataBaseAccessor {
// constructor connects to database...
// provides methods for data access ....
}
A GUI startup class:
public class MyGuiApp extends Application {
@Override
public void start(Stage primaryStage) {
DataBaseAccessor dbAccessor = new DataBaseAccessor();
// build UI etc...
primaryStage.show();
}
}
and a general startup class:
public class MyApp {
public static void main(String[] args) {
if (args.length == 1 && "-graphical".equals(args[0])) {
Application.launch(MyGuiApp.class, args) ;
} else {
runWithoutUI();
}
}
private static void runWithoutUI() {
DataBaseAccessor dbAccessor = new DataBaseAccessor();
// run without UI....
}
}
Now you can do
java MyApp
to run without the UI, and
java MyApp -graphical
to run with it.
Upvotes: 2