Read an Excerpt
Chapter 3: Swing Programming Topics
3.1 Versions of Swing
http://java.sun.com/products/jfc/. As this book goes to press, the most recent version of Swing is Swing 1.1.1. This version of Swing is bundled with Java 1.2.2 and is also available for use with Java 1.1 from the web site mentioned in the previous paragraph. Swing 1.1.1 fixes many bugs in the initial release of Swing 1.1 but does not change the Swing 1.1 API in any way. Its use is strongly recommended. Swing 1.1.1 is the last release of Swing that will be available for use with Java 1.1.Development of Swing continues, and Java 1.3 will ship with a new version that includes a number of minor changes and improvements to the Swing API. This future release will focus on improving the existing APIs and should not add many new APIs.
3.2 Labels and HTML
In the initial releases of Swing 1.1 and Java 1.2, the JLabel
, JButton
, and related classes that display textual labels can display only a single line of text using a single font. In Swing 1.1.1 and Java 1.2.2, however, components like these can display multiline, multifont text using simple HTML formatting. To display formatted text, simply specify a string of HTML text that begins with an <HTML>
tag. You can use this feature to present text using multiple fonts, font styles, and colors. Just as important, however, the introduction of HTML allows you to specify multiline labels.
This new formatted text display feature is available in Java 1.2.2 for the JLabel
, JButton
, MenuItem
, JMenu
, JCheckBoxMenuItem
, JRadioButtonMenuItem
, JTabbedPane
, and JToolTip
classes. It is not supported (at least in Java 1.2.2) by JCheckBox
or JRadioButton
, however. Formatted text display is particularly useful with JOptionPane
dialog boxes (described later in this chapter), as they display text using internal JLabel
objects.