stanford.cs106.gui
Class JStringList

Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JList
                  extended by stanford.cs106.gui.JStringList
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible, javax.swing.Scrollable

public class JStringList
extends javax.swing.JList

Represents a list of strings with optional colors.

Constructor Summary
JStringList()
          Constructs a new empty list of strings.
 
Method Summary
 void addActionListener(ActionListener listener)
          Attaches the given listener to this list of strings so that it will be notified when the user clicks on items in the list.
 void addItem(String s)
          Appends the given string to the end of this list, using the default black color.
 void addItem(String s, Color color)
          Appends the given string to the end of this list, using the given color.
 void addItems(String[] items)
          Adds all of the items from the given array to this list, in black.
 void addItems(String[] items, Color[] colors)
          Adds all of the items from the given array to this list, in the given colors.
 void clear()
          Removes all strings from this list.
 String getActionCommand()
          Returns the current action command of this list, which is the string that will occur when action events are performed on this list.
 Color getColor(int index)
          Returns the color of the item in the list at the given index.
 String getItem(int index)
          Returns the text of the item in the list at the given index.
 int getItemCount()
          Returns the number of items in the list.
 DefaultListModel getModel()
          Returns a reference to the underlying data model of this list.
 String getSelectedValue()
          Returns the string that is currently selected in this list.
 Color getSelectedValueColor()
          Returns the color of the string that is currently selected in this list, or null if no string is selected.
 void removeActionListener(ActionListener listener)
          Detaches the given listener so that it will not be notified of future action events that occur in this list.
 void removeItem(int index)
          Removes the element at the given index from this list, shifting others down an index if necessary.
 void removeItem(String s)
          Removes the first occurrence of the given string element at the given index from this list, shifting others down an index if necessary.
 void setActionCommand(String actionCommand)
          Sets the current action command of this list, which is the string that will occur when action events are performed on this list.
 void setColor(int index, Color color)
          Sets the color of the item at the given index to the given color.
 void setColors(Color[] colors)
          Sets the items of this list to use the colors from the given array.
 void setItem(int index, String s)
          Sets the item at the given index to store the given text.
 void setItem(int index, String s, Color color)
          Sets the item at the given index to store the given text in the given color.
 void setItems(String[] items, Color[] colors)
          Sets the list contents to be the given items in the given colors.
 
Methods inherited from class javax.swing.JList
addListSelectionListener, addSelectionInterval, clearSelection, createSelectionModel, ensureIndexIsVisible, fireSelectionValueChanged, getAccessibleContext, getAnchorSelectionIndex, getCellBounds, getCellRenderer, getDragEnabled, getDropLocation, getDropMode, getFirstVisibleIndex, getFixedCellHeight, getFixedCellWidth, getLastVisibleIndex, getLayoutOrientation, getLeadSelectionIndex, getListSelectionListeners, getMaxSelectionIndex, getMinSelectionIndex, getNextMatch, getPreferredScrollableViewportSize, getPrototypeCellValue, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedIndex, getSelectedIndices, getSelectedValues, getSelectionBackground, getSelectionForeground, getSelectionMode, getSelectionModel, getToolTipText, getUI, getUIClassID, getValueIsAdjusting, getVisibleRowCount, indexToLocation, isSelectedIndex, isSelectionEmpty, locationToIndex, paramString, removeListSelectionListener, removeSelectionInterval, setCellRenderer, setDragEnabled, setDropMode, setFixedCellHeight, setFixedCellWidth, setLayoutOrientation, setListData, setListData, setModel, setPrototypeCellValue, setSelectedIndex, setSelectedIndices, setSelectedValue, setSelectionBackground, setSelectionForeground, setSelectionInterval, setSelectionMode, setSelectionModel, setUI, setValueIsAdjusting, setVisibleRowCount, 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, 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, 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, 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 Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JStringList

public JStringList()
Constructs a new empty list of strings.

Method Detail

addActionListener

public void addActionListener(ActionListener listener)
Attaches the given listener to this list of strings so that it will be notified when the user clicks on items in the list.

Parameters:
listener - the listener to attach
Throws:
NullPointerException - if the listener is null.

addItem

public void addItem(String s)
Appends the given string to the end of this list, using the default black color.

Parameters:
s - the string to add
Throws:
NullPointerException - if s is null.

addItem

public void addItem(String s,
                    Color color)
Appends the given string to the end of this list, using the given color.

Parameters:
s - the string to add
color - the color in which to display the text
Throws:
NullPointerException - if s or color are null.

