【问题标题】:Update a combo box to display a specific Index within a JTable更新组合框以显示 JTable 中的特定索引
【发布时间】:2014-01-23 22:24:14
【问题描述】:

我在位于 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 来修改一些功能。这使我可以灵活地选择包含组合框的特定单元格,我目前正在尝试将其修改为解决方案。

【问题讨论】:

  • 等等,到底是什么问题?什么不起作用?
  • 抱歉,刚刚通读一遍,问题不会跳出来,我基本上想通过指定索引来更新 JTable 中的 ComboBox,但是这样做后表格不会更新.它在每一行将自己设置为 index(0)。

标签: java jtable jcombobox tablecelleditor cellrenderer


【解决方案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);

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 2019-02-26
    • 2011-12-21
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多