net.wotonomy.ui.swing.components
Class IconCellRenderer

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.IconCellRenderer
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.MouseListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.lang.Runnable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.CellEditor, javax.swing.ListCellRenderer, javax.swing.table.TableCellEditor, javax.swing.table.TableCellRenderer, javax.swing.tree.TreeCellEditor, javax.swing.tree.TreeCellRenderer

public class IconCellRenderer
extends javax.swing.JPanel
implements javax.swing.table.TableCellRenderer, javax.swing.table.TableCellEditor, javax.swing.tree.TreeCellRenderer, javax.swing.tree.TreeCellEditor, javax.swing.ListCellRenderer, java.lang.Runnable, java.awt.event.ActionListener, java.awt.event.MouseListener

A cell renderer that displays icons in addition to text, and additionally is an editor in case you want to click the icon to trigger some kind of action. You probably should override both getStringForContext and getIconForContext to achieve your desired results. To receive mouse clicks, set the same instance of the renderer as the editor for the same component.

One notable addition is that this class is an action event broadcaster. ActionEvents are broadcast when the mouse is clicked on the button with an action event containing a user-configurable string that defaults to CLICKED.

The renderer itself can be used as a JComponent if you need something like a JLabel that allows you to click on the icon. You will want to call setIcon and setText to configure the component since the renderer method would not be called. (If you add an instance of the renderer to a container, you cannnot use the same instance as an editor in a table, tree, or list.)

Version:
$Revision: 904 $ $Date: 2006-02-18 23:19:05 +0000 (Sat, 18 Feb 2006) $
Author:
michael@mpowers.net
See Also:
Serialized Form

Nested Class Summary
 
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
static java.lang.String CLICKED
           
protected  javax.swing.JButton editorButton
           
protected  javax.swing.JLabel editorLabel
           
protected  javax.swing.JPanel editorPanel
          The panel that is used to receive mouse clicks.
protected  javax.swing.JButton rendererButton
           
protected  javax.swing.JLabel rendererLabel
           
protected  javax.swing.JPanel rendererPanel
          The panel that is re-used to render everything.
 
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
IconCellRenderer()
          Default constructor.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent evt)
          Puts ourself on the end of the event queue for firing our action event to all listeners.
 void addActionListener(java.awt.event.ActionListener aListener)
          Adds the specified listener to the list of listeners to be notified when the button receives a click.
 void addCellEditorListener(javax.swing.event.CellEditorListener aListener)
          Adds the specified listener to the list of listeners to be notified when the table receives a double click.
 void cancelCellEditing()
          Fires an editing cancelled event and returns true.
protected  void configureComponentForContext(javax.swing.JPanel component, javax.swing.JButton iconButton, javax.swing.JLabel label, javax.swing.JComponent container, java.lang.Object value, int row, int column, boolean isSelected, boolean hasFocus, boolean isExpanded, boolean isLeaf)
          Called to configure components
