iglu.jdbc
Class JDBCComboBoxModel

java.lang.Object
  |
  +--javax.swing.AbstractListModel
        |
        +--iglu.jdbc.JDBCComboBoxModel
All Implemented Interfaces:
javax.swing.ComboBoxModel, javax.swing.ListModel, java.io.Serializable
Direct Known Subclasses:
SQLComboBoxModel

public class JDBCComboBoxModel
extends javax.swing.AbstractListModel
implements javax.swing.ComboBoxModel

implements a comboboxmodel so that the user can drop result sets into list or combo boxes. Assume that the last column is an integer which uniquely identifies a row in the table. Will also work as a ListModel.

Version:
1.0
Author:
Travis Bauer trbauer@indiana.edu
See Also:
Serialized Form

Nested Class Summary
static class JDBCComboBoxModel.ListItem
          This is how the items are stored in the list internally.
 
Field Summary
protected  int[] displayCols
          Which columns to display.
protected  java.sql.ResultSet rs
          The result set which provides the information for this model.
protected  JDBCComboBoxModel.ListItem selectedItem
          Which item is currently selected.
protected  int size
          The number of items in the result set.
 
Fields inherited from class javax.swing.AbstractListModel
listenerList
 
Constructor Summary
JDBCComboBoxModel()
          Don't do anything
JDBCComboBoxModel(java.sql.ResultSet nrs, int[] ndisplayCols)
          Create a comboBox Model from the given data.
 
Method Summary
protected  void figureSize()
          figure out how many items there are.
 java.lang.Object getElementAt(int index)
          Get the element at index.
 java.lang.Object getSelectedItem()
          Return the selected item
 java.lang.String getSelectedString(int colnum)
          Get the string of the currently selected item.
 int getSize()
          Get the number of items
 void setSelectedItem(java.lang.Object anItem)
          set the selected item.
 
Methods inherited from class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.ListModel
addListDataListener, removeListDataListener
 

Field Detail

rs

protected java.sql.ResultSet rs
The result set which provides the information for this model.


displayCols

protected int[] displayCols
Which columns to display. If you want columns 3, 4, and 6 to show up in the combo box, then set this to new int[] {3, 4, 6}. You do this in the constructor. Setting the columns to display does not affect you ability to see the rest of the columns in the ResultSet.


size

protected int size
The number of items in the result set. Added for efficiency.


selectedItem

protected JDBCComboBoxModel.ListItem selectedItem
Which item is currently selected.

Constructor Detail

JDBCComboBoxModel

public JDBCComboBoxModel()
Don't do anything


JDBCComboBoxModel

public JDBCComboBoxModel(java.sql.ResultSet nrs,
                         int[] ndisplayCols)
Create a comboBox Model from the given data.

Parameters:
nrs - The result set to use.
ndisplayCols - The columns to display. See displayCols for more information.
Method Detail

getElementAt

public java.lang.Object getElementAt(int index)
Get the element at index. Remember that JDBC ResultSets are 1-based.

Specified by:
getElementAt in interface javax.swing.ListModel
Parameters:
index -

getSize

public int getSize()
Get the number of items

Specified by:
getSize in interface javax.swing.ListModel

setSelectedItem

public void setSelectedItem(java.lang.Object anItem)
set the selected item. Object needs to be an instance of ListItem, also defined in this class.

Specified by:
setSelectedItem in interface javax.swing.ComboBoxModel
Parameters:
anItem -

getSelectedItem

public java.lang.Object getSelectedItem()
Return the selected item

Specified by:
getSelectedItem in interface javax.swing.ComboBoxModel

getSelectedString

public java.lang.String getSelectedString(int colnum)
Get the string of the currently selected item. This can be any column in the result set.

Parameters:
colnum -

figureSize

protected void figureSize()
figure out how many items there are.