net.wotonomy.ui.swing.components
Class ButtonPanel

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 net.wotonomy.ui.swing.components.ButtonPanel
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.MouseMotionListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible
Direct Known Subclasses:
CheckButtonPanel, RadioButtonPanel, StatusButtonPanel

public class ButtonPanel
extends javax.swing.JPanel
implements java.awt.event.ActionListener, java.awt.event.MouseMotionListener

ButtonPanel handles display and event broadcasting of standard buttons like OK/Cancel/Save/etc. The constructor takes a list or array of strings, each representing a button to appear on the panel from left to right. Any button click will send an action event to all listeners with the action command containing the corresponding string. Note action events are simply forwarded from the buttons themselves, so the source of the event will be the button, not the button panel. The button panel is the source of the STATE_CHANGED events that notify about changes to the panel itself.

Version:
$Revision: 904 $
Author:
michael@mpowers.net, $Author: cgruber $
See Also:
Serialized Form

Nested Class Summary
 class ButtonPanel.ActionChangeListener
          A property change listener that listens specifically for property changes from action objects.
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected  java.awt.event.ActionListener actionListener
           
static java.lang.String ADD
          Specifies an "Add" button.
protected  java.awt.Container buttonContainer
          This is the container to which buttons are added.
protected  java.util.Vector buttonList
          This is the list of all buttons on the panel.
protected  java.awt.FlowLayout buttonPanelLayout
          This is the layout manager - which must be a FlowLayout or subclass.
static java.lang.String CANCEL
          Specifies a "Refresh" button.
static java.lang.String CLEAR_ALL
          Specifies a "Clear All" button.
protected  java.awt.Insets insets
          The insets for this panel, so they can be modified.
static java.lang.String NO
          Specifies a "No" button.
static java.lang.String OK
          Specifies a "OK" button.
static java.lang.String REFRESH
          Specifies a "Refresh" button.
static java.lang.String REMOVE
          Specifies a "Remove" button.
static java.lang.String SAVE
          Specifies a "Save" button.
static java.lang.String STATE_CHANGED
          This is the action command to all listeners when the button state is changed.
static java.lang.String YES
          Specifies a "Yes" button.
 
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
ButtonPanel()
          Constructs a ButtonPanel.
ButtonPanel(javax.swing.Action[] actionList)
          Constructs a ButtonPane using specified actions.
ButtonPanel(java.lang.String[] buttonList)
          Constructs a ButtonPanel using specified buttons.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Called by buttons on panel and by other components that might be set to broadcast events to this listener.
 void addActionListener(java.awt.event.ActionListener l)
          Adds an action listener to the list that will be notified by button events and changes in button state.
protected  void addComponentToPanel(java.awt.Component aComponent)
          Adds a component to the right-most side of the layout.
protected  void broadcastEvent(java.awt.event.ActionEvent e)
          Notifies all registered action listeners of a pending Action Event.
protected  java.awt.Component createComponentWithLabel(java.lang.String aLabel)
          Creates a new component with the specified label.
 int getAlignment()
          Gets the alignment of the buttons in the panel.
 java.awt.Component getButton(java.lang.String aLabel)
          Gets the first component having the specified name.
 int getHgap()
          Gets the current horizontal spacing between components.
 java.awt.Insets getInsets()
          Overridden to return the user-specified insets for this panel.
 java.lang.String[] getLabels()
          Gets the labels of the buttons that appear on the panel, ordered from left to right.
 int getVgap()
          Gets the current vertical spacing between components.
protected  void initLayout()
          This method is responsible for the initial layout of the panel.
static void main(java.lang.String[] argv)
           
 void mouseDragged(java.awt.event.MouseEvent e)
           
 void mouseMoved(java.awt.event.MouseEvent e)
           
 void removeActionListener(java.awt.event.ActionListener l)
          Removes an action listener from the list that will be notified by button events and changes in button state.
 void setAlignment(int alignment)
          Changes the alignment of the buttons in the panel.
 void setEnabled(boolean isEnabled)
          Overridden to call setEnabled on all components on panel.
 void setHgap(int newHgap)
          Changes the horizontal spacing between components in the panel.
 void setInsets(java.awt.Insets newInsets)
          Changes the insets for this panel.
 void setLabels(javax.swing.Action[] actions)
           
 void setLabels(java.lang.String[] labels)
          Creates the buttons to appear on the panel.
 void setVgap(int newVgap)
          Changes the vertical spacing between components in the panel.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, 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, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, 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, setFocusTraversalKeys, setFont, setForeground, 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, minimumSize, paintComponents, preferredSize, 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

OK

public static final java.lang.String OK
Specifies a "OK" button. This is also the action command sent by the OK button.

See Also:
Constant Field Values

SAVE

public static final java.lang.String SAVE
Specifies a "Save" button. This is also the action command sent by the Save button.

See Also:
Constant Field Values

REFRESH

public static final java.lang.String REFRESH
Specifies a "Refresh" button. This is also the action command sent by the Refresh button.

See Also:
Constant Field Values

CLEAR_ALL

public static final java.lang.String CLEAR_ALL
Specifies a "Clear All" button. This is also the action command sent by the Clear All button.

See Also:
Constant Field Values

CANCEL

public static final java.lang.String CANCEL
Specifies a "Refresh" button. This is also the action command sent by the Cancel button.

See Also:
Constant Field Values

YES

public static final java.lang.String YES
Specifies a "Yes" button. This is also the action command sent by the Yes button.

