FileDocCategorySizeDatePackage
MultiLineLabel.javaAPI DocExample6035Mon Sep 22 13:30:32 BST 1997oreilly.beans.yesno

MultiLineLabel

public class MultiLineLabel extends Canvas
A custom component that displays multiple lines of text with specified margins and alignment. In Java 1.1, we could extend Component instead of Canvas, making this a more efficient "Lightweight component"

Fields Summary
protected String
label
protected int
margin_width
protected int
margin_height
protected int
alignment
public static final int
LEFT
public static final int
CENTER
public static final int
RIGHT
protected int
num_lines
protected String[]
lines
protected int[]
line_widths
protected int
max_width
protected int
line_height
protected int
line_ascent
protected boolean
measured
Constructors Summary
public MultiLineLabel(String label, int margin_width, int margin_height, int alignment)

 // Have the lines been measured?

  // Here are five versions of the constructor.
      
                            
    this.label = label;                 // Remember all the properties.
    this.margin_width = margin_width;
    this.margin_height = margin_height;
    this.alignment = alignment;
    newLabel();                         // Break the label up into lines.
  
public MultiLineLabel(String label, int margin_width, int margin_height)

    this(label, margin_width, margin_height, LEFT);
  
public MultiLineLabel(String label, int alignment)

    this(label, 10, 10, alignment);
  
public MultiLineLabel(String label)

 this(label, 10, 10, LEFT); 
public MultiLineLabel()

 this(""); 
Methods Summary
public intgetAlignment()

 return alignment; 
public java.lang.StringgetLabel()

 return label; 
public intgetMarginHeight()

 return margin_height; 
public intgetMarginWidth()

 return margin_width; 
protected synchronized voidmeasure()
This internal method figures out how the font is, and how wide each line of the label is, and how wide the widest line is.

    FontMetrics fm = this.getToolkit().getFontMetrics(this.getFont());
    line_height = fm.getHeight();
    line_ascent = fm.getAscent();
    max_width = 0;
    for(int i = 0; i < num_lines; i++) {
      line_widths[i] = fm.stringWidth(lines[i]);
      if (line_widths[i] > max_width) max_width = line_widths[i];
    }
    measured = true;
  
public java.awt.DimensionminimumSize()
This method is called when the layout manager wants to know the bare minimum amount of space we need to get by. For Java 1.1, we'd use getMinimumSize().

 return preferredSize(); 
protected synchronized voidnewLabel()
This internal method breaks a specified label up into an array of lines. It uses the StringTokenizer utility class.

    StringTokenizer t = new StringTokenizer(label, "\n");
    num_lines = t.countTokens();
    lines = new String[num_lines];
    line_widths = new int[num_lines];
    for(int i = 0; i < num_lines; i++) lines[i] = t.nextToken();
  
public voidpaint(java.awt.Graphics g)
This method draws the label (same method that applets use). Note that it handles the margins and the alignment, but that it doesn't have to worry about the color or font--the superclass takes care of setting those in the Graphics object we're passed.

    int x, y;
    Dimension size = this.size();  // use getSize() in Java 1.1
    if (!measured) measure();
    y = line_ascent + (size.height - num_lines * line_height)/2;
    for(int i = 0; i < num_lines; i++, y += line_height) {
      switch(alignment) {
      default:
      case LEFT:    x = margin_width; break;
      case CENTER:  x = (size.width - line_widths[i])/2; break;
      case RIGHT:   x = size.width - margin_width - line_widths[i]; break;
      }
      g.drawString(lines[i], x, y);
    }
  
public java.awt.DimensionpreferredSize()
This method is called by a layout manager when it wants to know how big we'd like to be. In Java 1.1, getPreferredSize() is the preferred version of this method. We use this deprecated version so that this component can interoperate with 1.0 components.

    if (!measured) measure();
    return new Dimension(max_width + 2*margin_width,
                         num_lines * line_height + 2*margin_height);
  
public voidsetAlignment(int a)

 alignment = a; repaint(); 
public voidsetFont(java.awt.Font f)

    super.setFont(f);         // Tell our superclass about the new font.
    measured = false;         // Note that we need to remeasure lines.
    repaint();                // Request a redraw.
  
public voidsetForeground(java.awt.Color c)

    super.setForeground(c);   // Tell our superclass about the new color.
    repaint();                // Request a redraw (size is unchanged).
  
public voidsetLabel(java.lang.String label)

    this.label = label;
    newLabel();               // Break the label into lines.
    measured = false;         // Note that we need to measure lines.
    repaint();                // Request a redraw.
  
public voidsetMarginHeight(int mh)

 margin_height = mh; repaint(); 
public voidsetMarginWidth(int mw)

 margin_width = mw; repaint();