Section 17.2. HTML Text in Buttons and Labels


17.2. HTML Text in Buttons and Labels

A neat feature of Swing is that it can interpret HTML-formatted text in JLabel and JButton labels. The following example shows how to create a button with some HTML-formatted text:

     JButton button = new JButton(       "<html>"       + "S<font size=-1>MALL<font size=+0> "       + "C<font size=-1>APITALS"); 

Older versions of Java may not render complex HTML very well. But as of JDK 1.4, most basic HTML features are supported, including crazy things such as images and tables.

Figure 17-1 uses an HTML table to arrange its text.

Figure 17-1. Button using HTML table


Figure 17-2 uses an HTML image tag to display an image.

Figure 17-2. Button using HTML img tag


The code for the two figures looks like this:

     String html=         "<html><table border=1>"         +"<tr><td>One</td><td>Two</td></tr>"         +"<tr><td>Three</td><td>Four</td></tr>"         +"</table>";     JButton button = new JButton(html);     String html2=         "<html><h3>Learning Java</h3>"         +"<img src=\"http://www.oreilly.com/catalog/covers/learnjava3.s.gif\">";     Jbutton button2 = new JButton(html2);



    Learning Java
    Learning Java
    ISBN: 0596008732
    EAN: 2147483647
    Year: 2005
    Pages: 262

    Similar book on Amazon

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