Diamond Control Suite 3.0

diamondedge.swing.grid
Class DsGrid

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JTable
                  extended by diamondedge.swing.grid.DsTable
                      extended by diamondedge.swing.grid.DsGrid
All Implemented Interfaces:
DsConstants, GridRowListener, FocusListener, MouseListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, CellEditorListener, ListSelectionListener, RowSorterListener, TableColumnModelListener, TableModelListener, Scrollable, SwingConstants, TableModel

public class DsGrid
extends DsTable
implements MouseListener, FocusListener, TableModel, GridRowListener

DsGrid is a data-aware drop-in replacement for JTable which is used to display and edit regular two-dimensional tables of cells. It provides full and easy formatting and styles for each cell, column, or row. For binding to databases, the ResultSetModel provides a scalable and high performance virtual view of up to 2 billion records from a JDBC ResultSet. The data in the grid can be formatted, sorted, printed and edited using any of the models supplied or any user defined TableModel.

A very easy and flexible object model is available to format and modify the style of cells in the grid. Cells can be individually changed such as:

grid.getCellAt( 1, 2 ).setForeground( Color.blue );
The selected cells can be easily changed such as:
grid.getCells().setBackground( Color.yellow );
All cells in certain columns can be changed easily:
grid.getColumns( 1, 2 ).setHorizontalAlignment( DsConstants.CENTER );
All cells in certain rows can be changed easily:
grid.getRows( 1, 2 ).setFontBold( true );
Cells can be formatted using Java's Format objects such as formatting the 2nd and 3rd column to display as a currency using the default locales currency format:
grid.getColumns( 1, 2 ).setFormat( NumberFormat.getCurrencyInstance() );
Cells can contain calculated values by setting a formula for the cell:
grid.setFormulaAt( "sum(A1:C1)", 0, 3 );
See Formula Functions Reference for a list of the functions available within formulas.

Note that DsTable provides many additional features such as spanning while JTable provides the base implementation.

See Diamond Grid Tutorial (or on the web site) for task-oriented documentation and more examples of using features specific to DsGrid.

See How to Use Tables in The Java Tutorial for task-oriented documentation and examples of using JTable.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class diamondedge.swing.grid.DsTable
DsTable.BooleanEditor, DsTable.BooleanRenderer, DsTable.ComponentEditor, DsTable.ComponentRenderer, DsTable.DateEditor, DsTable.ListEditor, DsTable.TableEditor
 
Nested classes/interfaces inherited from class javax.swing.JTable
JTable.AccessibleJTable, JTable.DropLocation, JTable.PrintMode
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
static int ALTERNATING_ROW_COLOR
           
static int AUTO_LABEL_NONE
           
static int AUTO_LABEL_START_WITH_0
           
static int AUTO_LABEL_START_WITH_1
           
static int AUTO_LABEL_START_WITH_A
           
protected  Paint autoColor1
           
protected  Paint autoColor2
           
protected  Paint autoColor3
           
protected  Paint autoColor4
           
protected  boolean leaveFrozenColumns
          if set to true then the columns remain in the column model when they are frozen and a copy is placed in the row header.
static int SELECTION_BY_COLUMN
          Selections to span entire columns, as if selecting ranges for a chart or fields for sorting.
static int SELECTION_BY_ROW
          Selections to span entire rows, as in a multi-column list box or record-based display.
static int SELECTION_FREE
          Individual cells to be selected, spreadsheet style.
