Can Items in a JList be formatted as HTML

孤街浪徒 提交于 2019-12-18 08:14:08

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3070484/can-items-in-a-jlist-be-formatted-as-html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!