JTableCombo值在运行时不更改

时间:2012-03-24 08:16:33

标签: java swing jtable jcombobox

实际上我有一个JTable,其中一个ColumnComboBox,我想要的是在运行时更改JTable的{​​{1}}的值,在ComboBox值更改事件中,我调用的函数为我获取了Combo的新值,但值没有变化。 CheckBox是获取util.RefreshReplicatedDataAndHeader(true,objVector,-1,this, jScroll_ProductStone.getName());数据的函数。它正在呼叫JTable。我提供了代码的一部分,我用它来改变组合值和实际的函数,它使我的JTable组合更改了值。非常感谢提供解决问题的方法。

AddComboBoxForJTable

为JTable添加组合框的代码如下

if (thaiFlag)
{       
    combo_Id_Name_tbl.add("Setting");// Table Name
    combo_Id_Name_tbl.add("SetID"); //Combo Id
    combo_Id_Name_tbl.add("SetThaiName");// Combo Val
    combo_Id_Name_tbl.add("Setting"); //  Identifer Of Table For Two Combo FromSameTable
    combo_Id_Name_tbl.add("Please select required Setting For Selected Lot");
    combo_Id_Name_tbl.add("SetID");// ColId For Selected Combo
    combo_Id_Name_tbl.add("SetName"); // Col Val For Selected Combo
    combo_Id_Name_tbl.add("N/A");    // Col Id For Non JTable Selected Cobo
    combo_Id_Name_tbl.add("N/A"); // Combo Filter Col Name
    combo_Id_Name_tbl.add("N/A"); // Combo Filter Col Value
    ColId_Val_Identifier_TableName_ForCombo.add(combo_Id_Name_tbl);       
}
else
{
    combo_Id_Name_tbl.add("Setting");// Table Name
    combo_Id_Name_tbl.add("SetID"); //Combo Id
    combo_Id_Name_tbl.add("SetName");// Combo Val
    combo_Id_Name_tbl.add("Setting"); //  Identifer Of Table For Two Combo From Same Table
    combo_Id_Name_tbl.add("Please select required Setting For Selected Lot");
    combo_Id_Name_tbl.add("SetID");// ColId For Selected Combo
    combo_Id_Name_tbl.add("SetName"); // Col Val For Selected Combo
    combo_Id_Name_tbl.add("N/A");    // Col Id For Non JTable Selected Cobo
    combo_Id_Name_tbl.add("N/A"); // Combo Filter Col Name
    combo_Id_Name_tbl.add("N/A"); // Combo Filter Col Value
    ColId_Val_Identifier_TableName_ForCombo.add(combo_Id_Name_tbl);
}


    TableColumnModel tcm = jtblSizeInfo.getColumnModel();
    objVector=null;
    objVector=new Vector<Object>();
    objVector.add(data_SizeInfo);
    objVector.add(header_SizeInfo);
    objVector.add(data_ForSave_SizeInfo);
    objVector.add(colHeader_ForSave_SizeInfo);
    objVector.add(AddedCols_Name_Pos);
    objVector.add(ColId_Val_Identifier_TableName_ForCombo);
    objVector.add(tcm);
    Vector<Object> objResultVector = util.RefreshReplicatedDataAndHeader(true, objVector,-1, this, jScroll_ProductStone.getName());


    data_SizeInfo =(Vector<Vector<String>>)objResultVector.get(0) ;
    header_SizeInfo=(Vector<String>)objResultVector.get(1) ;
    data_ForSave_SizeInfo =(Vector<Vector<String>>)objResultVector.get(2) ;
    colHeader_ForSave_SizeInfo=(Vector<String>)objResultVector.get(3) ; 
    TableColumnModel tcm1 = (TableColumnModel)objResultVector.get(4);



    // Column Model Is used to get the Columns And Action Listnner Added In Utility Form 
    Vector<TableColumn> vtc = new Vector<TableColumn>();
    int colCount_Ref = tcm.getColumnCount();

    for (int i=0;i<colCount_Ref;i++ )
    {
        vtc.add(tcm.getColumn(i));        
    }


    for (int i=0;i<vtc.size();i++ )
    {             
        tcm.removeColumn(vtc.get(i) ); 
    }

    for (int i=0;i<tcm1.getColumnCount();i++ )
    {
        tcm.addColumn(tcm1.getColumn(i)); 
    }
    jtblSizeInfo.setColumnModel(tcm);
    jtblSizeInfo.repaint();
    jtblSizeInfo.revalidate();
}

1 个答案:

答案 0 :(得分:2)

永远不要直接将Swing组件放在表模型中。从正确的重新绘制到传递关键和鼠标事件,存在大量问题。

您要做的是宣布JComboBox as a default editor。我认为它甚至是字符串数组的默认编辑器(例如JCheckBox是布尔值的默认编辑器)。因此,您只需将这些数组放入模型中,让JTable的魔法为您完成所有工作