static String version
           
 
Fields inherited from class diamondedge.swing.grid.DsTable
cellsEditable, spannedCells
 
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
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, 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 diamondedge.swing.DsConstants
ACCOUNTING, ALL, ASCENDING, ASCENDING_CASE, BORDER_BOTTOM, BORDER_LEFT, BORDER_NONE, BORDER_OUTLINE, BORDER_RIGHT, BORDER_TOP, CROSS_LINES, DASH, DASH_DOT, DASH_DOT_DOT, DEFAULT, DESCENDING, DESCENDING_CASE, DIAGONAL_CROSS_LINES, DOT, DOUBLE, DOUBLE_LINE, DOWNWARD_DIAGONAL_LINES, ETCHED, ETCHED_RAISED, FALSE, FIND_ALL, FIND_EQUALS, FIND_GT, FIND_GTE, FIND_LT, FIND_LTE, FIND_NE, FIND_PREFIX, FIND_REGEX, FIND_STRING, FIRST, FLAT, GRID_DASH, GRID_DASH_DOT, GRID_DASH_DOT_DOT, GRID_DOT, GRID_LINE, HIGHLIGHT_SELECTION_ALWAYS, HIGHLIGHT_SELECTION_NEVER, HIGHLIGHT_SELECTION_WITH_FOCUS, HORIZONTAL_LINES, LOWERED, LOWERED_LIGHT, NONE, RAISED, RAISED_LIGHT, SELECT_CELL, SELECT_NONE, SELECT_ROW, SINGLE, SINGLE_LINE, SOLID, STRETCH, TILE, TRANSPARENT, TRUE, UPWARD_DIAGONAL_LINES, VERTICAL_LINES
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
DsGrid()
           
DsGrid(int rows, int cols)
           
DsGrid(TableModel dataModel)
           
 
Method Summary
 void addColumnClickListener(ColumnClickListener listener)
          Add a listener to the list that's notified each time a column is clicked on.
 void addGridEditorListener(GridEditorListener l)
          Adds a listener to the list that's notified each time the editing state has changed.
 void addRow()
          Append a new row.
 void addRow(Object item)
          Append a new row with item being value in the first column.
 void addTableModelListener(TableModelListener l)
           
 void addTabOrder(DsCellRange cells)
           
 void addTabOrder(int row, int col)
           
 void addTabOrder(int firstRow, int firstColumn, int lastRow, int lastColumn)
           
protected  void applyAutoStyle()
           
 void changeSelection(int row, int column, boolean toggle, boolean extend)
          Overriden to not allow mouse to select cells that are not in tab order
 void clear()
          Clears the contents of each cell.
 void clearTabOrder()
           
protected  void configureEnclosingScrollPane()
           
protected  void configureRowHeader()
           
 void copyToClipboard()
           
 void copyToClipboard(boolean all, boolean includeColHeaders)
           
 void editingCanceled(ChangeEvent e)
          Invoked when editing is canceled.
 void editingStopped(ChangeEvent e)
          Invoked when editing is finished.
 void ensureVisible(int row)
          Scroll to make sure the row is visible on the screen
 void ensureVisible(int row, int column)
          Scroll to make sure the cell is visible on the screen
 DsCellRange find(Object searchFor, int matchType, int action)
          Finds a value in a cell or multiple cells.
protected  void fireColumnClicked(MouseEvent e)
          This method notifies the ColumnClickListeners that a column has been clicked on.
protected  boolean fireGridEditorEvent(int eventType, int row, int col, Component editor)
           
 void focusGained(FocusEvent e)
           
 void focusLost(FocusEvent e)
           
 void freezeColumn(int col)
          Turns the specified column into a fixed (non-scrollable) column by moving the columns into the RowHeader.
 Image getBackgroundImage()
          Return the image that is painted on the background surface
 DsCell getCell()
          Returns the active cell.
 DsCellRange getCells()
          Returns a range of cells defined by the user's selection or by the combination of the Row, Col, Row2, Col2 properties.
 DsCellRange getCells(int firstRow, int firstColumn, int lastRow, int lastColumn)
          Returns a range of cells in between and including the specified cell corners.
 Object getCellValue()
          Returns the text contents/object for the active cell.
 String getClipString()
          Returns the selected cells formated into a tab delimited string.
 int getCol()
          Returns the column of the active cell.
 int getCol2()
          Determines the starting or ending column for a range of cells.
 int getColumnAtPoint(Point point)
          Returns the index of the column that point lies in or -1 if not a valid column.
 Class getColumnClass(int column)
          Returns the type of the column appearing in the view at column position column.