addItems

public void addItems(String[] items)
Adds all of the items from the given array to this list, in black.

Parameters:
items - The array of items to add.
Throws:
NullPointerException - if the array or any of its elements is null.

addItems

public void addItems(String[] items,
                     Color[] colors)
Adds all of the items from the given array to this list, in the given colors. They are treated as parallel arrays; item 0 is added with color 0, item 1 is added with color 1, and so on. If the colors array is shorter, any extra strings are added in black. If the colors array is longer, any extra colors in it are ignored.

Parameters:
items - The array of items to add.
colors - The array of colors to use.
Throws:
NullPointerException - if either array or any of their elements is null.

clear

public void clear()
Removes all strings from this list.


getActionCommand

public String getActionCommand()
Returns the current action command of this list, which is the string that will occur when action events are performed on this list. Initially, the action command is null.

Returns:
the action command; initially null

getColor

public Color getColor(int index)
Returns the color of the item in the list at the given index.

Parameters:
index - the 0-based index of the item.
Returns:
the color at the given index.
Throws:
ArrayIndexOutOfBoundsException - If the index is outside the range of this list (0 .. size-1).

getItem

public String getItem(int index)
Returns the text of the item in the list at the given index.

Parameters:
index - the 0-based index of the item.
Returns:
the item at the given index.
Throws:
ArrayIndexOutOfBoundsException - If the index is outside the range of this list (0 .. size-1).

getItemCount

public int getItemCount()
Returns the number of items in the list.


getModel

public DefaultListModel getModel()
Returns a reference to the underlying data model of this list. To client: Don't modify it.

Overrides:
getModel in class javax.swing.JList

getSelectedValue

public String getSelectedValue()
Returns the string that is currently selected in this list. Initially, or if no string is selected, returns null.

Overrides:
getSelectedValue in class javax.swing.JList

getSelectedValueColor

public Color getSelectedValueColor()
Returns the color of the string that is currently selected in this list. Initially, or if no string is selected, returns null.


removeActionListener

public void removeActionListener(ActionListener listener)
Detaches the given listener so that it will not be notified of future action events that occur in this list.

Parameters:
listener - the listener to detach
Throws:
NullPointerException - if the listener is null.

removeItem

public void removeItem(int index)
Removes the element at the given index from this list, shifting others down an index if necessary.

Parameters:
index - the index from which to remove
Throws:
ArrayIndexOutOfBoundsException - If the index is outside the range of this list (0 .. size-1).

removeItem

public void removeItem(String s)
Removes the first occurrence of the given string element at the given index from this list, shifting others down an index if necessary. If the given string is not found, the call has no effect.

Parameters:
s - the string to remove
Throws:
NullPointerException - if the listener is null.

setActionCommand

public void setActionCommand(String actionCommand)
Sets the current action command of this list, which is the string that will occur when action events are performed on this list. Initially, the action command is null.

Parameters:
actionCommand - the new command string to use

setColor

public void setColor(int index,
                     Color color)
Sets the color of the item at the given index to the given color. The item's text is unchanged.

Parameters:
index - The index to modify.
color - The color to put there.
Throws:
ArrayIndexOutOfBoundsException - If the index is outside the range of this list (0 .. size-1).
NullPointerException - if the color is null.

setColors

public void setColors(Color[] colors)
Sets the items of this list to use the colors from the given array. List item 0 will use array color 0, and so on. If the array is shorter than the length of this list, items after the array length are untouched. If the array is longer than the length of this list, items in the array past the length of the list are ignored.

Parameters:
colors - The array of colors to use.
Throws:
NullPointerException - if the array or any of its elements is null.

setItem

public void setItem(int index,
                    String s)
Sets the item at the given index to store the given text. The item's color is unchanged.

Parameters:
index - The index to modify.
s - The text to put there.
Throws:
ArrayIndexOutOfBoundsException - If the index is outside the range of this list (0 .. size-1).
NullPointerException - if s is null.

setItem

public void setItem(int index,
                    String s,
                    Color color)
Sets the item at the given index to store the given text in the given color.

Parameters:
index - The index to modify.
s - The text to put there.
Throws:
ArrayIndexOutOfBoundsException - If the index is outside the range of this list (0 .. size-1).
NullPointerException - if s or color is null.

setItems

public void setItems(String[] items,
                     Color[] colors)
Sets the list contents to be the given items in the given colors.

Parameters:
items - Array of items to add.
colors - Array of colors to use for each item.
Throws:
NullPointerException - if either array or any of their elements is null.