2015-03-19 101 views
1

我已經使用了自定義表模型來動態地向表中添加行。我的自定義的表格模型 - 表中所向表中動態添加行並將值設置爲單元格的問題

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

當設置值的任何細胞,有時它設置值到在相同列其他細胞以及。

+0

我們會研究一下。 – 2015-03-20 06:09:02

+0

任何決議請? – 2016-02-18 12:03:45

+0

對不起,它被埋在任務列表下,我完全失去了它。我會盡量在週末進行審查/完成,但請留意這一點,並提醒我是否錯過了它! – 2016-02-19 03:47:23

回答

0

我查看了包含添加/刪除表格的原始問題,並決定這些方法不是很好,因此我們修復了DefaultTableModel以支持在表格中添加/刪除行。

這應該「只是工作」。它將成爲下一個插件更新的一部分,或者您可以通過working with the Codename One source code更快得到它。

+0

謝謝。等待下一個插件更新。我們何時獲得新的插件更新(大約)? – 2016-02-22 06:15:22

+0

我們剛剛發佈了一個,所以可能需要一段時間。可能在四月之前。 – 2016-02-23 04:07:36

相關問題