1

我在位于 JTable 列中的 JComboBox 中有一个现有的值内容。我要做的是从现有对象中读取一个值并更新 ComboBox 以立即显示该值。

我的第一次尝试是:

 // Sets up properties ComboBox
    propColumn = table.getColumnModel().getColumn(ENV_PROPERTIES_COLUMN);
    propComboBox = new JComboBox();
    propComboBox.addItem(""); // For initial empty string
    constructEnvProperties();

/**
 * Construct Environment Properties comboBox options
 */

     public void constructEnvProperties(){

           Vector<IWM781EnvProfileProperties> recordSet = dataMapperDatabase.getEnvironmentalProperties();

          // Iterate through vector and update combo box
                    for(int i = 0; i < recordSet.size(); i++){

                       logger.debug("Property: " + recordSet.get(i).getProp781Property());
                       propComboBox.addItem(recordSet.get(i).getProp781Property()); 
    }
}

现在,当我想将 ComboBox 更新为选定的索引时,我使用以下代码:

if(record.getProp785MapProperty().compareTo("") != 0){

    ComboBoxModel model = propComboBox.getModel(); 
    int size1 = model.getSize();

       for (int i1 = 0; i1 < size1; i1++){

            String comparision = record.getRegv785MapRegister();

              if(comparision.equals(propComboBox.getItemAt(i1)))
                 propComboBox.setSelectedIndex(i1);
       }
 }

propColumn.setCellRenderer(new ComboBoxCellRenderer());
propColumn.setCellEditor(new DefaultCellEditor(propComboBox));  

当我通过它进行调试时,它的性能完全符合我的预期,但表没有更新。

我已经修改了创建自己的 DefaultCellEditor 来修改一些功能。这使我可以灵活地选择包含组合框的特定单元格,我目前正在尝试将其修改为解决方案。

4

1 回答 1

1

想出了一个解决问题的方法,以防万一其他人看到这个并认为,嗯,我有类似的问题。当我设置我的 TableModel 时,我使用了以下方法:

   /**
   * Insert row into JTable
   * @param rowData
   */
public void insertRow (Object rowData){
    rows.add((Object[]) rowData);   
}

将行添加到 JTable 中。

当我从我的主向 JTable 中插入行时,我正在使用:

 // Data to be inserted into the JTable     
        String[] data = new String[] {seqID, fieldName, type, size, "", value, "", "",""};
        tableModel.insertRow(data);

由于硬编码的“”值,组合框被自动分配给组合框中存在的空字符串。快速修复是为每个组合框值创建一个字符串变量,对它们执行特定检查以确保有要填充的数据,瞧。

解决方案看起来很简单,我现在觉得很愚蠢......

于 2014-01-06T15:48:09.427 回答