org.fife.ui.modifiabletable
Class ModifiableTable

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.fife.ui.modifiabletable.ModifiableTable
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible

public class ModifiableTable
extends javax.swing.JPanel

A table with "Add," "Remove," and possibly "Modify" buttons.

Basically, the caller provides the table model for the data, along with a "handler" that is called when adding/editing/removing data from the model. When you want to retrieve the data back from the table, you can get it via getDataVector().

Version:
1.0
Author:
Robert Futrell
See Also:
ModifiableTableListener, 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.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
static int ADD
           
static int ADD_REMOVE
           
static int ADD_REMOVE_MODIFY
           
static int ALL_BUTTONS
           
static java.lang.String BOTTOM
           
static java.lang.String LEFT
           
static int MODIFY
           
static int MOVE_UP_DOWN
           
static java.lang.String PROPERTY_PANELS_NON_OPAQUE
          If this system property is set to true, then the panels created containing the buttons ("Add", "Modify", "Remove", etc.) will be non-opaque.
static int REMOVE
           
static java.lang.String RIGHT
           
static java.lang.String TOP
           
 
Fields inherited from class javax.swing.JComponent
accessibleContext, 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
ModifiableTable(javax.swing.table.DefaultTableModel model)
          Creates a ModifiableTable with Add, Remove, and Modify buttons below the table.
ModifiableTable(javax.swing.table.DefaultTableModel model, java.lang.Object[] columnNames)
          Creates a ModifiableTable with Add, Remove, and Modify buttons below the table.
ModifiableTable(javax.swing.table.DefaultTableModel model, java.lang.Object[] columnNames, java.lang.String buttonLocation, int buttons)
          Constructor.
ModifiableTable(javax.swing.table.DefaultTableModel model, java.lang.Object[] columnNames, java.lang.String buttonLocation, int buttons, java.util.List<? extends javax.swing.Action> customButtons)
          Constructor.
ModifiableTable(javax.swing.table.DefaultTableModel model, java.lang.String buttonLocation, int buttons)
          Constructor.
ModifiableTable(javax.swing.table.DefaultTableModel model, java.lang.String buttonLocation, int buttons, java.util.List<? extends javax.swing.Action> customButtons)
          Constructor.
 
Method Summary
 void addModifiableTableListener(ModifiableTableListener l)
          Adds a listener to this modifiable table.
protected  void addRow()
          Gets information from the user on a new row to add and adds it to the table.
protected  javax.swing.JPanel createButtonPanel(java.lang.String buttonLocation, int buttons, java.util.List<? extends javax.swing.Action> customButtons)
          Returns the panel of buttons for modifying the table.
protected  void fireModifiableTableEvent(int change, int row)
          Notifies all listeners of a row being added, removed, or modified.
 java.util.Vector<?> getDataVector()
          Retrieves the data stored in the table as a Vector of Vectors.
 int getFirstMovableRow()
          Returns the "first movable row."
 int getSelectedRow()
          Returns the currently selected row.
 javax.swing.JTable getTable()
          Returns the table displayed in this modifiable table.
protected  void modifyRow()
          Modifies the contents of a row in the table.
protected  void moveRow(int amt)
          Swaps the selected row with another.
 void removeModifiableTableListener(ModifiableTableListener l)
          Removes a listener from this modifiable table.
protected  void removeRow()
          Removes the contents of the selected row in the table.
 void setEnabled(boolean enabled)
          Toggles whether this modifiable table is enabled.
 void setFirstMovableRow(int firstMovableRow)
          Sets the "first movable row."
 void setRowHandler(RowHandler handler)
          Sets the handler used when the user adds or modifies a row in the table.
 void updateUI()
          Updates the Look and Feel of this table.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI
 
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, 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, 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

ADD

public static final int ADD
See Also:
Constant Field Values

REMOVE

public static final int REMOVE
See Also:
Constant Field Values

MODIFY

public static final int MODIFY
See Also:
Constant Field Values

ADD_REMOVE

public static final int ADD_REMOVE
See Also:
Constant Field Values

