向表中动态添加行并为单元格设置值时出现问题



我使用了一个自定义的表模型来向表中动态添加行。我的自定义表格模型是-

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);
}

当为任何单元格设置值时,有时它也会将值设置为同一列中的其他单元格

我已经查看了包含向表添加/删除行的原始问题,并认为这些方法不是很好,所以我们修复了DefaultTableModel,以支持向表添加或删除行。

这应该"起作用"。它将是下一次插件更新的一部分,或者你可以通过使用Codename One源代码更快地获得它。

最新更新