org.tbee.swing
Class MultiLineLabel

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by org.tbee.swing.MultiLineLabel
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class MultiLineLabel
extends JPanel

This class is taken from O'Reilly's Java in a Nutshell volume 1
It allows for a label which displays more than one line of text.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  int alignment
          The default alignment of the text is LEFT
static int CENTER
          Used to set the alingment of the text to the center.
static int LEFT
          Used to set the alingment of the text to the left.
protected  int line_ascent
          How far below the "line" a line of text extends, base on the font.
protected  int line_height
          The height of a line of text, based on the font used to display it.
protected  int[] line_widths
          The widths of each line of text, based on the font.
protected  String[] lines
          The text to display on the label.
protected  int margin_height
          The amount of space to place around the text, top to bottom.
protected  int margin_width
          The amount of space to place around the longest line of text, left to right.
protected  int max_width
          The maximum width of all the lines of text, based on font.
protected  int num_lines
          The number of individual lines of text to display on the label.
static int RIGHT
          Used to set the alingment of the text to the right.
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
MultiLineLabel(String label)
          The simplest of the constructors, all that is passed to it is the text that is to be displayed.
MultiLineLabel(String label, int alignment)
          This constructor is given the message to display and the alingment.
MultiLineLabel(String label, int margin_width, int margin_height)
          This constructor allows the message, margin width, and margin height to be passed in.
MultiLineLabel(String label, int margin_width, int margin_height, int alignment)
          This constructor allows the message, margin width, margin height, and alingment to be passed set.
 
Method Summary
 void addNotify()
          addNotify is used by the MultiLineLabel to know when the label is set up enough to allow for the font to be measured.
 String getText()
           
static void main(String[] args)
           
protected  void measure()
          This protected method is used by the MultiLineLabel to measure the amount of space which will be required to display the text, given whatever font we are displaying it in.
 Dimension minimumSize()
          minimumSize is used by the layout manager to determine the minimum amount of space need to display the text.
protected  void newLabel(String label)
          This protected method is called by the MultiLineLabel to set up the text for display.
 void paint(Graphics g)
          paint is the method which is called by the layout manager to draw the MultiLineLabel, with text to the screen.
 Dimension preferredSize()
          preferredSize is used by the layout manager to determine how much space the MultiLineLabel needs to display itself properly.
 void setAlignment(int a)
          This method allows for the alignment to be set, after the alingment is set the label is redisplayed by calling the repaint method.
 void setFont(Font font)
          Set the font to be used to display the text.
 void setForeground(Color color)
          This method allows the text color to be changed.
 void setLabel(String label)
          Set the label to display.
 void setMarginHeight(int mh)
          The margin height, specifies the amount of space from the top and bottom most line to the edge of the label.
 void setMarginWidth(int mw)
          The margin width, specifies the amount of space from the two ends of the longest line of text, to the edge of the label.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, paintComponents, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LEFT

public static final int LEFT
Used to set the alingment of the text to the left.

See Also:
Constant Field Values

CENTER

public static final int CENTER
Used to set the alingment of the text to the center.

See Also:
Constant Field Values

RIGHT

public static final int RIGHT
Used to set the alingment of the text to the right.

See Also:
Constant Field Values

lines

protected String[] lines
The text to display on the label.


num_lines

protected int num_lines
The number of individual lines of text to display on the label.


margin_width

protected int margin_width
The amount of space to place around the longest line of text, left to right.


margin_height

protected int margin_height
The amount of space to place around the text, top to bottom.


line_height

protected int line_height
The height of a line of text, based on the font used to display it.


line_ascent

protected int line_ascent
How far below the "line" a line of text extends, base on the font.


line_widths

protected int[] line_widths
The widths of each line of text, based on the font.


max_width

protected int max_width
The maximum width of all the lines of text, based on font.


alignment

protected int alignment
The default alignment of the text is LEFT

Constructor Detail

MultiLineLabel

public MultiLineLabel(String label)
The simplest of the constructors, all that is passed to it is the text that is to be displayed. The default alingment is LEFT and the default margin heigth and width is 10.