protected  void fireActionEvent(java.awt.event.ActionEvent anActionEvent)
          Broadcasts the specified action event to all listeners.
 java.lang.String getActionCommand()
          Returns the action command broadcast when this icon receives a click.
 java.lang.Object getCellEditorValue()
          Returns lastKnownValue, although this should not be called.
 java.awt.Component getEditorForContext(javax.swing.JComponent container, java.lang.Object value, int row, int column, boolean isSelected, boolean hasFocus, boolean isExpanded, boolean isLeaf)
          This method returns a separate component that should be visually identical to the renderer component.
 javax.swing.Icon getIcon()
          Returns the icon currently displayed in the editor component.
 javax.swing.Icon getIconForContext(javax.swing.JComponent container, java.lang.Object value, int row, int column, boolean isSelected, boolean hasFocus, boolean isExpanded, boolean isLeaf)
          Override this method to provide an icon for the renderer.
 java.awt.Component getListCellRendererComponent(javax.swing.JList list, java.lang.Object value, int index, boolean isSelected, boolean cellHasFocus)
          Returns the component from getRendererForContext.
 java.awt.Component getRendererForContext(javax.swing.JComponent container, java.lang.Object value, int row, int column, boolean isSelected, boolean hasFocus, boolean isExpanded, boolean isLeaf)
          This default implementation returns a JPanel that is configured by calling configureComponentForContext.
 java.lang.String getStringForContext(javax.swing.JComponent container, java.lang.Object value, int row, int column, boolean isSelected, boolean hasFocus, boolean isExpanded, boolean isLeaf)
          Override this method to provide a string for the renderer.
 java.awt.Component getTableCellEditorComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, int row, int column)
          Returns getEditorForContext with the same parameters with hasFocus true.
 java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column)
          Returns the component from getRendererForContext.
 java.lang.String getText()
          Returns the text string currently displayed in the editor component.
 java.awt.Component getTreeCellEditorComponent(javax.swing.JTree tree, java.lang.Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
          Returns the component from getEditorForContext with hasFocus true.
 java.awt.Component getTreeCellRendererComponent(javax.swing.JTree tree, java.lang.Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
          Returns the component from getRendererForContext.
 boolean isCellEditable(java.util.EventObject anEvent)
          Returns true.
 boolean isClickable()
          Returns whether the editor component's icon is clickable.
 boolean isEditable()
          Returns whether the editor component's label text is editable.
 void mouseClicked(java.awt.event.MouseEvent e)
          Passes through editor mouse clicks to last known component.
 void mouseEntered(java.awt.event.MouseEvent e)
          Does nothing.
 void mouseExited(java.awt.event.MouseEvent e)
          Cancels cell editing.
 void mousePressed(java.awt.event.MouseEvent e)
          Passes through editor right-mouse (popup trigger) mouse events to last known component.
 void mouseReleased(java.awt.event.MouseEvent e)
          Does nothing.
 void removeActionListener(java.awt.event.ActionListener aListener)
          Removes the specified listener from the list of listeners to be notified when the button receives a click.
 void removeCellEditorListener(javax.swing.event.CellEditorListener aListener)
          Removes the specified listener from the list of listeners to be notified when the table receives a double click.
 void run()
          Fires the action event to all listeners.
 void setActionCommand(java.lang.String anActionCommand)
          Sets the action command broadcast when this table receives a double click.
 void setClickable(boolean isClickable)
          Sets whether the editor component's icon is clickable.
 void setEditable(boolean isEditable)
          Sets whether the editor component's label text is editable.
 void setIcon(javax.swing.Icon anIcon)
          Sets the icon currently displayed in the editor component.
 void setText(java.lang.String aString)
          Sets the text string displayed in the editor component.
 boolean shouldSelectCell(java.util.EventObject anEvent)
          Returns true.
 boolean stopCellEditing()
          Fires an editing stopped event and returns true.
 
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, 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, setEnabled, 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

CLICKED

public static final java.lang.String CLICKED
See Also:
Constant Field Values

rendererPanel

protected javax.swing.JPanel rendererPanel
The panel that is re-used to render everything. This is returned by getRendererForContext.


rendererLabel

protected javax.swing.JLabel rendererLabel

rendererButton

protected javax.swing.JButton rendererButton

editorPanel

protected javax.swing.JPanel editorPanel
The panel that is used to receive mouse clicks. It must be a different component from rendererPanel. This is returned by getEditorForContext.


editorLabel

protected javax.swing.JLabel editorLabel

editorButton

protected javax.swing.JButton editorButton
Constructor Detail

IconCellRenderer

public IconCellRenderer()
Default constructor.

Method Detail

getText

public java.lang.String getText()
Returns the text string currently displayed in the editor component.


setText

public void setText(java.lang.String aString)
Sets the text string displayed in the editor component. Default is an empty string.


getIcon

public javax.swing.Icon getIcon()
Returns the icon currently displayed in the editor component.


setIcon

public void setIcon(javax.swing.Icon anIcon)
Sets the icon currently displayed in the editor component. Default is null.


isEditable

public boolean isEditable()
Returns whether the editor component's label text is editable.


setEditable

public void setEditable(boolean isEditable)
Sets whether the editor component's label text is editable. Default is true. Editable text is not yet implemented.


isClickable

public boolean isClickable()
Returns whether the editor component's icon is clickable.


setClickable

public void setClickable(boolean isClickable)
Sets whether the editor component's icon is clickable. Default is true.


getListCellRendererComponent

public java.awt.Component getListCellRendererComponent(javax.swing.JList list,
                                                       java.lang.Object value,
                                                       int index,
                                                       boolean isSelected,
                                                       boolean cellHasFocus)
Returns the component from getRendererForContext.

Specified by:
getListCellRendererComponent in interface javax.swing.ListCellRenderer

getTableCellRendererComponent

public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table,
                                                        java.lang.Object value,
                                                        boolean isSelected,
                                                        boolean hasFocus,
                                                        int row,
                                                        int column)
Returns the component from getRendererForContext.

Specified by:
getTableCellRendererComponent in interface javax.swing.table.TableCellRenderer

getTreeCellRendererComponent

public java.awt.Component getTreeCellRendererComponent(javax.swing.JTree tree,
                                                       java.lang.Object value,
                                                       boolean selected,
                                                       boolean expanded,
                                                       boolean leaf,
                                                       int row,
                                                       boolean hasFocus)
Returns the component from getRendererForContext.

Specified by:
getTreeCellRendererComponent in interface javax.swing.tree.TreeCellRenderer

getTableCellEditorComponent

public java.awt.Component getTableCellEditorComponent(javax.swing.JTable table,
                                                      java.lang.Object value,
                                                      boolean isSelected,
                                                      int row,
                                                      int column)
Returns getEditorForContext with the same parameters with hasFocus true.

Specified by:
getTableCellEditorComponent in interface javax.swing.table.TableCellEditor

getTreeCellEditorComponent

public java.awt.Component getTreeCellEditorComponent(javax.swing.JTree tree,
                                                     java.lang.Object value,
                                                     boolean isSelected,
                                                     boolean expanded,
                                                     boolean leaf,
                                                     int row)
Returns the component from getEditorForContext with hasFocus true.

Specified by:
getTreeCellEditorComponent in interface javax.swing.tree.TreeCellEditor

getRendererForContext

public java.awt.Component getRendererForContext(javax.swing.JComponent container,
                                                java.lang.Object value,
                                                int row,
                                                int column,
                                                boolean isSelected,
                                                boolean hasFocus,
                                                boolean isExpanded,
                                                boolean isLeaf)
This default implementation returns a JPanel that is configured by calling configureComponentForContext.

Returns:
An component that is used to render content.

getEditorForContext

public java.awt.Component getEditorForContext(javax.swing.JComponent container,
                                              java.lang.Object value,
                                              int row,
                                              int column,
                                              boolean isSelected,
                                              boolean hasFocus,
                                              boolean isExpanded,
                                              boolean isLeaf)
This method returns a separate component that should be visually identical to the renderer component. We can't simply reuse the renderer component because the renderer is still used to paint the table while the editor component is displayed. Clicks are received on this component. This default implementation returns a JPanel that is configured by calling configureComponentForContext.

Returns:
A component used to receive clicks on the cell.

configureComponentForContext

protected void configureComponentForContext(javax.swing.JPanel component,
                                            javax.swing.JButton iconButton,
                                            javax.swing.JLabel label,
                                            javax.swing.JComponent container,
                                            java.lang.Object value,
                                            int row,
                                            int column,
                                            boolean isSelected,
                                            boolean hasFocus,
                                            boolean isExpanded,
                                            boolean isLeaf)
Called to configure components


getIconForContext

public javax.swing.Icon getIconForContext(javax.swing.JComponent container,
                                          java.lang.Object value,
                                          int row,
                                          int column,
                                          boolean isSelected,
                                          boolean hasFocus,
                                          boolean isExpanded,
                                          boolean isLeaf)
Override this method to provide an icon for the renderer. This default implementation returns null.

Returns:
An icon to be displayed in the cell, or null to omit the icon from the cell.

getStringForContext

public java.lang.String getStringForContext(javax.swing.JComponent container,
                                            java.lang.Object value,
                                            int row,
                                            int column,
                                            boolean isSelected,
                                            boolean hasFocus,
                                            boolean isExpanded,
                                            boolean isLeaf)
Override this method to provide a string for the renderer. This default implementation returns toString on the value parameter, or null if the value is null.

Returns:
A string to be displayed in the cell.

addActionListener

public void addActionListener(java.awt.event.ActionListener aListener)
Adds the specified listener to the list of listeners to be notified when the button receives a click.


removeActionListener

public void removeActionListener(java.awt.event.ActionListener aListener)
Removes the specified listener from the list of listeners to be notified when the button receives a click.


fireActionEvent

protected void fireActionEvent(java.awt.event.ActionEvent anActionEvent)
Broadcasts the specified action event to all listeners.


getActionCommand

public java.lang.String getActionCommand()
Returns the action command broadcast when this icon receives a click. Defaults to CLICKED.


setActionCommand

public void setActionCommand(java.lang.String anActionCommand)
Sets the action command broadcast when this table receives a double click.


getCellEditorValue

public java.lang.Object getCellEditorValue()
Returns lastKnownValue, although this should not be called.

Specified by:
getCellEditorValue in interface javax.swing.CellEditor

isCellEditable

public boolean isCellEditable(java.util.EventObject anEvent)
Returns true.

Specified by:
isCellEditable in interface javax.swing.CellEditor

shouldSelectCell

public boolean shouldSelectCell(java.util.EventObject anEvent)
Returns true.

Specified by:
shouldSelectCell in interface javax.swing.CellEditor

stopCellEditing

public boolean stopCellEditing()
Fires an editing stopped event and returns true.

Specified by:
stopCellEditing in interface javax.swing.CellEditor

cancelCellEditing

public void cancelCellEditing()
Fires an editing cancelled event and returns true.

Specified by:
cancelCellEditing in interface javax.swing.CellEditor

addCellEditorListener

public void addCellEditorListener(javax.swing.event.CellEditorListener aListener)
Adds the specified listener to the list of listeners to be notified when the table receives a double click.

Specified by:
addCellEditorListener in interface javax.swing.CellEditor

removeCellEditorListener

public void removeCellEditorListener(javax.swing.event.CellEditorListener aListener)
Removes the specified listener from the list of listeners to be notified when the table receives a double click.

Specified by:
removeCellEditorListener in interface javax.swing.CellEditor

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent evt)
Puts ourself on the end of the event queue for firing our action event to all listeners.

Specified by:
actionPerformed in interface java.awt.event.ActionListener

run

public void run()
Fires the action event to all listeners. This is triggered by a click on the icon.

Specified by:
run in interface java.lang.Runnable

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Passes through editor mouse clicks to last known component. (left click only)

Specified by:
mouseClicked in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Passes through editor right-mouse (popup trigger) mouse events to last known component. Needed for possible displaying of popup menus on right click

Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Does nothing.

Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Does nothing.

Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Cancels cell editing.

Specified by:
mouseExited in interface java.awt.event.MouseListener


Copyright © 2006 null. All Rights Reserved.