Desktop Apps
Desktop Apps
Desktop Apps
At a distance there's a lot of similarity between the Swing/AWT/2D APIs and JavaFX APIs
• Both offer a comprehensive set of UI controls, with event handling , drag and drop, clip board, window
management ..
• Both support customization of the rendering of the UI controls
• Both provide graphics primitives and 2D geometry, transformations etc.
• Both supporting printing
[*] : Whenever we say or write just "Swing“ in this presentation we mean not just the Swing UI toolkit but
also Java 2D and AWT etc as a whole, since the old encompassing term of "JFC“ (Java Foundation
Classes) has fallen into dis-use. The phrase “JDK Client Libraries” is another way of describing all these
that are part of Java SE.
They work together seamlessly in the same application - even in the same
window. Swing can be nested within JavaFX – and vice versa.
And you can then interoperate between the two of them to create a first class
desktop application
- The take away will be you will be to apply this to build your own application for your use case.
• Applications can take time to start if they need to load files, access network resources etc
• Good UI experience gives immediate feedback that the App DID get the message to launch
• Add on command line (NO coding required)
java -splash:images/DukeMailbox.png
• Or embed the image in the jar and reference from the Manifest (META-INF/MANIFEST.MF)
SplashScreen-Image: images/DukeMailbox.png
• For best results provide splash images for different screen DPIs : Duke.png, Duke125pct.png,
Duke150pct.png, Duke200pct.png. JDK automatically selects the best match
• Splash is automatically hidden on displaying first AWT Window.
• Use the java.awt.SplashScreen API to programmatically update or hide the Splash
• Jmail applies a fadeout effect on an un-decorated replacement window.
• javax.swing.Timer fires every 20ms and calls Window.setOpacity(getOpacity()-0.05) until window
is invisible.
JFrame
JMail UI
JavaFX
TreeTableView JTable
embedded in a
JFXPanel from
the JavaFX JSplitPane
Swing interop
module JPanel with
JLabel
children
JSplitPane
JEditorPane
• JMail uses a Swing JFrame with a tool bar, folder list, messages list and message.
• The hierarchy of containers depends on how components should be grouped and what should
happen on resize
• The folder list is a JavaFX TreeTableView embedded in a JFXPanel
• The Message list is a JTable, the message view is a JPanel with multiple children
• JSplitPane is used to allow the user to reapportion the size of each grouping
• The Message JPanel children are another Panel for To/From/Subject etc
• JEditorPane is used to display the HTML content of the message text
• A variable number of JPanels display in-line attachments and the attachment list
• Choose a LayoutManager flexible enough to handle all the children of a container
as the window is resized. GridBagLayout or SpringLayout are possibilities for AWT. JavaFX has
GridPane, AnchorPane and simpler one such as HBox and VBox
TextField
HTMLEditor
import java.nio.files.*;
JComponent createTextPanel(String fileName) {
Path filePath = Path.of(fileName);
try {
text = Files.readString(filePath);
} catch (IOException e) {
}
return new JTextArea(text); // the important line
}
• Built-in support in JDK for PNG, GIF, JPEG, TIFF, BMP images
• Straightforward to load - but first check image size in a robust app.
• Scale image down to fit – some photo images are very large.
• Wrap it in a Swing JComponent to be layed out and painted.
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
..
BufferedImage image = ImageIO.read(imageFile);
..
class ImagePanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, x, y, scaledWidth, scaledHeight, null);
}
}
class MediaControl {
// add Play etc to wrapped MediaView
}
Not enough time to create or show everything, but for your app also consider :
Step 1 - Build your JDK runtime containing both Swing and JavaFX
First create the JDK to include in the application based on JDK 19 and OpenJFX 19 using the JDK’s jlink
command line tool.
Since we are emphasizing being a 1st class desktop app, you should look at jpackage platform options
Windows : Add app to the Windows Menu and have installer create a desktop shortcut.
Provide an application icon in Windows required format.
jpackage .. --win-menu --win-shortcut –icon jmail.ico ..
Look at the JMail app again now we know how it was implemented
• We have covered how to build a modern desktop application using both the Swing and JavaFX APIs
• We have looked at the desktop integration features these provide
• We have seen how easy it is to handle multiple content types
• We have learned how to use jpackage to create platform installers
• We have provided pointers to documentation
Shortly after Oracle Cloudworld / JavaOne 2022 we expect to post some of the content we have covered
here for you to review code etc at your leisure : https://cr.openjdk.java.net/~prr/javaone/2022/
You now have the information you need to start to build your own Java desktop UI application.
MORE ABOUT JAVAFX: “JavaFX 19 and Beyond” [LRN2615], THIS room, 2.30pm Thursday.