Code example for JLabel

Methods: setSizegetPreferredSizesetText

0
   void updateLabel(){ 
 
      String htmltxt = 
         "<html><div style='" + getStyleString() + "'>" 
         + text + "</div></html>";
      label.setText(htmltxt);
 
      Dimension size = label.getPreferredSize();
      if (size.width > max_width){
         // hack to limit the width of the text to width 
         htmltxt = 
            "<html><div style='width:" + max_width + ";" + getStyleString() + "'>"
            + text + "</div></html>";
         label.setText(htmltxt);
      } 
      updateSize(); 
   } 
 
   void updateSize(){ 
      Dimension size = label.getPreferredSize();
      label.setSize(size);
      setActualSize(size);
   } 
 
 
   int fontSize = 12;