FileDocCategorySizeDatePackage
MultiLineLabel.javaAPI DocExample5874Sat Jan 24 10:44:38 GMT 2004je3.beans

MultiLineLabel

public class MultiLineLabel extends JComponent
A custom component that displays multiple lines of text with specified margins and alignment.

Fields Summary
protected String
label
protected int
margin_width
protected int
margin_height
protected Alignment
alignment
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, Alignment 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, Alignment.LEFT);
    
public MultiLineLabel(String label, Alignment alignment)

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

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

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

 return alignment; 
public java.lang.StringgetLabel()

 return label; 
public intgetMarginHeight()

 return margin_height; 
public intgetMarginWidth()

 return margin_width; 
public java.awt.DimensiongetMinimumSize()
This method is called when the layout manager wants to know the bare minimum amount of space we need to get by.

 return getPreferredSize(); 
public java.awt.DimensiongetPreferredSize()
This method is called by a layout manager when it wants to know how big we'd like to be.

	if (!measured) measure();
	return new Dimension(max_width + 2*margin_width,
			     num_lines * line_height + 2*margin_height);
    
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.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;
    
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 voidpaintComponent(java.awt.Graphics g)
This method draws the component. 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.getSize();
	if (!measured) measure();
	y = line_ascent + (size.height - num_lines * line_height)/2;
	for(int i = 0; i < num_lines; i++, y += line_height) {
	    if (alignment == Alignment.LEFT) x = margin_width;
	    else if (alignment == Alignment.CENTER) 
		x = (size.width - line_widths[i])/2; 
	    else x = size.width - margin_width - line_widths[i];
	    g.drawString(lines[i], x, y);
	}
    
public voidsetAlignment(Alignment a)

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

	super.setFont(f);         // Tell our superclass about the new font.
	repaint();                // Request a redraw.
	measured = false;         // Note that we need to remeasure lines.
	invalidate();             // Tell our containers about new size
    
public voidsetLabel(java.lang.String label)

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

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

 margin_width = mw; repaint();