Friday, May 20, 2022

Java desktop applications

I have posted a number of times about the various Java-based desktop application frameworks like Swing and JavaFX, and how you can create various applications with them in either Java or Clojure. It occurs to me that I should write an overview of the different desktop libraries that you can use in your JVM applications.

AWT

The AWT (abstract window toolkit) was introduced with Java 1.0. A major purpose of AWT and the first versions of Java was to provide support for Java applets. An applet is an AWT component and container, so applets were fully integrated into the AWT widget system. They basically allowed you to add AWT components into webpages. There weren't many options to add interactivity into websites at the time.

The AWT toolkit came with a peer system provided by the java.awt.peer package. Each AWT component came with a peer, which is that component defined in the native environment. This means that AWT components were heavy weight, already in the next version of Java was updated to address this.

The old peer system meant that each new Component class had its own opaque native window. Java 1.1 was updated with the lightweight UI framework which allowed you to directly extend java.awt.Component and java.awt.Container to create your own components that do not have native windows associated to them. In a way, this layed the foundation for Swing which builds upon these classes. It goes without saying that classical AWT is not used anymore.

Swing

The Swing library was released as part of Java 1.2. It was based upon the Java 1.1 lightweight UI framework. In particular, all Swing components extend java.awt.Component and java.awt.Container. They are lightweight, so they don't have native counterparts.

In place of native widgets, Swing came with a pluggable look and feel (PLAF). Swing widgets don't inherit from native widgets, but in theory you can at least make them look like them. Or you can modify the existing look and feel to create your own.

The fact that Swing was constructed on the AWT means that it has a lot of historical legacy. For example, all Swing widgets start with the letter "J" so that they don't interfer with their AWT counterparts. So you use JFrame instead of Frame, and JButton instead of Button, and so on. This makes Swing appear like a legacy framework with a great deal of baggage.

The fact that Swing was introduced early on meant that applets were still widely used. Swing extended the Applet class with the Swing based JApplet class, and after that many applets were created with Swing. Applets were phased out in the 2010s, for various reasons that we could go into depth on later. Probably the most basic one is that JavaScript gained a kind of maturity that led browser vendors to consider it sufficient.

Swing is basically where we are at now. Although it doesn't run in browsers and it is full of historical legacy, it is still a part of every Java SE distribution and some of the best desktop applications are developed with it. IntelliJ IDEA, for example, was created with Swing. So we are stuck with Swing as part of the Java desktop application stack for the forseeable future.

JavaFX

The fact that Java Swing comes with so much legacy means that it is necessary to have a fresh start. That fresh start came with JavaFX. The components in JavaFX do not extend from their AWT counterparts. JavaFX was intended to replace Swing in Java SE, but that project was abandoned and it was moved out of the JDK to be maintained elsewhere.

JavaFX also comes with a number of more modern features, like an updated scene graph library. Scene graphs are a fundamental part of any advanced structured graphical applications like zooming user interfaces or interactive 3D components. JavaFX was also equipped with an XML based file format and support for CSS styling of components. Its API is more modern and clean then Swing.

Alternatives

The Java platform comes with the Java native interface (JNI) which allows Java programs to interoperate with C/C++ programs. In theory, this should allow you to create any number of different types of user interfaces using native components. The only really successful example is SWT. It uses the JNI to create Java based extensions of native widgets, to provide users with a native look and feel that fits in with the platform.

As is the case with AWT, this means that it is hard or impossible to extend SWT components because they rely on native peers. Additionally, the fact that SWT requires the JNI means that you have to package your program to deal with different desktop environments and toolkits. SWT is not part of Java SE, but unlike other alternative desktop toolkits it has strong support because of its use in the Eclipse IDE.

Remarks

The Java platform is widely used in scientific and mathematical applications. These applications of course need to have data visualisation functionality associated with them. I am looking in to JavaFX for scientific visualisations, it already has its own charting library built in for example. But I can't make any promises, all I know is I will continue to use the JVM which I have already dedicated so much time to.

External references:
Java desktop module

JavaFX overview

No comments:

Post a Comment