Lesson 7: Graphical User Interface

So far, we have covered most of the basics of Java and Object-oriented programming. We are now ready to learn how to code a GUI!
Java provides a rich set of libraries that can be used to create a platform-independent graphical user interface. So we can use the graphics classes provided in JDK for constructing our own Graphical User Interface applications. Writing your own graphics classes will take far too long to be a feasible option.

There are two sets of Java APIs for GUI programming: AWT and Swing. In this tutorial we will be programming using Swing rather than AWT, as most of the AWT components have become obsolete and Swing has a more comprehensive set of graphics libraries.

Introduction to Swing

The main features of Swing are (Source: Swing website):

  1. Swing is written in pure Java (except a few classes) and therefore is 100% portable.
  2. Swing components are lightweight.
  3. Swing components (JComponents) are written in Java.
  4. Swing components support pluggable look-and-feel. You can choose between Java look-and-feel and the look-and-feel of the underlying OS (e.g., Windows, UNIX or Mac). If the later is chosen, a Swing button runs on the Windows looks like a Windows' button and feels like a Window's button. Similarly, a Swing button runs on the UNIX looks like a UNIX's button and feels like a UNIX's button.
  5. Swing supports mouse-less operation, i.e., it can operate entirely using keyboard.
  6. Swing components support "tool-tips".
  7. Swing components are JavaBeans – a Component-based Model used in Visual Programming (like Visual Basic). You can drag-and-drop a Swing component into a "design form" using a "GUI builder" and double-click to attach an event handler.
  8. Others can be found on the Swing website

Swing component classes (in package javax.swing) begin with a prefix "J", e.g., JButton, JTextField, JLabel, JPanel, JFrame, or JApplet.

Original Source: https://netbeans.org/images_www/v7/3/features/client-swing-palette-cut.png