O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Use HTML and CSS in Text Components #52
Chapter 7, Text
|
275
HACK
This looks amusing and, when you type, the new text is indeed backward.
But the novelty wears off quickly; messing with the display screws up the
drawing of the caret and text highlighting, along with the handling of mouse
clicks to set the insert point. In playing around with
JTextArea
, we found
that we hosed word wrap, too, so we were stuck on the top line.
So, it’s a cute trick, but let’s reserve it for labels, OK?
H A C K
#52
Use HTML and CSS in Text Components Hack #52
Spruce up your plain JLabels and buttons using HTML and CSS effects, such
as underlines, color, and even embedded tables.
You may know that you can display HTML using a subclass of
JTextPane
,
but did you also know that Swing supports simple HTML and CSS in virtu-
ally every text component? As long as you can trick it into showing it as
HTML instead of the markup, you can do some pretty nifty things.
Here’s the Trick
Every text component in Swing can display HTML, but the component
needs to know that the text is HTML, rather than a string that just happens
to contain a bunch of angle brackets. Since there is no
setHTMLText(true)
method on JTextComponent, you have to resort to being a little trickier. If the
string passed to the component’s constructor (or
setText( )) method starts
with
<html>, then the component will switch to HTML mode. Here is a
quick example:
JButton b1a = new JButton("<html><i>my button</i>");
This code will produce a button that looks like Figure 7-11.
You don’t need to match the
<html> with an </html> tag at the end. Swing’s
HTML parser is pretty tolerant of malformed HTML, so for simple things
you can just type whatever is shortest. The mode can only be set once, so if
you put plain text into the component first and then HTML later, it will still
be in plain text mode. You should note that slower computers will exhibit a
noticeable delay the first time a component is shown with HTML. This is
because Swing has to load up all of the
javax.swing.text.html classes; how-
ever, they are cached for any further instances.
Figure 7-11. Italic text
276
|
Chapter 7, Text
#52 Use HTML and CSS in Text Components
HACK
To avoid this initial delay, you could load a hidden compo-
nent in a separate thread during program startup.
HTML can be used as a shortcut for text effects that would be cumbersome
or impossible with standard
Font objects. For example, Font doesn’t provide
a way to draw underlined text, but with HTML you can do this:
JLabel l1 = new JLabel("<html><u>underlined</u></html>");
This produces the text seen in Figure 7-12.
HTML also lets you get multi-lined text and mixed fonts:
JButton b2 = new JButton("<html><i>my</i> button</html>");
This produces the button seen in Figure 7-13.
You can even add line break tags:
JLabel l2 = new JLabel("<html>my multi-<br>line text</html>");
Using this, you get the button seen in Figure 7-14.
You can use HTML in more than just labels and buttons.
JCheckBox,
JRadioButton, and even JComboBox support it. Here is an example of each:
JCheckBox cb1 = new JCheckBox("<html>The <i>real</i> thing");
JRadioButton rb1 = new JRadioButton(
"<html>Even <font color=\"#ff0000\">better</font>");
Figure 7-12. Underlined text
Figure 7-13. Mixed styles
Figure 7-14. Multi-lined text
Use HTML and CSS in Text Components #52
Chapter 7, Text
|
277
HACK
String[] vals = { "<html><i>better</i>",
"<html><u>yet again</u>" };
JComboBox combo1 = new JComboBox(vals);
This creates the collection of HTML-styled components seen in Figure 7-15.
Swing supports most features of basic HTML: paragraphs, lists, simple
tables, and colors. It also supports basic CSS, which can give you greater
control over borders, padding, and other visual attributes. This next exam-
ple creates a header followed by list items of large colored text surrounded
by a border. Since there is so much markup, I put it into a
StringBuffer first:
StringBuffer sb = new StringBuffer( );
sb.append("<html><head><style type='text/css'>");
sb.append("li { font-style: italic; font-size: 30pt; }");
sb.append("li { font-family: serif; color: #ff5555; }");
sb.append("ul { border-width: 4px; border-style: solid;
border-color: #ff0000; } ");
sb.append("ul { background-color: #ffeeee; }");
sb.append("</style></head>");
sb.append("<h3>H3 Header</h3>");
sb.append("<ul><li>large serifed text</li><li>as list items</li>");
sb.append("</html>");
JLabel l3 = new JLabel(sb.toString( ));
This produces the label seen in Figure 7-16.
Figure 7-15. Some other HTML components
Figure 7-16. Borders and lists

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required