ADD_REMOVE_MODIFY

public static final int ADD_REMOVE_MODIFY
See Also:
Constant Field Values

MOVE_UP_DOWN

public static final int MOVE_UP_DOWN
See Also:
Constant Field Values

ALL_BUTTONS

public static final int ALL_BUTTONS
See Also:
Constant Field Values

TOP

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

BOTTOM

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

LEFT

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

RIGHT

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

PROPERTY_PANELS_NON_OPAQUE

public static final java.lang.String PROPERTY_PANELS_NON_OPAQUE
If this system property is set to true, then the panels created containing the buttons ("Add", "Modify", "Remove", etc.) will be non-opaque. This is useful on OS's such as Windows XP and Vista, where tabbed panes have a gradient background. By default this is false in case it messes up some other LookAndFeel.

See Also:
Constant Field Values
Constructor Detail

ModifiableTable

public ModifiableTable(javax.swing.table.DefaultTableModel model)
Creates a ModifiableTable with Add, Remove, and Modify buttons below the table.

Parameters:
model - The model to use for the table.
See Also:
setRowHandler(org.fife.ui.modifiabletable.RowHandler)

ModifiableTable

public ModifiableTable(javax.swing.table.DefaultTableModel model,
                       java.lang.Object[] columnNames)
Creates a ModifiableTable with Add, Remove, and Modify buttons below the table.

Parameters:
model - The model to use for the table.
columnNames - Objects to use for names of the columns. This value can be null if the model already takes care of its column names.
See Also:
setRowHandler(org.fife.ui.modifiabletable.RowHandler)

ModifiableTable

public ModifiableTable(javax.swing.table.DefaultTableModel model,
                       java.lang.String buttonLocation,
                       int buttons)
Constructor.

Parameters:
model - The model to use for the table.
buttonLocation - The location of the buttons, relative to the table.
buttons - A bit flag representing what buttons to display.
See Also:
setRowHandler(RowHandler), ADD_REMOVE, ADD_REMOVE_MODIFY, MOVE_UP_DOWN, ALL_BUTTONS

ModifiableTable

public ModifiableTable(javax.swing.table.DefaultTableModel model,
                       java.lang.String buttonLocation,
                       int buttons,
                       java.util.List<? extends javax.swing.Action> customButtons)
Constructor.

Parameters:
model - The model to use for the table.
buttonLocation - The location of the buttons, relative to the table.
buttons - A bit flag representing what buttons to display.
customButtons - A list of Actions for "extra" buttons to add. This may be null if no custom buttons are to be added.
See Also:
setRowHandler(RowHandler), ADD_REMOVE, ADD_REMOVE_MODIFY, MOVE_UP_DOWN, ALL_BUTTONS

ModifiableTable

public ModifiableTable(javax.swing.table.DefaultTableModel model,
                       java.lang.Object[] columnNames,
                       java.lang.String buttonLocation,
                       int buttons)
Constructor.

Parameters:
model - The model to use for the table.
columnNames - Objects to use for names of the columns. This value can be null if the model already takes care of its column names.
buttonLocation - The location of the buttons, relative to the table.
buttons - A bit flag representing what buttons to display.
See Also:
setRowHandler(org.fife.ui.modifiabletable.RowHandler)

ModifiableTable

public ModifiableTable(javax.swing.table.DefaultTableModel model,
                       java.lang.Object[] columnNames,
                       java.lang.String buttonLocation,
                       int buttons,
                       java.util.List<? extends javax.swing.Action> customButtons)
Constructor.

Parameters:
model - The model to use for the table.
columnNames - Objects to use for names of the columns. This value can be null if the model already takes care of its column names.
buttonLocation - The location of the buttons, relative to the table.
buttons - A bit flag representing what buttons to display.
customButtons - A list of Actions for "extra" buttons to add. This may be null if no custom buttons are to be added.
See Also:
setRowHandler(org.fife.ui.modifiabletable.RowHandler)
Method Detail

addModifiableTableListener

