public class DefaultTableModel
- Object
- AbstractTableModel
- DefaultTableModel
ImplementsTableModel
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();
Constructors
public DefaultTableModel(String[] columnNames, Object[][] data) | Constructs a new table with a 2 dimensional array for row/column data |
public DefaultTableModel(String[] columnNames, Object[][] data, boolean editable) | Constructs a new table with a 2 dimensional array for row/column data |
Methods
public int getRowCount() | Returns the number of rows in the table |
public int getColumnCount() | Returns the number of columns in the table |
public String getColumnName(int i) | Returns the name of the column at the given offset |
public boolean isCellEditable(int row, int column) | Returns true if the cell at the given location is an editable cell |
public Object getValueAt(int row, int column) | Returns the value of the cell at the given location |
public void setValueAt(int row, int column, Object o) | Sets the value of the cell at the given location |
public void addDataChangeListener(DataChangedListener d) | Adds a listener to the data changed event |
public void removeDataChangeListener(DataChangedListener d) | Removes a listener to the data changed event |
public void addRow(Object... row) | Adds the given row to the table data |
public void insertRow(int offset, Object... row) | Inserts the given row to the table data at the given offset |
public void removeRow(int offset) | Removes the given row offset from the table |
Inherited methods
Constructor details
DefaultTableModel
public DefaultTableModel(String[] columnNames, Object[][] data)Constructs a new table with a 2 dimensional array for row/column data
Parameters
columnNamesString[]- the names of the columns
dataObject[][]- the data within the table
DefaultTableModel
public DefaultTableModel(String[] columnNames, Object[][] data, boolean editable)Constructs a new table with a 2 dimensional array for row/column data
Parameters
columnNamesString[]- the names of the columns
dataObject[][]- the data within the table
editableboolean- indicates whether table cells are editable or not by default
See also
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
public String getColumnName(int i)Returns the name of the column at the given offset
Parameters
iint- 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
rowint- the cell row
columnint- the cell column
Returns
true if the cell at the given location is an editable cell
getValueAt
public Object getValueAt(int row, int column)Returns the value of the cell at the given location
Parameters
rowint- the cell row
columnint- the cell column
Returns
the value of the cell at the given location
setValueAt
public void setValueAt(int row, int column, Object o)Sets the value of the cell at the given location
Parameters
rowint- the cell row
columnint- the cell column
oObject- the value of the cell at the given location
addDataChangeListener
public void addDataChangeListener(DataChangedListener d)Adds a listener to the data changed event
Parameters
dDataChangedListener- the new listener
removeDataChangeListener
public void removeDataChangeListener(DataChangedListener d)Removes a listener to the data changed event
Parameters
dDataChangedListener- the listener to remove
addRow
public void addRow(Object... row)Adds the given row to the table data
Parameters
rowObject...- array or row items, notice that row.length should match the column count exactly!
insertRow
public void insertRow(int offset, Object... row)Inserts the given row to the table data at the given offset
Parameters
offsetint- position within the table that is 0 or larger yet smaller than the row count
rowObject...- 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
offsetint- position within the table that is 0 or larger yet smaller than the row count