AWT vs. Swing

To begin, you need to know something of the differences between the "heavyweight" components of the AWT (Abstract Windowing Toolkit) and the "lightweight" components of Swing (a code name during development that stuck), and how they fit into the overall scheme of things. Study the handout showing the hierarchy of both AWT and Swing GUI interface components.

A component is said to be a heavyweight component if it uses native code provided by your computer's operating system to display buttons, choice lists, text fields, and the like. Such operating-system routines are said to be the components's peers.

A Swing component is said to be a lightweight component because it written entirely in Java and does the high-level display work itself, rather than relying on code provided by your computer's operating system.

You should try to avoid mixing heavyweight and lightweight components since a heavyweight component will hide any lightweight components that occupy the same display area.

All this having been said, it must also be remembered that Swing has not "replaced" AWT; instead it is built "on top of" AWT, which means that AWT is still very much a part of the Java infrastructure, though somewhat "under the covers" since the arrival of Swing. New code should, however, be written using Swing components in preference to the older AWT components wherever possible.

What are the fundamental questions for GUI development?

Although it's somewhat of an oversimplification, the essential questions that need to be asked when you are constructing a GUI interface are these:

  1. Which components will I need?
  2. Which of the (perhaps many) constructors that might be available should I use for each component?
  3. What properties of each component should I set after the component has been constructed, and what methods are available to do this? This may include things like fonts and colors, for example.
  4. How should my components be positioned in the window of my application? In other words, how and where do I add my components to the appropriate container? Should I use a built-in layout of some kind, or should I consider the absolute positioning of one or more components?
  5. What do I expect my components to do? That is, how do I arrange for appropriate events to be sent, and for those events to be received by the appropriate recipients who are "listening" for them, and then "handled" properly?
  6. How does my GUI interface relate to, and communicate with, the rest of my program? This relates to the notion of a "design pattern" called the MVC (or Model-View-Controller) design pattern.

Random facts to remember during Java GUI development

  1. Heavyweight components paint over lightweight components.
  2. Use standard layouts if at all possible. If you must use absolute positioning and sizing in a particular component, pass a null parameter to the constructor to disable whatever the default layout is, and then use the setLocation and setSize (or setBounds) methods.
  3. JFrames have a default BorderLayout, and components added to a JFrame all get placed in the CENTER portion of the BorderLayout, causing only the last-entered one to show up.
  4. If you want to access a "resource" file like an image file from a jar file, as part of the display of your GUI, use getResource() with the class that needs the file, in a statement analogous to this one:
    ImageIcon redBall = new ImageIcon(MyView.class.getResource("red-ball.gif"));
    
  5. When accessing files, the forward slash (/) can be used as a "universal" directory separator, another instance of cross-platform nature of Java. In other words, you do not need to use the double backslashes (\\) to separate directories in Java code when writing for a Windows platform (when providing a path to a file for getResource(), for example), though you could if you wanted to.
  6. If your top-level window extends JFrame (which will normally be the case), and you don't want the user to be able to resize it, be sure to call setResizable(false) on your application window.