Parameters:
label - The text to display, as a String.

MultiLineLabel

public MultiLineLabel(String label,
                      int alignment)
This constructor is given the message to display and the alingment. The default margin heigth and width of 10 is used.

Parameters:
label - The text to display, as a String.
alignment - The alignment to display the text.

MultiLineLabel

public MultiLineLabel(String label,
                      int margin_width,
                      int margin_height)
This constructor allows the message, margin width, and margin height to be passed in. The default alingment of LEFT is used.

Parameters:
label - The text to display, as a String.
margin_width - The margin width to use while displaying the text.
margin_height - The margin height to use while displaying the text.

MultiLineLabel

public MultiLineLabel(String label,
                      int margin_width,
                      int margin_height,
                      int alignment)
This constructor allows the message, margin width, margin height, and alingment to be passed set. All of the other constructors call this one.

Parameters:
label - The text to display, as a String.
margin_width - The margin width to use while displaying the text.
margin_height - The margin height to use while displaying the text.
alignment - The alingment to use while displaying the text.
Method Detail

addNotify

public void addNotify()
addNotify is used by the MultiLineLabel to know when the label is set up enough to allow for the font to be measured. This method is not normaly called by a programmer.

Overrides:
addNotify in class JComponent

minimumSize

public Dimension minimumSize()
minimumSize is used by the layout manager to determine the minimum amount of space need to display the text. This method is not normaly called by a programmer.

Overrides:
minimumSize in class Container
Returns:
Dimension The space needed to display the text.

paint

public void paint(Graphics g)
paint is the method which is called by the layout manager to draw the MultiLineLabel, with text to the screen. This method is not normaly called by a programmer.

Overrides:
paint in class JComponent
Parameters:
g - The Graphics to draw upon.

measure

protected void measure()
This protected method is used by the MultiLineLabel to measure the amount of space which will be required to display the text, given whatever font we are displaying it in. This method is not normaly called by a programmer.


newLabel

protected void newLabel(String label)
This protected method is called by the MultiLineLabel to set up the text for display. '\n' (carrage return) is used to break up the text into individual lines.

Parameters:
label - The text to be displayed.

preferredSize

public Dimension preferredSize()
preferredSize is used by the layout manager to determine how much space the MultiLineLabel needs to display itself properly. This method is not normaly called by a programmer.

Overrides:
preferredSize in class Container
Returns:
Dimension The space needed to display the label.

setAlignment

public void setAlignment(int a)
This method allows for the alignment to be set, after the alingment is set the label is redisplayed by calling the repaint method.

Parameters:
a - The alingment to use when displaying the text. Passed using the static ints defined in this class.

setFont

public void setFont(Font font)
Set the font to be used to display the text. After setting the font the label is redisplayed, by calling the measure and repaint methods.

Overrides:
setFont in class JComponent
Parameters:
font - The Font to use.

setForeground

public void setForeground(Color color)
This method allows the text color to be changed. After the color is changed, the label is redisplayed, by calling the repaint method.

Overrides:
setForeground in class JComponent
Parameters:
color - The Color to display the text in.

setLabel

public void setLabel(String label)
Set the label to display. After the label is set, the measure and repaint methods are called to change the display.

Parameters:
label - The text to display, passed in as a String.

getText

public String getText()

setMarginHeight

public void setMarginHeight(int mh)
The margin height, specifies the amount of space from the top and bottom most line to the edge of the label. After the margin height is changed, the label is redrawn, by calling repaint.

Parameters:
mh - The margin height, in the form of an integer.

setMarginWidth

public void setMarginWidth(int mw)
The margin width, specifies the amount of space from the two ends of the longest line of text, to the edge of the label. After the margin width is set, the label is redrawn, by calling repaint.

Parameters:
mw - The margin width, in the form of an integer.

main

public static void main(String[] args)
                 throws Exception
Throws:
Exception


Copyright © 2011 KnowledgePlaza. All Rights Reserved.