public class DefaultTableModel extends AbstractTableModel
A default implementation of the table model based on a two dimensional array.
Form hi = new Form("Table", new BorderLayout()); | |
TableModel model = new DefaultTableModel(new String[] {"Col 1", "Col 2", "Col 3"}, new Object[][] { | |
{"Row 1", "Row A", "Row X"}, | |
{"Row 2", "Row B can now stretch", null}, | |
{"Row 3", "Row C", "Row Z"}, | |
{"Row 4", "Row D", "Row K"}, | |
}) { | |
public boolean isCellEditable(int row, int col) { | |
return col != 0; | |
} | |
}; | |
Table table = new Table(model) { | |
@Override | |
protected Component createCell(Object value, int row, int column, boolean editable) { // (1) | |
Component cell; | |
if(row == 1 && column == 1) { // (2) | |
Picker p = new Picker(); | |
p.setType(Display.PICKER_TYPE_STRINGS); | |
p.setStrings("Row B can now stretch", "This is a good value", "So Is This", "Better than text field"); | |
p.setSelectedString((String)value); // (3) | |
p.setUIID("TableCell"); | |
p.addActionListener((e) -> getModel().setValueAt(row, column, p.getSelectedString())); // (4) | |
cell = p; | |
} else { | |
cell = super.createCell(value, row, column, editable); | |
} | |
if(row > -1 && row % 2 == 0) { // (5) | |
// pinstripe effect | |
cell.getAllStyles().setBgColor(0xeeeeee); | |
cell.getAllStyles().setBgTransparency(255); | |
} | |
return cell; | |
} | |
@Override | |
protected TableLayout.Constraint createCellConstraint(Object value, int row, int column) { | |
TableLayout.Constraint con = super.createCellConstraint(value, row, column); | |
if(row == 1 && column == 1) { | |
con.setHorizontalSpan(2); | |
} | |
con.setWidthPercentage(33); | |
return con; | |
} | |
}; | |
hi.add(BorderLayout.CENTER, table); | |
hi.show(); |
Constructor and Description |
---|
DefaultTableModel(String[] columnNames,
Object[][] data)
Constructs a new table with a 2 dimensional array for row/column data
|
DefaultTableModel(String[] columnNames,
Object[][] data,
boolean editable)
Constructs a new table with a 2 dimensional array for row/column data
|
Modifier and Type | Method and Description |
---|---|
void |
addDataChangeListener(DataChangedListener d)
Adds a listener to the data changed event
|
void |
addRow(Object... row)
Adds the given row to the table data
|
int |
getColumnCount()
Returns the number of columns in the table
|
String |
getColumnName(int i)
Returns the name of the column at the given offset
|
int |
getRowCount()
Returns the number of rows in the table
|
Object |
getValueAt(int row,
int column)
Returns the value of the cell at the given location
|
void |
insertRow(int offset,
Object... row)
Inserts the given row to the table data at the given offset
|
boolean |
isCellEditable(int row,
int column)
Returns true if the cell at the given location is an editable cell
|
void |
removeDataChangeListener(DataChangedListener d)
Removes a listener to the data changed event
|
void |
removeRow(int offset)
Removes the given row offset from the table
|
void |
setValueAt(int row,
int column,
Object o)
Sets the value of the cell at the given location
|
getCellType, getMultipleChoiceOptions, getValidationConstraint, getValidator, setValidator
public DefaultTableModel(String[] columnNames, Object[][] data)
columnNames
- the names of the columnsdata
- the data within the tablepublic DefaultTableModel(String[] columnNames, Object[][] data, boolean editable)
columnNames
- the names of the columnsdata
- the data within the tableeditable
- indicates whether table cells are editable or not by defaultisCellEditable(int, int)
public int getRowCount()
public int getColumnCount()
public String getColumnName(int i)
i
- the offset for the column namepublic boolean isCellEditable(int row, int column)
row
- the cell rowcolumn
- the cell columnpublic Object getValueAt(int row, int column)
row
- the cell rowcolumn
- the cell columnpublic void setValueAt(int row, int column, Object o)
row
- the cell rowcolumn
- the cell columno
- the value of the cell at the given locationpublic void addDataChangeListener(DataChangedListener d)
d
- the new listenerpublic void removeDataChangeListener(DataChangedListener d)
d
- the listener to removepublic void addRow(Object... row)
row
- array or row items, notice that row.length should match the column count exactly!public void insertRow(int offset, Object... row)
offset
- position within the table that is 0 or larger yet smaller than the row countrow
- array or row items, notice that row.length should match the column count exactly!public void removeRow(int offset)
offset
- position within the table that is 0 or larger yet smaller than the row count