As with many properties a default data Format can be set for all
cells in a column or only on specific cells. The examples below will
mostly just set the data
type for one particular cell. This is done by using the
setFormat method. Instances of the java.text.Format class are used both
to format how the value is printed on the screen and what types of
valid values are allowed.
Whan an instance of Number is assigned as the value of a cell then
it will by default be right aligned.
There are two ways to force the user input to always be a number.
One is to force all values in a column to be of a particular type such as:
grid.setColumnClass( column, Integer.class )
Another way is to set the Format property to be one of the Number type formats.
This can be done either on a particular cell or on the column. This example
will force all user input for the selected cells to be integers.
grid.getCells().setValue( new Integer(3) );
grid.getCells().setFormat( NumberFormat.getIntegerInstance() );
A currency cell type is created by setting the Format property of the cell
to a currency Format instance.
All numeric values are then displayed in the given currency format.
User input is restricted to a formatted currency or to any valid Number.
A percent cell type is created by setting the Format property of the cell
to a percent Format instance.
All numeric values are then displayed in the given percent format.
User input is restricted to a formatted percent or to any valid Number.
A Date cell type is created by setting the value to a Date object
or by setting the Format property of the cell to a date Format instance.
The date is then displayed in the given date format or the default date format.
User can enter a new date directly by typing it in or using the arrow keys to modify
the data. The date can also be entered by clicking on the down arrow button
to display the drop-down date picker.
Forcing the value of a cell to be a string with a very specific format is easy
with Java's MaskFormatter object. This object can then be used to set the
Format property on a cell or on a column causing all user input to conform to
Displaying just an icon or image in a cell can be achieved by setting the
value of the cell to that image or icon. Alternatively, you can set the
value to null and call setIcon on the cell to be the icon to be displayed.
Displaying an icon and a value can be achieved by setting the
value of the cell to what should be displayed. Then you can set the icon
by calling setIcon. You may also want to adjust the relative positioning
of the text and icon by setting the HorizontalTextPosition and
VerticalTextPosition properties on the cell.
Using a ComboBox as the user input editor can be done with DsGrid as it
can be with any JTable. Currently it is limited to making an entire column
use the same ComboBox. This is done by setting the CellEditor for the
column to be the given JComboBox.
JComboBox comboBox = new JComboBox();
grid.getColumn( 1 ).setCellEditor( new DefaultCellEditor(comboBox) );
Any class that is an extension of JComponent can be used to draw and edit
the contents of a cell. This example uses JButton. Just as with any swing
component you can add listeners to the component to process any events you
are interested in.