I learnt something new about Swing yesterday

I've been using Swing since it first appeared and thought myself pretty competent at using it despite not having done much in anger with it for the past couple of years, where my focus has been more browser-based.

I had a little task to update some of a colleague's code yesterday, which involved adding a new message to be displayed in a JOptionPane. I noticed, and was puzzled by the fact that there were messages in the file which had HTML constructs in them. These messages are displayed in a JOptionPane too.

At first, I ignored the HTML constructs, assuming that the HTML was somehow filtered. However, when I queried my colleague on it, it turned out that you can indeed use limited HTML in Swing components, and it is documented in the manual. I guess I just haven't looked at the JLabel API docs for so long that I missed the change.

In the end, I didn't need the HTML, because I only wanted basic multiline presentation in the JOptionPane, which you can achieve by interspersing the string "\\n" in your text. For example:

    String msg = "This is a\\nmulti-line\\nmessage.";

My colleague was trying to do the same multi=line display, but using HTML:

    String msg = "<html>This is a<br>multi-line<br>message.</html>";

I may not have needed it, but I'm sure I will find a use for it in the future, so it's nice to know.

Comments:

Yes, it's really cool to make these little discoveries (and to use your Sun blog to record them). It makes work even more fun than it normally is at Sun. :-)

Posted by Geertjan on April 08, 2005 at 10:00 AM BST #

Not only can you use HTML in labels, etc..., but Swing has a fairly competent HTML editor hidden away in it's code. I had a similar surprise a few weeks ago, when I found out that not only do EditorPanes support HTML, but RTF, too. I have an application that needs to edit RTF; this saved me a lot of time.

Posted by Eric Hancock on April 08, 2005 at 11:04 AM BST #

Post a Comment:
Comments are closed for this entry.
About

tdw

Search

Top Tags
Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today