protected  JViewport getColumnHeaderViewport()
           
 DsCellRange getColumns(int firstColumn, int lastColumn)
          Returns a range of cells that includes all cells in the specified columns.
 int getFirstVisibleColumn()
          Returns the index of the column that is displayed at the left of the grid
 int getFirstVisibleRow()
          Returns the index of the row that is displayed at the top of the grid
 int getFrozenColumnCount()
          Returns the total number of fixed (non-scrollable) columns.
 GridModel getGridModel()
          Returns the inner most TableModel which is an instance of GridModel.
 int getHighlightPolicy()
          Determines when the selection is highlighted.
 int getNumClicksToSort()
          Returns the number of clicks on the column header required to allow the user to select the column to sort on or to change the sort order.
 int getRow()
          Returns the row of the active cell.
 int getRow2()
          Determines the starting or ending row for a range of cells.
 int getRowAtPoint(Point point)
          Returns the index of the row that point lies in or -1 if not a valid row.
 GridRowHeader getRowHeader()
          Returns fixed columns displaying a row header for each row.
 int getRowHeaderAutoLabel()
          Returns a value that determines the automatic assignment of the row header values.
protected  JViewport getRowHeaderViewport()
           
 int getRowHeaderWidth()
          Returns a value that determines the automatic assignment of the row header values.
 DsCellProperties getRowProperties(int row)
          Returns the default cell containing the default properties for the row.
 DsCellRange getRows(int firstRow, int lastRow)
          Returns a range of cells that includes all cells in the specified rows.
 int getSortColumn()
          Returns the column in the model that is currently being used to sort the rows.
 int getSortOrder()
          Returns whether or not the rows will be sorted in ascending or descending order.
 String getTextAt(int row, int col)
          Returns the text (contents formatted as a string) for the cell at the given row and column.
 int getTopVisibleRow()
          Deprecated. replaced by getFirstVisibleRow()
 void insertRow(int index, String item)
          Adds a new row at run time at the given position.
 boolean isAutoRecalc()
          Returns whether recalcFormulas() automatically called when cell values change.
 boolean isCellEditable(int row, int column)
          Returns true if the cell at row and column is editable.
 boolean isColumnHeaderVisible()
          Returns whether there is a fixed row displaying a column header for each column.
 boolean isRowHeaderVisible()
          Returns whether there are fixed columns displaying a row header for each row.
 boolean isRowStatusIconShowing()
          Returns whether an icon will be shown in the row header indicating the status of the row.
 boolean isSorted()
          Indicates whether the rows are automatically sorted alphabetically.
 void loadCSV(File file, boolean firstRowIsColHeader)
          Load data from a text stream formatted as comma separated values.
 void loadCSV(Reader reader, boolean firstRowIsColHeader, boolean convertData)
          Load data from a text stream formatted as comma separated values.
 void loadResultSet(ResultSet rs, boolean setHeaderColumns)
          Loads data from the ResultSet into the grid.
 void loadResultSet(ResultSet rs, int begRow, int begCol, int endRow, int endCol)
          Loads data from the ResultSet into the grid.
 void loadTabDelim(File file, boolean firstRowIsColHeader)
          Load data from a text stream formatted as tab delimited values.
 void loadTabDelim(Reader reader, boolean firstRowIsColHeader, boolean convertData)
          Load data from a text stream formatted as tab delimited values.
 void loadTextData(Reader reader, int begRow, int begCol, int endRow, int endCol, boolean firstRowIsColHeader, char delim, boolean stripStringDelim, boolean convertData)
          Load data from a text stream.
