org.gjt.btools.gui.component
Class ButtonRenderer

java.lang.Object
  |
  +--javax.swing.AbstractCellEditor
        |
        +--org.gjt.btools.gui.component.ButtonRenderer
All Implemented Interfaces:
java.awt.event.ActionListener, javax.swing.CellEditor, java.util.EventListener, java.io.Serializable, javax.swing.table.TableCellEditor, javax.swing.table.TableCellRenderer

public class ButtonRenderer
extends javax.swing.AbstractCellEditor
implements javax.swing.table.TableCellRenderer, javax.swing.table.TableCellEditor, java.awt.event.ActionListener

A button that is used as a table cell. This component is actually implemented both as a cell renderer and a cell editor. Because the user must interact directly with this component, it must be registered both as a renderer and an editor for the appropriate table cells and the table model must declare that such cells be editable, regardless of whether the table contents will ever be changed.

To listen for button presses, you can simply register an action listener with this object; there is no need to access the underlying JButtons. An action event will be fired when the table cell button is pressed, and the corresponding routine that is called (at the listener's end) should end with a call to either stopCellEditing() (to save any changes to the table) or to cancelCellEditing() (to make no changes to the table).

If changes are to be made to the table, routine getCellEditorValue() must be overridden to return a new value for the table cell.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.AbstractCellEditor
changeEvent, listenerList
 
Constructor Summary
ButtonRenderer(java.lang.String text)
          Creates a new button renderer/editor displaying the given text.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          For internal use only.
 void addActionListener(java.awt.event.ActionListener l)
          Registers the given listener to listen for action events coming from this object.
 int getCellEditorColumn()
          Returns the column containing the cell editor.
 int getCellEditorRow()
          Returns the row containing the cell editor.
 java.lang.Object getCellEditorValue()
          Returns the value contained in the cell editor.
 javax.swing.JButton getEditorButton()
          Returns the button used for cell editing.
 javax.swing.JButton getRendererButton()
          Returns the button used for cell rendering.
 java.awt.Component getTableCellEditorComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, int row, int column)
          Returns the component that does the editing.
 java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column)
          Returns the component that does the rendering.
 void removeActionListener(java.awt.event.ActionListener l)
          Unregisters the given listener so it no longer listens for action events coming from this object.
 void setBorder(javax.swing.border.Border border)
          Sets the border to use for both the rendering and editing buttons.
 void setText(java.lang.String text)
          Sets the text to display on both the rendering and editing buttons.
 boolean shouldSelectCell(java.util.EventObject e)
          Prepares the component for editing as requested by the given event.
 
Methods inherited from class javax.swing.AbstractCellEditor
addCellEditorListener, cancelCellEditing, fireEditingCanceled, fireEditingStopped, getCellEditorListeners, isCellEditable, removeCellEditorListener, stopCellEditing
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.CellEditor
addCellEditorListener, cancelCellEditing, isCellEditable, removeCellEditorListener, stopCellEditing
 

Constructor Detail

ButtonRenderer

public ButtonRenderer(java.lang.String text)
Creates a new button renderer/editor displaying the given text.

Parameters:
text - the text to place on the button.
Method Detail

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 that does the rendering. This implementation ignores the value of the table cell and simply returns this button.

Specified by:
getTableCellRendererComponent in interface javax.swing.table.TableCellRenderer
Parameters:
table - the table in which a cell is being rendered.
value - the value of the table cell being rendered.
isSelected - whether or not the table cell is currently selected.
hasFocus - whether or not the table cell currently has the focus.
row - the row of the table cell being rendered.
column - the column of the table cell being rendered.
Returns:
the component that does the rendering.

getTableCellEditorComponent

public java.awt.Component getTableCellEditorComponent(javax.swing.JTable table,
                                                      java.lang.Object value,
                                                      boolean isSelected,
                                                      int row,
                                                      int column)
Returns the component that does the editing. This implementation ignores the value of the table cell and simply returns the editor button.

Specified by:
getTableCellEditorComponent in interface javax.swing.table.TableCellEditor
Parameters:
table - the table in which a cell is being edited.
value - the value of the table cell being edited.
isSelected - whether or not the table cell is currently selected.
row - the row of the table cell being edited.
column - the column of the table cell being edited.
Returns:
the component that does the editing.

getCellEditorRow

public int getCellEditorRow()
Returns the row containing the cell editor.

Returns:
the row containing the cell editor.

getCellEditorColumn

public int getCellEditorColumn()
Returns the column containing the cell editor.

Returns:
the column containing the cell editor.

getCellEditorValue

public java.lang.Object getCellEditorValue()
Returns the value contained in the cell editor. This implementation simply returns the value of the table cell being edited.

Specified by:
getCellEditorValue in interface javax.swing.CellEditor
Returns:
the value contained in the cell editor.

getRendererButton

public javax.swing.JButton getRendererButton()
Returns the button used for cell rendering. Note that this is not the button used for cell editing. Generally you should not need to call this function.

Returns:
the button used for cell rendering.
See Also:
getEditorButton()

getEditorButton

public javax.swing.JButton getEditorButton()
Returns the button used for cell editing. Note that this is not the button used for cell rendering. Generally you should not need to call this function.

Returns:
the button used for cell editing.
See Also:
getRendererButton()

setText

public void setText(java.lang.String text)
Sets the text to display on both the rendering and editing buttons.

Note that there are two buttons used by this class; one that renders table cells and one that the user interacts with when editing table cells. This routine affects both of these buttons.

Parameters:
text - the text to display on both buttons.

setBorder

public void setBorder(javax.swing.border.Border border)
Sets the border to use for both the rendering and editing buttons.

Note that there are two buttons used by this class; one that renders table cells and one that the user interacts with when editing table cells. This routine affects both of these buttons.

Parameters:
border - the border to use for both buttons.

addActionListener

public void addActionListener(java.awt.event.ActionListener l)
Registers the given listener to listen for action events coming from this object. This object fires an action event when the button in the table cell is pressed.

Parameters:
l - the listener to add.

removeActionListener

public void removeActionListener(java.awt.event.ActionListener l)
Unregisters the given listener so it no longer listens for action events coming from this object. This object fires an action event when the button in the table cell is pressed.

Parameters:
l - the listener to remove.

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
For internal use only. Called when the table cell button is pressed; this routine fires a new action event to all listeners registered with this object.

Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
e - the details of the button press event.

shouldSelectCell

public boolean shouldSelectCell(java.util.EventObject e)
Prepares the component for editing as requested by the given event.

Specified by:
shouldSelectCell in interface javax.swing.CellEditor
Overrides:
shouldSelectCell in class javax.swing.AbstractCellEditor
Parameters:
e - the event that caused editing to begin.
Returns:
true if and only if the table cell being edited should be selected.


Copyright © 1998-2001, Ben Burton
This software is released under the GNU Public License.
For further information, or to submit a bug or other problem, please contact Ben Burton (bab@debian.org).