Architecture of a multiple-windowed JavaFX 8 Application - java

I'm building a multi-window and multi-module simulation application, and I use JavaFX for GUI.
From what I understood, to make a JavaFX project run, you need to run Application.launch( MyApplicationSubclass.class ) and use the primary stage given in the start(Stage primaryStage) method to get started. Everything in the start method and beyond happens in a JavaFX thread.
My app is a multi-moduled application. Today, a Main class bootstraps the program and launches several modules (which are distinct Maven modules), including the one in charge of the GUI. In the beginning (when I designed the current architecture), I planned to create only one window. So, once loaded, the GUI module would make the Application.launch( MyOnlyMainWindow.class ) call, and everything would work fine.
Today, I'd like to add a console window, launched before any module, and used to "live-log" the application (by redirecting the System.out stream, between other things, into a TextFlow). But with the architecture previously described, it's impossible, because if I want ot keep the Application.launch() call in the GUI module, then I can't make it in the Console window, which is not a part of the GUI module. It would also be impossible to add another GUI module that would manage another set of window.
So I can think of two and a half ways to solve this:
Centralize, somehow, the Application.launch() call so that it is available to every class that would need it. I don't know how to do it though.
Change the Main class to heritate from javaFx Application class. But wouldn't that make me execute all my module instanciations into the JavaFX Thread? I would guess this is a bad way of doing things. Or even, if I decide to create other threads for module instanciation, those threads would have been created in the JavaFX thread.
(Use Swing for the Console Window, though it does not actually solve my problem a viable way... What if I need to create another window in another GUI module?)
What would be a viable way of creating and managing a multiple window application with JavaFX? How could I, for instance, centralize the JavaFX Application.launch() calls?
Or, in other words
Is it a good practice to decentralize the JavaFX Application implementation in a module and launch the module from the main() class, or is it better to implement it in the main() class, even if I try to make my program modular?

Related

Java Swing application on multiple package

I am currently working on a self-project using Java SE-13 on Eclipse with Window builder.
I intend to create a program that has the following design flow using individual button as the event-source.
[Edit](for those who do not wish to visit the link)
Default Package Swing application <---> Package A swing application
I using package to help me simulate then I am using people program and integrate it. Trying to think of modular and keeping the project organize
[/Edit]
I had tried to use the following two approach.
Destroy the "current Swing application"(extend Frame) using
XXX.dispose(), create the instance of the second Swing application
and set it visible.
It works fine if the flow is one direction but not bidirectional. When I try to return back to the "current Swing application" it was unable to destroy the second-swing application which it throws a Nullpointer exception.
Using CardLayout. By creating multiple Jpanel and only set the one I wish to display as visible(true).
It allows me to have the bi-direction flow. But I am not using packages.
I had heard about the getContentpane() method but I am unsure if it will meet my requirement or it the best practice. If so, how will you be implementing else if not, what will you do to achieve my desire flow.
Thank you and take care

Is it advisable to create a seperate class for JavaFX UI building if done entirely through objects rather than FXML

I'm figuring out whether or not to build my application based on FXML or plain Java. This is because we are not allowed to use the scene builder.
So my problem is whether or not to use one class to build the entire UI through various methods rather than using multiple FXML Files.
If the class option is the best way then should I implement 1 UI Stage per class or all in one
I've used only FXML upto now but when i tried integrating JFoenix i realized that there was no FXML Code given so this was an issue for me
I advice you to use JavaFX even when you have to hardcode it because even then your Code will be simpler. Creating an own UI is very complex and nothing for beginners. It needs a team of pro devs to create something like JavaFX. Then in JavaFX all methods you could dream of are included. So if I were you i would use JavaFX because it is a good and existing way to do it.

Using a CN1 Eclipse project from another one?

In Eclipse, I created one CN1 project and one normal Java project, which depends on the former. The latter contains some utilities (e.g., source code generation) and some JUnit tests. I use the following simple hack:
CodenameOneImplementation impl = new JavaSEPort();
Util.setImplementation(impl);
Display.init(impl);
It works, but there's a fullscreen window shown and the program doesn't terminate when main is done. I know, that it's the normal behavior for GUI applications, but I don't need any GUI as I only initialized Display, in order for Display#getResource to work.
How can I get rid of the window (or at least make it small)?
How can I terminate the program without having to call System.exit (i.e., something like running the event handling thread as daemon)?
Is there more to set up?
Use something like this:
JavaSEPort.setDefaultInitTarget(new JPanel());
This would draw the UI of the display into the blank JPanel.
About exiting the app you would need to use System.exit(0) as the EDT loop and native GUI loop are running. You can stop the EDT but that might not work well for the desktop port so just using exit is easy and common practice.

Expose JPanel to external jar

I have a Java SWING program with basically a single, simple window. The window contains a menu an a JPanel. Ideally, I'd want to use the menu to choose an external jar file (which is another SWING program) and run it. The jar should then execute in the background and use the JPanel in my window instead of creating a new window on its own.
Would this be possible? My guess is that I should somehow "expose" or "make available" my JPanel to the external application, but I can't figure out how I could do this. Even a simple code snippet would be really appreciated. Thanks a lot
You're going to need to load the external Jar in a custom class loader to make it available to your application to run. Something like URLClassLoader should be capable of doing the trick.
Your application and external Jar should use a common, known, interface(s) that they can communicate through. This allows you to load the external jar, find and load the required "entry" class and run it.
This interface should provide some kind of registration for notification/callback mechanism (AKA a listener), which you application can attach to the external "task" and which the external class could then use to provide notification of changes back to your application.
You should avoid, where possible, exposing more of you application to the external Jar then you have to. This reduces the risk of the external Jar messing with your application, or indeed, needing to even care about it...
You could use either the Observer Pattern and Producer/Consumer Pattern, depending on your needs
More on the Observer Pattern

Starting a second JavaFX Application

I'm trying to launch a JavaFx application from within a JavaFx application, but it looks like Application.launch() can only be called once. Does this mean I have to start a separate JVM... as in exec("java... or is there another way?
More background info. I want my JavaFx app to be able to build and run JavaFx apps. Right now it compiles classes in-memory, loads the classes... it would be really unfortunate to have to resort to writing everything to the filesystem so I can get a jar on the filesystem, so I can use exec to start it up.
As a secondary question... Is there a way to open another JavaFx window and get the stage and pass it to my newly compiled and loaded Application sub-class?
If you want to execute another JavaFX application in the same JVM you can just create instance of it, manually create Stage and call Application#start()
public void runAnotherApp(Class<? extends Application> anotherAppClass) throws Exception {
Application app2 = anotherAppClass.newInstance();
Stage anotherStage = new Stage();
app2.start(anotherStage);
}
N.B.: it wouldn't work if you use special features of standard initialization in anotherApp, e.g. Application.init() or Application.getParameters()

Categories

Resources