static void main(String[] args)
           
 void mouseClicked(MouseEvent e)
           
 void mouseEntered(MouseEvent e)
           
 void mouseExited(MouseEvent e)
           
 void mousePressed(MouseEvent e)
           
 void mouseReleased(MouseEvent e)
           
 void paint(Graphics g)
           
 void paste()
           
 Component prepareEditor(TableCellEditor editor, int row, int column)
          Creates and prepares the editor when an edit is requested.
 boolean print()
          Print data using default properties
 boolean print(boolean showPrintDialog, double scaleFactor, DsCellRange range, String footer, int showColumnHeader, int showRowHeader)
          Print data in the grid.
 void printPreview()
          Preview the data in the grid as it would appear on the printed page.
 void printPreview(boolean showAllPages, double scaleFactor, DsCellRange range, String footer, int showColumnHeader, int showRowHeader)
          Preview the data in the grid as it would appear on the printed page.
 void recalcFormulas()
          Calculate the values of all of the formulas.
 void removeAll()
          Removes all cells including all rows and columns.
 void removeColumnClickListener(ColumnClickListener listener)
          Remove a listener from the list that's notified each time a column is clicked on.
 void removeGridEditorListener(GridEditorListener l)
          Removes a listener from the list that's notified each time the editing state has changed.
 void removeRow(int index)
          Removes a row at run time
 void removeTableModelListener(TableModelListener l)
           
 DsCellRange replace(Object searchFor, int matchType, Object replaceWith)
          Finds values in a cell or multiple cells and replaces the entire value with the value specified.
 void rowChanged(GridEvent e)
           
 void saveAsCSV(File file, boolean saveHeaderColumns)
          Save data to a file with comma separated value format.
 void saveAsHTML(File file, boolean saveHeaderColumns)
          Save data to a HTML formated stream.
 void saveAsHTML(Writer writer, int begRow, int begCol, int endRow, int endCol, boolean saveHeaderColumns)
          Save data to a HTML formated stream.
 void saveAsTabDelim(File file, boolean saveHeaderColumns)
          Save data to a file with tab delimeted text format.
 void saveAsTextData(Writer writer, int begRow, int begCol, int endRow, int endCol, boolean saveHeaderColumns, char delim, boolean addStringDelim)
          Save data to a delimeted stream such as tab-delimeted or csv.
 boolean select(int row, int col)
          Sets the row and column of the active cell.
 boolean select(int row, int col, int row2, int col2)
          Sets the row and column of the active cell which is the beginning of the selection.
 void setAutoRecalc(boolean automaticallyRecalculateFormulas)
          Sets whether recalcFormulas() automatically called when cell values change.
 void setAutoResizeMode(int autoResize)
          Sets the table's auto resize mode when the table is resized.
 void setAutoStyle(int autoStyle)
          Automatically sets the styles for rows and columns using default colors.
 void setAutoStyle(int autoStyle, Paint color1, Paint color2, Paint color3, Paint color4)
          Automatically sets the styles for rows and columns.
 void setBackgroundImage(Image image)
          Set the image that is painted on the background surface.
 void setBackgroundImageStyle(int style)
          Paint the background image using the given style
 void setCellValue(Object value)
          Sets the text contents/object for the active cell.
 void setClipString(String tabDelimtedStr)
          Replaces the selected cells with the values from the tab delimited string.
 void setCol(int col)
          Sets the column of the active cell.
 void setCol2(int val)
          Determines the starting or ending column for the selected range of cells.
 void setColumnClass(int column, Class type)
          Sets the type of the column appearing in the view at column position column.
 void setColumnHeaderVisible(boolean visible)
          Sets whether there is a fixed row displaying a column header for each column.
 void setColumnSelectionAllowed(boolean columnSelectionAllowed)
          Sets whether the columns in this model can be selected.
 void setFormulaAt(String formula, int row, int col)
          Sets the formula for the cell at the given row and column.
 void setFrozenColumnCount(int val)
          Sets the total number of fixed (non-scrollable) columns by moving the specified number of columns into the RowHeader.
 void setHighlightPolicy(int val)
          Determines when the selection is highlighted.
 void setModel(TableModel dataModel)
           
 void setNumClicksToSort(int numClicks)
          Allow the user to interactively select which column to sort on by clicking on the column header.
 void setRow(int row)
          Sets the row of the active cell.
 void setRow2(int row)
          Determines the starting or ending row for a range of cells.
 void setRowCount(int val)
          Sets the total number of rows by adding new blank rows or removing rows.
 void setRowHeaderAutoLabel(int autoLabel)
          Set the automatic assignment of the row header values.
 void setRowHeaderVisible(boolean visible)
          Sets whether there are fixed columns displaying a row header for each row.
 void setRowHeaderWidth(int width)
          Set the total width of all columns in the row header.
 void setRowHeaderWidthToFit(boolean fitColumnNames, boolean fitColumnData, int columnMargin)
          Sets the total width of the row header needed to fit the text for the name of the column and/or the data in cells in that column for each column in the row header.
 void setRowHeight(int rowHeight)
          Sets the height, in pixels, of all cells to rowHeight, revalidates, and repaints.
 void setRowHeight(int row, int rowHeight)
          Sets the height for row to rowHeight, revalidates, and repaints.
 void setRowSelectionAllowed(boolean rowSelectionAllowed)
          Sets whether the columns in this model can be selected.
 void setRowStatusIconShowing(boolean showRowIcon)
          Determines whether an icon will be shown in the row header indicating the status of the row.
 void setSelectionPolicy(int val)
          Sets whether it should allow regular cell selection, selection by rows, or selection by columns.
 void setSortColumn(int modelIndex)
          Sets the column index in the model that is to be used to sort the rows.
 void setSorted(boolean sort)
          Sets whether the rows are automatically sorted alphabetically.
 void setSortIcons(Icon ascendingSortIcon, Icon descendingSortIcon)
          Set custom icons that show which direction a column is sorted These are only used if sorting by one column
 void setSortOrder(int sortOrder)
          Sets whether or not the rows will be sorted in ascending or descending order.
 void setValueAt(Object val, int row, int col)
          Sets the text contents/object for the cell at the given row and column.
 void sort()
          Sorts the list items according to the current sort column and order.
 void sort(int column, int sortOrder)
          Sorts the model by comparing values in the given column.
 boolean spanCells(int startRow, int startColumn, int rowsToSpan, int columnsToSpan)
          Makes the cell at position startRow, startColumn span across rowsToSpan rows and columnsToSpan columns.
 void tableChanged(TableModelEvent e)
          Invoked when this table's TableModel generates a TableModelEvent.
 void updateUI()
           
 void valueChanged(ListSelectionEvent e)
           
 
Methods inherited from class diamondedge.swing.grid.DsTable
addColumn, addColumn, addColumn, calculateColumnWidthToFit, calculateWidthToFit, columnAtPoint, columnSelectionChanged, createCell, createDefaultColumnsFromModel, createDefaultEditors, createDefaultRenderers, createDefaultTableHeader, getCellAt, getCellColumnSpan, getCellEditor, getCellRect, getCellRenderer, getCellRowSpan, getCellValueAt, getColumn, getColumn, getColumnCount, getColumnHeader, getColumnHeaderBackground, getColumnHeaderFont, getColumnHeaderForeground, getColumnHeaderHeight, getColumnIndex, getColumnName, getColumnProperties, getColumnWidth, getScrollPane, getToolTipText, hideColumn, isColumnReorderingAllowed, isColumnResizingAllowed, isEditable, removeSpan, rowAtPoint, setColumnCount, setColumnHeaderBackground, setColumnHeaderFont, setColumnHeaderForeground, setColumnHeaderHeight, setColumnHorizontalAlignment, setColumnName, setColumnReorderingAllowed, setColumnResizingAllowed, setColumnVerticalAlignment, setColumnWidth, setColumnWidth, setColumnWidthToFit, setEditable
 
Methods inherited from class javax.swing.JTable
addColumnSelectionInterval, addNotify, addRowSelectionInterval, clearSelection, columnAdded, columnMarginChanged, columnMoved, columnRemoved, convertColumnIndexToModel, convertColumnIndexToView, convertRowIndexToModel, convertRowIndexToView, createDefaultColumnModel, createDefaultDataModel, createDefaultSelectionModel, createScrollPaneForTable, doLayout, editCellAt, editCellAt, getAccessibleContext, getAutoCreateColumnsFromModel, getAutoCreateRowSorter, getAutoResizeMode, getCellEditor, getCellSelectionEnabled, getColumn, getColumnModel, getColumnSelectionAllowed, getDefaultEditor, getDefaultRenderer, getDragEnabled, getDropLocation, getDropMode, getEditingColumn, getEditingRow, getEditorComponent, getFillsViewportHeight, getGridColor, getIntercellSpacing,