Can Items in a JList be formatted as HTML
Asked Answered
C

2

6

I would like to create a JList in Java so that each individual item is formattted using HTML tags, but I'm not clear how to do this or even if this is possible. Does anyone have any suggestions?

Thank you.

Circulate answered 18/6, 2010 at 14:32 Comment(0)
F
7

Its actually very simple. For every string in the list surround it with the html tags such as this:

<html><font color=green>this will be green</font></html>

When the JList displays it it will be green.

Fernyak answered 18/6, 2010 at 14:46 Comment(1)
This was all I needed. Thanks.Circulate
W
3

Swing supports the use of HTML in many of the controls that display text.

In your case the JList is actually using a JLabel to display each item, so you just need the list model to return the string values in the list as HTML and it should all work.

Alternately you can write a javax.swing.ListCellRenderer that converts the value in the list to HTML.

There's some more info on Swing's HTML support here.

Wahoo answered 18/6, 2010 at 14:45 Comment(2)
This should work, just remember that embedding images in HTML code won't work when using Swing.Bourgeois
@Abel - good point, should've said it's only really a subset of HTML that's supported.Wahoo

© 2022 - 2024 — McMap. All rights reserved.