Last active
July 8, 2020 09:57
-
-
Save codenameone/6b106772ad1d58c50270 to your computer and use it in GitHub Desktop.
Sample for using the Table API with pinstripes, spanning and custom editors for a cell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage code for Table & DefaultTableModel.
From the Codename One project