JavaPerspective.com >
Intermediate Tutorials >
5. Graphical User Interfaces
5. Graphical User Interfaces
Last updated: 1 February 2013.
This chapter will show you how to build graphical user interfaces in Java with the Swing library.
A graphical user interface (GUI) is a frame containing interactive components like buttons, editable text fields, check boxes, combo boxes and so on. This chapter will give you tips in building graphical user interfaces in Java with a library called Swing that is included in the JDK.
In fact, the JDK provides two libraries for building GUIs: AWT (Abstract Windowing Toolkit) and Swing. AWT is the Java platform's first library for GUI development. Swing was introduced later as a replacement to AWT.
The difference between AWT and Swing is that AWT is based on heavyweight native system components whereas Swing implements lightweight Java portable components. Consequently, the two libraries differ in the following areas:
- Appearance: A GUI written with AWT will look different on different systems. If you write a program with AWT that displays a button and run it on a Linux system, a native Linux button will be displayed. If you run the same program on a Windows system, the result will have a completely different appearance since a Windows native button is displayed. Conversely, components created with Swing look pretty much the same on all systems. Furthermore, Swing components generally look much better than AWT components.
- Customization: Swing allows you to change the look and feel (L&F) of a GUI very easily. A given L&F displays the GUI in a particular flavour. The look defines the general appearance of components and the feel defines their behaviour. Several L&Fs are provided by the JDK. You can choose a L&F from the ones provided by the JDK or define your own L&F or just set the L&F to the native system's L&F. AWT does not support L&Fs.
- Components variety: Since AWT is based on native components, it has a limited set of components that are common to all systems. In contrast, Swing offers a much wider range of components. For example, progress bars, sliders, tables, trees, ... are only supported by Swing.
- Performance: In the early versions of Swing, there were performance issues because Swing components took longer to load than AWT components. However, the performance of Swing has been largely improved since the versions 1.4 and 1.5 of Java. On the whole, the performance problems of Swing are no longer noticeable, unless you are using a very old device with little RAM and low CPU speed.
In the light of the above, Swing seems to be a better choice than AWT for building GUIs, which is the reason why these tutorials only discuss GUI development with Swing. Besides, because AWT code is very similar to Swing code, if you can do Swing GUIs then you can pick up AWT with relative ease.
In fact, although Swing has a completely different approach to creating components, it is actually built on top of AWT. Consequently, when developing GUIs with Swing, you will probably have to import from AWT as well (fonts, colors, layouts, ... belong to AWT).
In the Java API, the AWT classes are in the package java.awt and the Swing classes are in the package javax.swing. The names of Swing components start with the letter J (JButton, JPanel, JFrame, ...) whereas AWT components do not (Button, Panel, Frame, ...).
As I said earlier, the tutorials of this chapter will look at GUI development with Swing. Nevertheless, if you still want to use AWT to build GUIs, note that it is not recommended to mix up AWT components with Swing components. Even if a program mixing up AWT and Swing components generally compiles, the result may display incorrectly.
The tutorials of this chapter are logically sequenced: tutorials 5.1 to 5.6 (included) discuss the basics I believe you need to know before being able to efficiently build GUIs with Swing. Then the tutorial 5.7 and the following will show you how to use a wide range of common Swing components.
This chapter is the last one in the intermediate section. More advanced Swing features will be discussed at the beginning of the advanced section.
5.1. Swing class hierarchy
5.2. Your first JFrame
5.3. Event listeners
5.4. Layouts
5.6. Look and Feel
5.7. Borders
5.8. Scroll panes
5.9. Labels
5.10. Text Fields
5.11. Formatted Text Fields
5.12. Password Fields
5.2. Your first JFrame
5.3. Event listeners
5.4. Layouts
5.4.1. How to use FlowLayout
5.4.2. How to use BoxLayout
5.4.3. How to use GridLayout
5.4.4. How to use BorderLayout
5.4.5. How to use CardLayout
5.4.6. How to use GridBagLayout
5.5. Colors and fonts5.4.2. How to use BoxLayout
5.4.3. How to use GridLayout
5.4.4. How to use BorderLayout
5.4.5. How to use CardLayout
5.4.6. How to use GridBagLayout
5.6. Look and Feel
5.7. Borders
5.8. Scroll panes
5.9. Labels
5.10. Text Fields
5.11. Formatted Text Fields
5.12. Password Fields
You are here :
JavaPerspective.com >
Intermediate Tutorials >
5. Graphical User Interfaces
Next tutorial : JavaPerspective.com > Intermediate Tutorials > 5. Graphical User Interfaces > 5.1. Swing class hierarchy
Next tutorial : JavaPerspective.com > Intermediate Tutorials > 5. Graphical User Interfaces > 5.1. Swing class hierarchy