Hi all,
I have developed a JfxFrame for JavaFX for my own needs. Now you can have your JavaFX code without a JavaFX application. I share it as it might interest others. Please find below a small code sample.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import javafx.scene.control.Label; import javafx.scene.paint.Color; import org.capcaval.ermine.jfx.JfxFrame; import org.capcaval.ermine.jfx.JfxFrameCallBack; import org.capcaval.ermine.jfx.JfxTools; public class JfxFrameMain { public static void main(String[] args) { System.out.println("Main thread name : " + Thread.currentThread().getName()); // create a instance of frame JfxFrame frame = JfxFrame.factory.newInstance( "JfxFrameMain", 10, 10, 500, 300, new JfxFrameCallBack() { @Override public void notifyFrameCreated(JfxFrame frame) { // check out that it is running // inside the correct thread System.out.println("Jfx thread name : " + Thread.currentThread().getName()); frame.setbackgroundColor(Color.DARKBLUE); // create and add a label Label label = new Label("Hello!"); label.setTextFill(Color.WHITE); frame.setView(label); } }); // display it frame.display(); } } |
To use it you have to download Ermine.jar
Of course you can create several frames, just by allocating and displaying several instances.
At first glance, it looks like the old Swing or AWT frame, but it is a bit different as it is coming with a callback method which is executed inside the correct JavaFX Thread. I have created such frame not by nostalgia but for having a class containing main without JavaFX application. This way you can use JavaFX everywhere inside your own architecture and not only from a class with JavaFX’s main.
In my opinion the JavaFx code should be a leaf in architecture as other application parts like data processing, database access, network server etc..
To create this frame, I had to add a method known by Swing coder invokeAndWait that might also interest some JavaFX coders :
1 2 3 4 5 6 |
JfxTools.invokeAndWait(new Runnable() { @Override public void run() { // Add your code } }); |
Next time I will present you new features of RefPointLayout.
Cheers.
Mik