我使用自定义表模型动态地向表添加行 . 我的自定义表型号是 -
public class CustomTableModel implements TableModel {
private ArrayList<Object[]> data;
private String[] columnNames;
private EventDispatcher dispatcher = new EventDispatcher();
private boolean editable;
public CustomTableModel(String[] columnNames, Object[][] data) {
this(columnNames, data, false);
}
public CustomTableModel(String[] columnNames, Object[][] data, boolean editable) {
this.data = new ArrayList<Object[]>();
for(Object[] o : data) {
this.data.add(o);
}
this.columnNames = columnNames;
this.editable = editable;
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return columnNames.length;
}
public String getColumnName(int i) {
return columnNames[i];
}
public boolean isCellEditable(int row, int column) {
return editable;
}
public Object getValueAt(int row, int column) {
try {
return data.get(row)[column];
} catch(ArrayIndexOutOfBoundsException err) {
return "";
}
}
public void setValueAt(int row, int column, Object o) {
data.get(row)[column] = o;
dispatcher.fireDataChangeEvent(column, row);
}
public void addDataChangeListener(DataChangedListener d) {
dispatcher.addListener(d);
}
public void removeDataChangeListener(DataChangedListener d) {
dispatcher.removeListener(d);
}
public void addRow(Object[] rowData, int row) {
try {
data.add(rowData);
dispatcher.fireDataChangeEvent(0, row);
} catch (Exception e) {
e.printStackTrace();
}
}
表中使用的变量是 -
// variables used in Table dataTable
String[][] tableData = {
{ "1", "2", "3","4","5", "6", "7", "8", "9","10","11", "12", "13", "14", "15"}
};
final CustomTableModel tableModel = new CustomTableModel (
new String[] { "","Density", "Volume Flow", "T (in)","T (out)","Flow", "Specific Heat", "Density", "Volume Flow",
"T (in)","T (out)","Flow", "Specific Heat", "Duty", "UA"}, tableData,true);
final String[] r = { tableModel.getRowCount()+1+"", "2", "3","4","5", "6", "7", "8", "9","10","11", "12", "13", "14", "15"};
表实现在这里 -
dataTable = new Table(tableModel) {
@Override
protected Component createCell(Object value, final int row, final int column, boolean editable) {
if(row == -1) {
final Button headerButton = new Button((String)value);
headerButton.setUIID(getUIID() + "Header");
headerButton.getUnselectedStyle().setAlignment(Component.CENTER);
headerButton.getSelectedStyle().setAlignment(Component.CENTER);
headerButton.setFlatten(true);
}
if(row >= 0) {
if(column == 3 || column == 4 || column == 9 || column == 10) {
final Button cell = new Button((String)value);
cell.setUIID(getUIID() + "Cell");
cell.getUnselectedStyle().setAlignment(Component.CENTER);
cell.getSelectedStyle().setAlignment(Component.CENTER);
cell.setFlatten(true);
cell.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
Container body = new Container();
body.setLayout(new GridBagLayout());
Label value = new Label("Value");
value.getStyle().setBgTransparency(0);
TextField text = new TextField();
text.getStyle().setBgTransparency(0);
text.setText(cell.getText());
Label unit = new Label("Unit");
unit.getStyle().setBgTransparency(0);
ComboBox<String> uom = new ComboBox<String>();
uom.addItem("Celcius");
uom.addItem("Fahrenheit");
uom.addItem("Kelvin");
uom.getStyle().setBgTransparency(0);
body.addComponent(new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), value);
body.addComponent(new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), text);
body.addComponent(new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), unit);
body.addComponent(new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), uom);
Command cmd = Dialog.show("Add Values", body, new Command[] {new Command("OK")});
if(cmd.getCommandName().equals("OK")) {
tableModel.setValueAt(row, column, text.getText());
}
int totalRow = tableModel.getRowCount();
if(!(totalRow > row +1)) {
tableModel.addRow(r, row);
}
}
});
return cell;
}
if(column == 1 || column == 2 || column == 5 || column == 6 || column == 7 || column ==8 || column > 10) {
final Button cell = new Button((String)value);
cell.setUIID(getUIID() + "Cell");
cell.getUnselectedStyle().setAlignment(Component.CENTER);
cell.getSelectedStyle().setAlignment(Component.CENTER);
cell.setFlatten(true);
cell.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
Container body = new Container();
body.setLayout(new GridBagLayout());
Label value = new Label("Value");
value.getStyle().setBgTransparency(0);
TextField text = new TextField();
text.getStyle().setBgTransparency(0);
text.setText(cell.getText());
Label unit = new Label("Unit");
unit.getStyle().setBgTransparency(0);
ComboBox<String> uom = new ComboBox<String>();
uom.addItem("Celcius");
uom.addItem("Fahrenheit");
uom.addItem("Kelvin");
uom.getStyle().setBgTransparency(0);
body.addComponent(new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), value);
body.addComponent(new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), text);
body.addComponent(new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), unit);
body.addComponent(new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), uom);
Command cmd = Dialog.show("Add Values", body, new Command[] {new Command("OK")});
if(cmd.getCommandName().equals("OK")) {
tableModel.setValueAt(row, column, text.getText());
}
int totalRow = tableModel.getRowCount();
if(!(totalRow > row +1)) {
tableModel.addRow(r, row);
}
}
});
return cell;
}
}
Button cell = new Button("" + value);
cell.setUIID(getUIID() + "Cell");
cell.getUnselectedStyle().setAlignment(Component.CENTER);
cell.getSelectedStyle().setAlignment(Component.CENTER);
cell.setFlatten(true);
return cell;
}
};
如果你在上面的代码中看到,在表中添加行的条件是 -
if(!(totalRow > row +1)) {
tableModel.addRow(r, row);
}
如果我触摸表格最后一行的任何单元格,将弹出一个对话框,当触摸确定按钮时,它应该在表格中添加一个额外的行 . 但发生了什么 - 第一次触摸确定按钮没有任何反应,然后重复此步骤,一行被添加到表中 . 这会在每个最后一行(添加的行)上递归发生 .
第二个问题是 CustomTableModel 的下面方法
public void setValueAt(int row, int column, Object o) {
data.get(row)[column] = o;
dispatcher.fireDataChangeEvent(column, row);
}
将值设置为任何单元格时,有时它会将值设置为同一列中的其他单元格 .
1 回答
我很好,所以我们修改了DefaultTableModel以支持向表中添加/删除行 .
这应该"just work" . 它将成为下一个插件更新的一部分,或者您可以通过working with the Codename One source code更快地获得它 .