See Also:
Constant Field Values

NO

public static final java.lang.String NO
Specifies a "No" button. This is also the action command sent by the No button.

See Also:
Constant Field Values

ADD

public static final java.lang.String ADD
Specifies an "Add" button. This is also the action command sent by the Add button.

See Also:
Constant Field Values

REMOVE

public static final java.lang.String REMOVE
Specifies a "Remove" button. This is also the action command sent by the Remove button.

See Also:
Constant Field Values

STATE_CHANGED

public static final java.lang.String STATE_CHANGED
This is the action command to all listeners when the button state is changed.

See Also:
Constant Field Values

buttonContainer

protected java.awt.Container buttonContainer
This is the container to which buttons are added.


buttonList

protected java.util.Vector buttonList
This is the list of all buttons on the panel.


insets

protected java.awt.Insets insets
The insets for this panel, so they can be modified.


buttonPanelLayout

protected java.awt.FlowLayout buttonPanelLayout
This is the layout manager - which must be a FlowLayout or subclass.


actionListener

protected java.awt.event.ActionListener actionListener
Constructor Detail

ButtonPanel

public ButtonPanel()
Constructs a ButtonPanel. Three buttons are created so the panel is filled when used in a GUI-builder environment.


ButtonPanel

public ButtonPanel(java.lang.String[] buttonList)
Constructs a ButtonPanel using specified buttons.

Parameters:
buttonList - An array containing the strings to be used in labeling the buttons.

ButtonPanel

public ButtonPanel(javax.swing.Action[] actionList)
Constructs a ButtonPane using specified actions. For each action, a button is created, that when pressed the corresponding action is activated. The "name" of the action is used as the title of the button.

Parameters:
actionList - An array of actions to be used to create buttons with.
Method Detail

initLayout

protected void initLayout()
This method is responsible for the initial layout of the panel. Subclasses can implement different layouts, but this method is responsible for initializing buttonContainer and buttonPanelLayout and setting the container to use the layout.


setLabels

public void setLabels(java.lang.String[] labels)
Creates the buttons to appear on the panel. Any existing buttons are replaced. The labels are used as names and action commands in addition to labels.

Parameters:
labels - An array of strings to be used in labeling the buttons. If null, all buttons will be removed.

setLabels

public void setLabels(javax.swing.Action[] actions)

getLabels

public java.lang.String[] getLabels()
Gets the labels of the buttons that appear on the panel, ordered from left to right.

Returns:
A new list containing strings used in labeling the buttons.

getButton

public java.awt.Component getButton(java.lang.String aLabel)
Gets the first component having the specified name.

Returns:
A component with the specified name, or null if none match.

createComponentWithLabel

protected java.awt.Component createComponentWithLabel(java.lang.String aLabel)
Creates a new component with the specified label. The label is also used for the component's name and action command, if any. (This implementation returns a JButton.)

Parameters:
aLabel - The label for the component that will be created.
Returns:
The newly created component.

addComponentToPanel

protected void addComponentToPanel(java.awt.Component aComponent)
Adds a component to the right-most side of the layout.

Parameters:
aComponent - The component to be added to the layout.

setAlignment

public void setAlignment(int alignment)
Changes the alignment of the buttons in the panel. Defaults to right-justified.

Parameters:
alignment - A valid alignment code, per BetterFlowLayout implementation.
See Also:
BetterFlowLayout

getAlignment

public int getAlignment()
Gets the alignment of the buttons in the panel.

Returns:
An alignment code, per FlowLayout implementation.
See Also:
FlowLayout

setHgap

public void setHgap(int newHgap)
Changes the horizontal spacing between components in the panel.

Parameters:
newHgap - the new spacing, in pixels. May not be negative.

getHgap

public int getHgap()
Gets the current horizontal spacing between components.

Returns:
the current horizontal spacing, in pixels.

setVgap

public void setVgap(int newVgap)
Changes the vertical spacing between components in the panel.

Parameters:
newVgap - the new spacing, in pixels. May not be negative.

getVgap

public int getVgap()
Gets the current vertical spacing between components.

Returns:
the current vertical spacing, in pixels.

setInsets

public void setInsets(java.awt.Insets newInsets)
Changes the insets for this panel.

Parameters:
newInsets - the new insets.

getInsets

public java.awt.Insets getInsets()
Overridden to return the user-specified insets for this panel.

Overrides:
getInsets in class javax.swing.JComponent
Returns:
the current insets for this panel.

setEnabled

public void setEnabled(boolean isEnabled)
Overridden to call setEnabled on all components on panel.

Overrides:
setEnabled in class javax.swing.JComponent
Parameters:
isEnabled - whether to enable the panel and all components on it.

addActionListener

public void addActionListener(java.awt.event.ActionListener l)
Adds an action listener to the list that will be notified by button events and changes in button state.

Parameters:
l - An action listener to be notified.

removeActionListener

public void removeActionListener(java.awt.event.ActionListener l)
Removes an action listener from the list that will be notified by button events and changes in button state.

Parameters:
l - An action listener to be removed.

broadcastEvent

protected void broadcastEvent(java.awt.event.ActionEvent e)
Notifies all registered action listeners of a pending Action Event.

Parameters:
e - An action event to be broadcast.

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Called by buttons on panel and by other components that might be set to broadcast events to this listener.

Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
e - An action event to be received.

main

public static void main(java.lang.String[] argv)

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener


Copyright © 2006 null. All Rights Reserved.