public void addModifiableTableListener(ModifiableTableListener l)
Adds a listener to this modifiable table.

Parameters:
l - The listener to add.
See Also:
removeModifiableTableListener(org.fife.ui.modifiabletable.ModifiableTableListener)

addRow

protected void addRow()
Gets information from the user on a new row to add and adds it to the table. This method is called whenever the user presses the Add button.

See Also:
modifyRow(), removeRow()

createButtonPanel

protected javax.swing.JPanel createButtonPanel(java.lang.String buttonLocation,
                                               int buttons,
                                               java.util.List<? extends javax.swing.Action> customButtons)
Returns the panel of buttons for modifying the table.

Parameters:
buttonLocation - The location of the buttons, relative to the table.
buttons - A bit flag representing what buttons to display.
customButtons - A list of Actions for "extra" buttons to add. This may be null if no custom buttons are to be added.
Returns:
The panel of buttons.

fireModifiableTableEvent

protected void fireModifiableTableEvent(int change,
                                        int row)
Notifies all listeners of a row being added, removed, or modified.

Parameters:
change - The change that occurred.
row - The row that was added, removed or modified.

getDataVector

public java.util.Vector<?> getDataVector()
Retrieves the data stored in the table as a Vector of Vectors. Each Vector inside of the returned Vector represents a single row.

This method is simply a wrapper for ((DefaultTableModel)getTable().getModel()).getDataVector().

Returns:
The data in the table.

getFirstMovableRow

public int getFirstMovableRow()
Returns the "first movable row." If this property is set to a value greater than 0, then the rows 0 through getFirstMovableRow()-1 can not be moved up or down, and other rows cannot be moved up into them.

This property is ignored if this modifiable table is not displaying the "move up" and "move down" buttons.

Returns:
The first movable row.
See Also:
setFirstMovableRow(int)

getSelectedRow

public int getSelectedRow()
Returns the currently selected row.

Returns:
The currently selected row, or -1 if no row is selected.

getTable

public javax.swing.JTable getTable()
Returns the table displayed in this modifiable table.

Returns:
The table.

modifyRow

protected void modifyRow()
Modifies the contents of a row in the table. This method is called whenever the user presses the Modify button.

See Also:
addRow(), removeRow()

moveRow

protected void moveRow(int amt)
Swaps the selected row with another. If the selected row cannot be moved the amount specified, nothing happens.

Parameters:
amt - The offset between the selected row and the row to swap it with; e.g. "1" or "-1".

removeModifiableTableListener

public void removeModifiableTableListener(ModifiableTableListener l)
Removes a listener from this modifiable table.

Parameters:
l - The listener to remove.
See Also:
addModifiableTableListener(org.fife.ui.modifiabletable.ModifiableTableListener)

removeRow

protected void removeRow()
Removes the contents of the selected row in the table. This method is called whenever the user presses the Remove button.

See Also:
addRow(), modifyRow()

setEnabled

public void setEnabled(boolean enabled)
Toggles whether this modifiable table is enabled.

Overrides:
setEnabled in class javax.swing.JComponent
Parameters:
enabled - Whether this modifiable table is enabled.

setFirstMovableRow

public void setFirstMovableRow(int firstMovableRow)
Sets the "first movable row." If this property is set to a value greater than 0, then the rows 0 through getFirstMovableRow()-1 can not be moved up or down, and other rows cannot be moved up into them.

Typically, if this is modified, you'll also want to ensure your RowHandler forbids the removal of the unmovable rows.

This property is ignored if this modifiable table is not displaying the "move up" and "move down" buttons.

Parameters:
firstMovableRow - The first movable row. This should be greater than 0.
See Also:
getFirstMovableRow()

setRowHandler

public void setRowHandler(RowHandler handler)
Sets the handler used when the user adds or modifies a row in the table.

Parameters:
handler - The new handler.
See Also:
RowHandler

updateUI

public void updateUI()
Updates the Look and Feel of this table. Overridden to also update the LaF of the RowHandler, if it is a Swing component.

Overrides:
updateUI in class javax.swing.JPanel