Cell Data Types and Formatting Begin | Previous | Next
Number | Currency | Percent | Date | Mask | Icon | Text and Icon | CheckBox | ComboBox | JComponent (owner-draw / JButton)

Overview of Data Types
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.
Number
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() );
Currency
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.
grid.getCells().setFormat( NumberFormat.getCurrencyInstance() );
Percent
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.
grid.getCells().setFormat( NumberFormat.getPercentInstance() );
Date
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.
grid.getCells().setValue( new java.util.Date() );
Mask
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 that format.
javax.swing.text.MaskFormatter formatter;
formatter = new javax.swing.text.MaskFormatter( "(###) ###-####" );
formatter.setPlaceholderCharacter('_');
grid.getCells().setFormat( formatter );
grid.getCells().setValue( null );
Icon or Image
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.
grid.getCells().setValue( UIManager.getIcon("Tree.expandedIcon") );
Text and Icon
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.
grid.getCells().setIcon( UIManager.getIcon("Tree.expandedIcon") );
grid.getCells().setHorizontalTextPosition( DsConstants.RIGHT );
CheckBox
A CheckBox is displayed whenever the value of a cell is of type Boolean. All cells in a column can als be forced to be CheckBoxes by setting the ColumnClass property on the grid.
grid.getCells().setValue( new Boolean(false) );
ComboBox
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();
comboBox.addItem("");
comboBox.addItem("Football");
comboBox.addItem("Basketball");
comboBox.addItem("Soccer");
comboBox.addItem("Skiing");
comboBox.addItem("Snowboarding");
grid.getColumn( 1 ).setCellEditor( new DefaultCellEditor(comboBox) );
JComponent (owner-draw / JButton)
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.
grid.getCells().setValue( new JButton("Test") );
Copyright 2003 Diamond Edge, Inc. All rights reserved.