jsky.util.gui
Class SortedJTable
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JTable
jsky.util.gui.PrintableJTable
jsky.util.gui.SortedJTable
- All Implemented Interfaces:
- MouseListener, ImageObserver, MenuContainer, Printable, Serializable, EventListener, Accessible, CellEditorListener, ListSelectionListener, TableColumnModelListener, TableModelListener, Scrollable
public class SortedJTable
- extends PrintableJTable
- implements MouseListener, TableModelListener
A JTable that allows the user to sort the table by clicking on the
column headers. An icon is shown in the sort column to indicate the
sort. Ascending and descending sorting is supported.
Sorting depends on the java.lang.Comparable interface. SortedJTable
will attempt to compare the cell contents using Comparable. If the
contents are not Comparable, then their toString() values are compared.
Note that all the Java primitive wrapper objects (Integer, Double, etc)
implement Comparable, so you'll get better sorting (1, 5, 10 instead of
1, 10, 5) if you use, for example, Integers instead of Strings.
This code was developed by NASA, Goddard Space Flight Center, Code 588
for the Scientist's Expert Assistant (SEA) project.
- Version:
- 10/27/99
- Author:
- Kong Eu Tak (for Swing Connection Article), J. Jones / 588, A. Brighton (changes, additions, fixes, performance improvements)
- See Also:
Comparable
,
Serialized Form
Field Summary |
static int |
ASCENDING
Ascending sort type. |
static int |
DESCENDING
Descending sort type. |
Fields inherited from class javax.swing.JTable |
AUTO_RESIZE_ALL_COLUMNS, AUTO_RESIZE_LAST_COLUMN, AUTO_RESIZE_NEXT_COLUMN, AUTO_RESIZE_OFF, AUTO_RESIZE_SUBSEQUENT_COLUMNS, autoCreateColumnsFromModel, autoResizeMode, cellEditor, cellSelectionEnabled, columnModel, dataModel, defaultEditorsByColumnClass, defaultRenderersByColumnClass, editingColumn, editingRow, editorComp, gridColor, preferredViewportSize, rowHeight, rowMargin, rowSelectionAllowed, selectionBackground, selectionForeground, selectionModel, showHorizontalLines, showVerticalLines, tableHeader |
Constructor Summary |
SortedJTable()
Default constructor |
SortedJTable(TableModel model)
Constructs a JTable which is initialized with model as the data model,
a default column model, and a default selection model. |
Method Summary |
void |
addRow(Vector row)
Add the given row in the table. |
protected int |
compareObjects(Object a,
Object b)
Compare two objects, which may or may not be Comparables, or nulls |
protected void |
doSort()
Performs the sort. |
int |
getSortColumn()
Returns the column number of the current sort column. |
int |
getSortedRowIndex(int row)
Return the index of the given sorted row before sorting |
int |
getSortType()
Returns whether the current sort is ASCENDING or DESCENDING. |
int |
getUnsortedRowIndex(int row)
Return the index of the given row after sorting |
static void |
main(String[] args)
test main |
void |
mouseClicked(MouseEvent m)
If user clicks on column header, perform sort and toggle sort ordering
if the sort column was already the sort column. |
void |
mouseEntered(MouseEvent m)
|
void |
mouseExited(MouseEvent m)
|
void |
mousePressed(MouseEvent m)
|
void |
mouseReleased(MouseEvent m)
|
protected void |
quickSort(Object[] a,
int lo0,
int hi0)
Do a quick sort on the given array, which contains column values. |
void |
removeRows(int[] rows)
Remove the given rows from the table (assumes use of DefaultTableModel). |
protected void |
setCustomHeaderRenderer(int i)
Assigns a new CustomHeaderRenderer to a column,
preserving state of the existing header renderer. |
void |
setModel(TableModel model)
Set the table model and arrange to have it sorted when the user
clicks on the header. |
void |
setSortColumn(int column)
Sets the sort column. |
void |
setSortType(int type)
Sets the sort type. |
protected void |
sortAndUpdate()
Sorts the table, notifies the model, and redraws the header. |
Methods inherited from class javax.swing.JTable |
addColumn, addColumnSelectionInterval, addNotify, addRowSelectionInterval, changeSelection, clearSelection, columnAdded, columnAtPoint, columnMarginChanged, columnMoved, columnRemoved, columnSelectionChanged, configureEnclosingScrollPane, convertColumnIndexToModel, convertColumnIndexToView, createDefaultColumnModel, createDefaultColumnsFromModel, createDefaultDataModel, createDefaultEditors, createDefaultRenderers, createDefaultSelectionModel, createDefaultTableHeader, createScrollPaneForTable, doLayout, editCellAt, editCellAt, editingCanceled, editingStopped, getAccessibleContext, getAutoCreateColumnsFromModel, getAutoResizeMode, getCellEditor, getCellEditor, getCellRect, getCellRenderer, getCellSelectionEnabled, getColumn, getColumnClass, getColumnCount, getColumnModel, getColumnName, getColumnSelectionAllowed, getDefaultEditor, getDefaultRenderer, getDragEnabled, getEditingColumn, getEditingRow, getEditorComponent, getGridColor, getIntercellSpacing, getModel, getPreferredScrollableViewportSize, getPrintable, getRowCount, getRowHeight, getRowHeight, getRowMargin, getRowSelectionAllowed, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedColumn, getSelectedColumnCount, getSelectedColumns, getSelectedRow, getSelectedRowCount, getSelectedRows, getSelectionBackground, getSelectionForeground, getSelectionModel, getShowHorizontalLines, getShowVerticalLines, getSurrendersFocusOnKeystroke, getTableHeader, getToolTipText, getUI, getUIClassID, getValueAt, initializeLocalVars, isCellEditable, isCellSelected, isColumnSelected, isEditing, isRowSelected, moveColumn, paramString, prepareEditor, prepareRenderer, print, print, print, processKeyBinding, removeColumn, removeColumnSelectionInterval, removeEditor, removeNotify, removeRowSelectionInterval, resizeAndRepaint, rowAtPoint, selectAll, setAutoCreateColumnsFromModel, setAutoResizeMode, setCellEditor, setCellSelectionEnabled, setColumnModel, setColumnSelectionAllowed, setColumnSelectionInterval, setDefaultEditor, setDefaultRenderer, setDragEnabled, setEditingColumn, setEditingRow, setGridColor, setIntercellSpacing, setPreferredScrollableViewportSize, setRowHeight, setRowHeight, setRowMargin, setRowSelectionAllowed, setRowSelectionInterval, setSelectionBackground, setSelectionForeground, setSelectionMode, setSelectionModel, setShowGrid, setShowHorizontalLines, setShowVerticalLines, setSurrendersFocusOnKeystroke, setTableHeader, setUI, setValueAt, sizeColumnsToFit, sizeColumnsToFit, tableChanged, unconfigureEnclosingScrollPane, updateUI, valueChanged |
Methods inherited from class javax.swing.JComponent |
addAncestorListener, 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, 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, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, 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, 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 |
ASCENDING
public static final int ASCENDING
- Ascending sort type.
- See Also:
- Constant Field Values
DESCENDING
public static final int DESCENDING
- Descending sort type.
- See Also:
- Constant Field Values
SortedJTable
public SortedJTable(TableModel model)
- Constructs a JTable which is initialized with model as the data model,
a default column model, and a default selection model.
SortedJTable
public SortedJTable()
- Default constructor
setModel
public void setModel(TableModel model)
- Set the table model and arrange to have it sorted when the user
clicks on the header.
- Overrides:
setModel
in class JTable
getSortColumn
public int getSortColumn()
- Returns the column number of the current sort column.
- Returns:
- current sort column number
setSortColumn
public void setSortColumn(int column)
- Sets the sort column. The table will be resorted.
- Parameters:
column
- new sort column
getUnsortedRowIndex
public int getUnsortedRowIndex(int row)
- Return the index of the given row after sorting
getSortedRowIndex
public int getSortedRowIndex(int row)
- Return the index of the given sorted row before sorting
getSortType
public int getSortType()
- Returns whether the current sort is ASCENDING or DESCENDING.
- Returns:
- ASCENDING or DESCENDING
setSortType
public void setSortType(int type)
- Sets the sort type. Valid values are ASCENDING and DESCENDING.
- Parameters:
type
- new sort type
removeRows
public void removeRows(int[] rows)
- Remove the given rows from the table (assumes use of DefaultTableModel).
- Parameters:
rows
- array of row indexes
addRow
public void addRow(Vector row)
- Add the given row in the table.
The argument may be null, in which case an empty row is inserted
(that can be edited by the user).
doSort
protected void doSort()
- Performs the sort.
compareObjects
protected int compareObjects(Object a,
Object b)
- Compare two objects, which may or may not be Comparables, or nulls
quickSort
protected void quickSort(Object[] a,
int lo0,
int hi0)
- Do a quick sort on the given array, which contains column values.
As a result, the index array "_mapToSorted" is modified, not the
paramater array.
- Parameters:
a
- an array of values for the column to sortlo0
- starting index in the arrayhi0
- end index in array
mouseEntered
public void mouseEntered(MouseEvent m)
- Specified by:
mouseEntered
in interface MouseListener
mouseExited
public void mouseExited(MouseEvent m)
- Specified by:
mouseExited
in interface MouseListener
mousePressed
public void mousePressed(MouseEvent m)
- Specified by:
mousePressed
in interface MouseListener
mouseReleased
public void mouseReleased(MouseEvent m)
- Specified by:
mouseReleased
in interface MouseListener
mouseClicked
public void mouseClicked(MouseEvent m)
- If user clicks on column header, perform sort and toggle sort ordering
if the sort column was already the sort column.
- Specified by:
mouseClicked
in interface MouseListener
sortAndUpdate
protected void sortAndUpdate()
- Sorts the table, notifies the model, and redraws the header.
setCustomHeaderRenderer
protected void setCustomHeaderRenderer(int i)
- Assigns a new CustomHeaderRenderer to a column,
preserving state of the existing header renderer.
- Parameters:
i
- set header for this column
main
public static void main(String[] args)
- test main
Copyright © 2015 Central Laboratory of the Research Councils. All Rights Reserved.