Multiline Jlabel with specific HTML tag
I want to add multi line to my Jlabel; I do it with help of HTML, but in some situation I have problem with it, the situation is that I use special tag property like dir="RTL" and ... . What should I do to solve this problem?
If I use:
jLabel1.setText("<html><center>John<br>2010/7/21 11:57:47 AM<br>In<开发者_运维知识库;/center></html>");
The label show :
john
2010/7/21 11:57:47 AM
In
But If I use :
jLabel1.setText("<html DIR=\"LTR\"><center>John<br>2010/7/21 11:57:47 AM<br>In</center></html>");
The label show:
<html DIR=\"LTR\"><center>John<br>2010/7/21 11:57:47 AM<br>In</center></html>
jLabel1.setText("<html DIR="LTR"><center>John<br>2010/7/21 11:57:47 AM<br>In</center></html>");
Needs to be
jLabel1.setText("<html DIR=\"LTR\"><center>John<br>2010/7/21 11:57:47 AM<br>In</center></html>");
That is, you need to escape double quotes inside a double quoted string. What you're doing at the moment is printing out <html DIR=
, breaking the string, then printing ><center>John<br>2010/7/21 11:57:47 AM<br>In</center></html>
.
If you start your label text with <html dir="ltr">
, it will not be an HTML label but a plain text label. The reason is that java.swing.plaf.basic.BasicHTML#isHtmlString(String)
is very simple, its implementation is
public static boolean isHTMLString(String s) {
if (s != null) {
if ((s.length() >= 6) && (s.charAt(0) == '<') && (s.charAt(5) == '>')) {
String tag = s.substring(1,5);
return tag.equalsIgnoreCase(propertyKey);
}
}
return false;
}
so you can only use <html>
or <HTML>
, or interestingly also <abcd>
or other four-letter words :-)
So in your case you would have to use <html><span dir="ltr">Your text</span></html>
. However, the Swing HTML subsystem does not honor the dir
attribute. You have to call
label.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT)
in order to change the component orientation for a label.
精彩评论