How to Create GUI Applications with Java Classes - dummies

How to Create GUI Applications with Java Classes

By Barry Burd

Java has four (”count ’em, four”) sets of classes for creating GUI applications. Take a look at these classes and how you can use them in your own Java programming.

  • The Abstract Window Toolkit (AWT): The original set of classes, dating back to JDK 1.0.

    Classes in this set belong to packages whose names begin with java.awt. Components in this set have names like Button, TextField, Frame, and so on.

    Each component in an AWT program has a peer — a companion component that belongs to the computer’s own operating system. For example, when you create an AWT Button, a Mac computer creates its own kind of button to be displayed on the user’s screen.

    When the same program runs on a Windows computer, the Windows computer creates a different kind of button (a Windows button) to display on the computer’s screen. The Java code in the AWT interacts with the Mac or Windows button, adding additional functionality where functionality is needed.

    The AWT implements only the kinds of components that were available on all common operating systems in the mid-1990s. So, using AWT, you can add a button to your application, but you can’t easily add a table or a tree.

  • Java Swing: A set of classes created to fix some of the difficulties posed by the use of the AWT. Swing was introduced in J2SE 1.2.

    Classes in this set belong to packages whose names begin with javax.swing. Components in this set have names like JButton, JTextField, JFrame, and so on.

    Unlike an old AWT component, a Swing component has no peer. When you create a JButton in your Java program, the computer’s operating system doesn’t create a button of its own. Instead, the JButton that you see is a pure Java object. Java’s visual rendering code draws this object on a window. This is both good news and bad news.

    The good news is, a Swing program looks the same on every operating system. In a Swing program, you can create table components and tree components because Java simply draws them in the computer’s window. The bad news is, Swing components aren’t pretty. A JButton looks primitive and crude compared to a Mac button or a Windows button.

    Java’s Swing classes replace some (but not all) of the classes in the older AWT. To use some of the Swing classes, you have to call on some of the old AWT classes.

  • Eclipse’s Standard Widget Toolkit (SWT): An alternative to Java’s AWT and Swing sets. Despite the word “Standard” in SWT’s name, SWT is not part of Oracle’s standard Java.

    Classes in this set belong to packages whose names begin with org.eclipse.swt.

    The SWT takes an “all or nothing” approach. When you create an exotic component that a particular operating system doesn’t have, the SWT draws the component the way Swing does. (That is, SWT does all the work of creating and managing the component.)

    But when you create a component that’s built into a computer’s operating system, SWT displays the operating system’s component and adds no additional functionality. Unlike the AWT, the SWT creates an operating system button and then lets the button do its own thing. This carefully defined window of interaction between SWT and the operating system overcomes many of the difficulties posed by the design of the AWT.

    A Google Trends search in 2014 puts Swing way ahead of AWT and SWT in terms of interest by Java developers.

  • JavaFX: The newest set of GUI classes in Oracle standard Java. JavaFX comes with new(er) versions of Java 7 and with all versions of Java 8.

    Classes in this set belong to packages whose names begin with javafx.

    JavaFX supports over 60 kinds of components. (Sure, you want a Button component. But do you also want an Accordion component? JavaFX has one.) In addition, JavaFX supports multi-touch operations and takes advantage of each processor’s specialized graphics capabilities.