Class DefaultTableModel
- All Implemented Interfaces:
TableModel
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 Summary
ConstructorsConstructorDescriptionDefaultTableModel(String[] columnNames, Object[][] data) Constructs a new table with a 2 dimensional array for row/column dataDefaultTableModel(String[] columnNames, Object[][] data, boolean editable) Constructs a new table with a 2 dimensional array for row/column data -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a listener to the data changed eventvoidAdds the given row to the table dataintReturns the number of columns in the tablegetColumnName(int i) Returns the name of the column at the given offsetintReturns the number of rows in the tablegetValueAt(int row, int column) Returns the value of the cell at the given locationvoidInserts the given row to the table data at the given offsetbooleanisCellEditable(int row, int column) Returns true if the cell at the given location is an editable cellvoidRemoves a listener to the data changed eventvoidremoveRow(int offset) Removes the given row offset from the tablevoidsetValueAt(int row, int column, Object o) Sets the value of the cell at the given locationMethods inherited from class AbstractTableModel
getCellType, getMultipleChoiceOptions, getValidationConstraint, getValidator, setValidator
-
Constructor Details
-
DefaultTableModel
-
DefaultTableModel
Constructs a new table with a 2 dimensional array for row/column data
Parameters
-
columnNames: the names of the columns -
data: the data within the table -
editable: indicates whether table cells are editable or not by default
See also
- #isCellEditable(int, int)
-
-
-
Method Details
-
getRowCount
public int getRowCount()Returns the number of rows in the table
Returns
the number of rows in the table
-
getColumnCount
public int getColumnCount()Returns the number of columns in the table
Returns
the number of columns in the table
-
getColumnName
Returns the name of the column at the given offset
Parameters
i: the offset for the column name
Returns
name to display at the top of the table
-
isCellEditable
public boolean isCellEditable(int row, int column) Returns true if the cell at the given location is an editable cell
Parameters
-
row: the cell row -
column: the cell column
Returns
true if the cell at the given location is an editable cell
-
-
getValueAt
Returns the value of the cell at the given location
Parameters
-
row: the cell row -
column: the cell column
Returns
the value of the cell at the given location
-
-
setValueAt
Sets the value of the cell at the given location
Parameters
-
row: the cell row -
column: the cell column -
o: the value of the cell at the given location
-
-
addDataChangeListener
Adds a listener to the data changed event
Parameters
d: the new listener
-
removeDataChangeListener
Removes a listener to the data changed event
Parameters
d: the listener to remove
-
addRow
Adds the given row to the table data
Parameters
row: array or row items, notice that row.length should match the column count exactly!
-
insertRow
Inserts the given row to the table data at the given offset
Parameters
-
offset: position within the table that is 0 or larger yet smaller than the row count -
row: array or row items, notice that row.length should match the column count exactly!
-
-
removeRow
public void removeRow(int offset) Removes the given row offset from the table
Parameters
offset: position within the table that is 0 or larger yet smaller than the row count
-