JavaFX is powerful, believe it or not and in this session I will try to share some tips and tricks that might
help you creating your own JavaFX based apps. I will cover things like styling a JavaFX app using CSS, let you know
when it makes sense to use the JavaFX Canvas node, why you really should learn how to use a vector drawing program.
In addition you will learn how easy it is to make your JavaFX application not only run on your desktop machine but also
on mobile devices or even on the browser...without using a plugin.
So if you are curious and would like to learn more about JavaFX...listen...and learn...
11. NATIVE LOOKING WINDOWS
Properties
Title
Buttons for minimize, maximize, close
Resizeable
Draggable
Rounded corners on Macos
Support dark and light themes !
Have toolbuttons, text
fi
elds etc. on Macos !!!
45. SHADOWS...
Custom windows
Add another Region
Keep the Region transparent
Add padding to left, right, bottom
Add the BorderPane to the
Region
Apply the shadow to the
BorderPane
BorderPane
setE
ff
ect(new DropShadow())
DropShadow
Stage
Transparent Region
49. public class CircleButton extends Circle {
public enum Type {
CLOSE("close"),
MINIMIZE("minimize"),
MAXIMIZE("maximize");
public final String style;
Type(final String style) {
this.style = style;
}
}
public CircleButton(final Type type) {
super(6);
getStyleClass().add(type.style);
}
}
CODE BUTTON...
extends Circle
50. public class CircleButton extends StackPane {
public enum Type {
CLOSE("close"),
MINIMIZE("minimize"),
MAXIMIZE("maximize");
public final String style;
Type(final String style) {
this.style = style;
}
}
private final Region icon;
public CircleButton(final Type type) {
super();
icon.getStyleClass().add("icon")
getChildren().setAll(icon);
getStyleClass().add(type.style);
}
}
CODE BUTTON...
extends StackPane
Region icon
51. .close:hover .icon {
-fx-scale-shape : false;
-fx-shape : "M 2,2.5 l 2,0 l 0,2.5 l 2.5,0 l 0,2
l -2.5,0 l 0,2.5 l -2,0 l 0,-2.5
l -2.5,0 l 0,-2 l 2.5,0 l 0,-2.5 z";
}
CSS BUTTON...
adding points to the SVG shape...
52. .close:hover .icon {
-fx-background-color: rgb(55, 52, 52);
-fx-scale-shape : false;
-fx-shape : "M 2,2.5 l 2,0 l 0,2.5 l 2.5,0 l 0,2
l -2.5,0 l 0,2.5 l -2,0 l 0,-2.5
l -2.5,0 l 0,-2 l 2.5,0 l 0,-2.5 z";
}
CSS BUTTON...
set the
fi
ll color
53. .close:hover .icon {
-fx-rotate : 45;
-fx-background-color: rgb(55, 52, 52);
-fx-scale-shape : false;
-fx-shape : "M 2,2.5 l 2,0 l 0,2.5 l 2.5,0 l 0,2
l -2.5,0 l 0,2.5 l -2,0 l 0,-2.5
l -2.5,0 l 0,-2 l 2.5,0 l 0,-2.5 z";
}
CSS BUTTON...
rotate the shape by 45°
67. WHEN TO USE CSS...?
Use CSS when...
...you use standard controls
...your control inherits a standard control
...it has an impact on many controls
...the environment is already using CSS
...creating a library and want total control
...the customer should be able to customize the app
71. Re-styled ToggleButton
using CSS
Icons implemented
using SVGPath
Chart implemented
using Canvas
SWIFT JAVA FX
ToggleButton
HBox ToggleButton ToggleButton
78. Makes use of AWT (EDT* + FXAT**) !!!
Only supports images !!!
Sizes di
ff
er on operating systems
Macos, Windows, Linux***
FXTRAYICON
FXTrayIcon library
*** not all all distributions
** FX Application Thread
* Event Dispatch Thread
80. public BufferedImage createTextTrayIcon(String text, Color color) {
final int width = 64;
final int height = 18;
final double fontSize = 14;
final double x = 32;
final double y = 14;
Canvas canvas = new Canvas(width, height);
GraphicsContext ctx = canvas.getGraphicsContext2D();
ctx.setFont(Fonts.sfProRoundedRegular(fontSize));
ctx.setTextAlign(TextAlignment.CENTER);
ctx.setFill(color);
ctx.fillText(text, x, y);
final WritableImage img = new WritableImage(width, height);
final SnapshotParameters parameters = new SnapshotParameters();
parameters.setFill(Color.TRANSPARENT);
canvas.snapshot(parameters, img);
return SwingFXUtils.fromFXImage(img, null);
}
FXTRAYICON
FXTrayIcon library
Draw text in Canvas node
Take a snapshot of the node
Create a Bu
ff
eredImage
Display the image as tray icon
Sync EDT and FXAT !!!
91. JPRO APP
Advantages of using JPro
Large parts of the code can be re-used
Performance is good
Easy to start with for Java developers
Runs on every HTML5 capable browser
Is very close to the desktop version
Can run in docker container
Dark/light mode support
92. JPRO APP
Disadvantages of using JPro
Need a server/cloud infrastructure to run on
Multiuser setup is tricky running on one instance
Dialogs are di
ff
erent from Desktop
No accent color support
101. GLUON APP
Advantages of using Gluon
Large parts of the code can be re-used
Performance is NOT the problem
Easy to start with for Java developers
Runs on iOS and Android
Can be adjusted for each platform
Can be published on app stores
102. GLUON APP
Disadvantages of using Gluon
No real native UI out of the box (better on Android)
Problem with 24/7 apps (no native background tasks)
No support for iOS widgets and watch faces
Sound is handled di
ff
erently than on desktop
No accent color support
No dark/light mode support
No automatic locale detection
111. CANVAS NODE
Advantages of using Canvas
Just one node
You have "full" control
Fast
Flexible
99% compatible with HTML5 canvas
112. CANVAS NODE...
Disadvantages of using Canvas
No direct interaction with shapes
Layout is your job (no layout container)
Cleanup and redraw is your job
Not testable
Content is not styleable using CSS*
* partly possible using StyleableProperties
114. CANVAS NODE...
When to use it...
Complex graphics needed
Pixel based graphics needed
Performance needed
No interaction needed
Full control needed
Games
On graphical restricted devices (e.g. Raspberry Pi)
127. PERFORMANCE...
Avoid deeply nested layout structures
Reduce number of nodes
Avoid using e
ff
ects
Apply e
ff
ects to groups instead of single nodes
Keep track of dirty state when override layoutChildren()
Use Canvas if needed
Cache nodes before doing animations
Things to keep in mind
128. PERFORMANCE...
Be aware of FXApplicationThread
Avoid inlining CSS
Know your target platform
Avoid using transparent stages if possible
Disable layout of unused nodes (setManaged(true/false))
Things to keep in mind