Re: Swing uses bad features
 
"Andrey Kuznetsov" <spam0@imagero.com.invalid> wrote in message 
news:e64a6f$586$1@online.de...
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;
This code is essentially what I had. The main difference
(i.e., the thing that caused the problem) was that I had
     private String requested_text = null;
     private String converted_text = null;
Note the explicit setting to null.
(I also have a getRequestedText() method, which returns requested_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
-- 
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project