Using HTML in Swing Components

 < Day Day Up > 

Using HTML in Swing Components

Many Swing components display a text string as part of their GUI. By default, that text is displayed in a single font and color, all on one line. You can determine the font and color of a component's text by invoking the component's setFont and setForeground methods , respectively. For example, the following code creates a label and then sets its font and color:

 label = new JLabel("A label"); label.setFont(new Font("Serif", Font.PLAIN, 14)); label.setForeground(new Color(0xffffdd)); 

If you want to mix fonts or colors within the text or have formatting, such as multiple lines, you can use HTML. HTML formatting can be used in all Swing buttons , menu items, labels, tool tips, and tabbed panes, as well as in components such as trees and tables that use labels as renderers. To specify that a component's text has HTML formatting, just put the <html> tag at the beginning of the text; then use any valid HTML in the remainder. Here's an example of using HTML in a button's text:

 button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>"); 

Figure 7 shows the resulting button.

Figure 7. A button with HTML formatting

graphics/03fig07.gif

Performance Note: Because Swing's HTML rendering support uses many classes, users on older systems might notice a delay the first time a component with HTML formatting is shown. One way to avoid this delay is not to show the HTML-formatted component immediately and to create it (or another component that uses HTML) on a background thread.


Example One: HtmlDemo

The snapshot in Figure 8 shows an application called HtmlDemo that lets you play with HTML formatting by setting the text on a label.

Figure 8. The HTMLDemo application.

graphics/03fig08.jpg

Try This:

  1. graphics/cd_icon.gif

    Run HtmlDemo using Java Web Start or compile and run the example yourself. [1]

    [1] To run HtmlDemo using Java Web Start, click the HtmlDemo link on the RunExamples/components.html page on the CD. You can find the source files here: JavaTutorial/uiswing/components/example-1dot4/index.html#HTMLDemo .

  2. Edit the HTML in the text area at the left and click the Change the label button. The label at the right shows the result.

  3. Remove <html> from the text area on the left. The label's text is no longer parsed as HTML.

Example Two: ButtonHtmlDemo

graphics/cd_icon.gif

Let's look at another example that uses HTML (Figure 9). ButtonHtmlDemo adds font, color, and other text formatting to three buttons. [2]

[2] To run ButtonHtmlDemo using Java Web Start, click the ButtonHtmlDemo link on the RunExamples/components.html page on the CD. You can find the source files here: JavaTutorial/uiswing/components/example-1dot4/index.html#ButtonHTMLDemo .

Figure 9. The ButtonHtmlDemo application.

graphics/03fig09.gif

The left and right buttons have multiple lines and text styles and are implemented using HTML. The middle button, on the other hand, uses just one line, font, and color, so it doesn't require HTML. Here's the code that specifies the text formatting for these three buttons:

 b1 = new JButton("<html><center><b><u>D</u>isable</b><br>"                  + "<font color=#ffffdd>middle button</font>",                  leftButtonIcon); Font font = b1.getFont().deriveFont(Font.PLAIN); b1.setFont(font); ... b2 = new JButton("middle button", middleButtonIcon); b2.setFont(font); b2.setForeground(new Color(0xffffdd)); ... b3 = new JButton("<html><center><b><u>E</u>nable</b><br>"                  + "<font color=#ffffdd>middle button</font>",                  rightButtonIcon); b3.setFont(font); 

Note that we had to use a <u> tag to cause the mnemonic characters D and E to be underlined in the HTML-using buttons. Note also that when a button is disabled its HTML text unfortunately remains black instead of becoming gray. [3]

[3] Bug #4783068 requests that HTML text on disabled components be grayed out. You can track the bug status online at: http://developer.java.sun.com/developer/bugParade/ bugs /4783068.html.

This section discusses how to use HTML in ordinary, nontext components. For information on components whose primary purpose is formatting text, see Using Text Components (page 60) in this chapter.

Version Note: HTML rendering in ordinary Swing components was first added in v1.2.2 and completed in v1.3. The components that supported HTML in v1.2.2 were JButton , JLabel , JMenuItem , JMenu , JRadioButtonMenuItem , JCheckBoxMenuItem , JTabbedPane , and JToolTip . HTML support was added in v1.3 to JToggleButton , JCheckBox , and JRadioButton .


 < Day Day Up > 


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net