Re: Swing uses bad features
For example, here's one that took me quite a while to
figure out what was wrong. I subclasses JLabel to create
a label that has multi-line capability when it is passed
a String with embedded newlines (not HTML). It
just prepends "<HTML>", replaces all newlines
with "<BR>", and appends "</HTML>".
I was a bit curios and implemented your JMultilineLabel.
And it works as expected.
here is quick and dirty implementation (not error free):
public class JMultiLineLabel extends JLabel {
private String requested_text; //assigned but never accessed
private String converted_text;
public JMultiLineLabel(String text, Icon icon, int horizontalAlignment)
{
super(text, icon, horizontalAlignment);
}
public void setText(String text) {
requested_text = text;
converted_text = checkText(text);
super.setText(converted_text);
}
private String checkText(String text) {
StringBuffer sb = new StringBuffer();
sb.append("<html>");
int count = text.length();
for (int i = 0; i < count; i++) {
char c = text.charAt(i);
if(c == 10 || c == 13) {
sb.append("<br>");
char c0 = text.charAt(i + 1);
if(c0 == 10 || c0 == 13) {
i++;
}
}
else {
sb.append(c);
}
}
sb.append("</html>");
return sb.toString();
}
public static void main(String[] args) {
JMultiLineLabel label = new JMultiLineLabel("abc\ndef\ngeh", null,
JLabel.LEFT);
System.out.println(label.getText());
}
}
the output was "<html>abc<br>def<br>geh</html>"
So I assume you have some other errors in your class.
Andrey
--
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities