The JSmartGridTMTutorial
The JSmartGrid and the JTable
A grid is best described by comparison with a table:
-
How they are alike:
-
They do not contain data, they simply open a view on it in accordance
with the Model View Controller pattern used in all
SwingTM components.
-
They present a data model that is structured as a matrix, with a given
number of rows and columns. The elementary subdivision of the matrix is
the cell, identified by a row and a column.
-
They usually present more data than can be viewed at one time, so they
need to be incorporated inside some Viewport that will display only a partial
view, and allow scrolling through it.
-
How they are different:
-
Tables are fundamentally vertical in that the structure is determined in
the columns, while the rows contain the data. Grids are basically symmetrical
with respect to the vertical and the horizontal orientations.
-
In a grid, cells may also be merged into bigger rectangular arrays, called
spans.
In other words, the JSmartGrid is very versatile and can handle
tabular data presentations, spreadsheet applications, and everything in
between. As it has very little automatic behavior, its usage is not limited
therefore allowing it to be turned into a specialized tool to be used in
any particular context. Here is a list of some practical differences:
-
By default, a JSmartGrid does not display headers, even inside
a JScrollPane. A JTable is a view on a TableModel which, being
vertically-oriented, seems to provide natural column headers. A GridModel
does not use these default headers which actually gives you more flexibility
to customize your grid according to your specific needs. If your needs
do require headers, adding them is very easy:
A header is, in fact, a natural implementation of frozen panes and has
all the capabilities of an independent JSmartGrid.
- Unlike the JTable, the JSmartGrid does not handle column dragging by default
and does not impose a strict data structure, thus there are no swapping features
in the data model. The grid therefore provides a "dragging" API based
on cells, rows and columns. See how easy it is with this API to implement
the JTable behavior with the GenericGridModel. See
also a different version.
-
The consequences of the vertical structure of the JTable rendering
are the following:
- renderers (and editors) tend to be column-specific
and rely on the getColumnClass method provided by models;
- the default (DefaultTableModel) model does not provide a complex
definition, so the default, text-oriented renderer is used for all cells,
despite of the type of data they may contain.
- The grid has no such expectations thus its default rendering is cell-based.
See an example of how Integer and Boolean
data are rendered appropriately without the model needing to provide any particular
intelligence. This would not be the case if the tableModel where
this data resides was to be displayed in a JTable.