8.1 HTML on Components


8.1 HTML on Components

Most text-based Swing components, such as labels, buttons , menu items, tabbed panes, and tool tips, can have their text specified as HTML. The component will display it appropriately. If you want the label on a JButton to include bold, italic, and plain text, the simplest way is to write the label in HTML directly in the source code like this:

 JButton jb = new JButton("<html><b><i>Hello World!</i></b></html>"); 

The same technique works for JFC-based labels, menu items, tabbed panes, and tool tips. Example 8-1 and Figure 8-1 show an applet with a multiline JLabel that uses HTML.

Example 8-1. Including HTML in a JLabel
 import javax.swing.*; public class HTMLLabelApplet extends JApplet {   public void init( ) {        JLabel theText = new JLabel(      "<html>Hello! This is a multiline label with <b>bold</b> "      + "and <i>italic</i> text. <P> "      + "It can use paragraphs, horizontal lines, <hr> "      + "<font color=red>colors</font> "      + "and most of the other basic features of HTML 3.2</html>");         this.getContentPane( ).add(theText);      } } 

Figure 8-1. An HTML label

You can actually go pretty far with this. Almost all HTML tags are supported, at least partially, including IMG and the various table tags. The only completely unsupported HTML 3.2 tags are <APPLET> , <PARAM> , <MAP> , <AREA> , <LINK> , <SCRIPT> , and <STYLE> . The various frame tags (technically not part of HTML 3.2, though widely used and implemented) are also unsupported. In addition, the various new tags introduced in HTML 4.0 such as BDO , BUTTON , LEGEND , and TFOOT , are unsupported.

Furthermore, there are some limitations on other common tags. First of all, relative URLs in attribute values are not resolved because there's no page for them to be relative to. This most commonly affects the SRC attribute of the IMG element. The simplest way around this is to store the images in the same JAR archive as the applet or application and load them from an absolute jar URL. Links will appear as blue underlined text as most users are accustomed to, but nothing happens when you click on one. Forms are rendered, but users can't type input or submit them. Some CSS Level 1 properties such as font- size are supported through the style attribute, but STYLE tags and external stylesheets are not. In brief, the HTML support is limited to static text and images. After all, we're only talking about labels, menu items, and other simple components.

Java Network Programming
Java Network Programming, Third Edition
ISBN: 0596007213
EAN: 2147483647
Year: 2003
Pages: 164

Similar book on Amazon

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