JTableCombo值在运行时不更改

时间:2023-01-28 14:33:08

Actually i have a JTable in which one Column has ComboBox , what i want is to change the values of JTable's ComboBox at runtime , on the CheckBox value changed event , I am calling the function which gets me New Values For Combo but there is no change in the values. util.RefreshReplicatedDataAndHeader(true,objVector,-1,this, jScroll_ProductStone.getName()); is the function which gets me JTable Data. It is calling AddComboBoxForJTable. I am provide the part of the code which i am using for changing combo values and actual Function Which gets me JTable combo with the changed value. Lots of thanks in advance for providing solution to the problem.

实际上我有一个JTable,其中一个列有ComboBox,我想要的是在运行时更改JTable的ComboBox的值,在CheckBox值更改事件,我调用的函数让我获得Combo的新值但是没有变化在价值观中。 util.RefreshReplicatedDataAndHeader(true,objVector,-1,this,jScroll_ProductStone.getName());是获取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();
}

The Code For Adding Combo Box for JTable is as follows

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

public void  AddComboBoxForJTable( int colIndex,Vector<String>vectTable_JtblColIdVal_ComboIdentify, int selectedRow,Object          currentFormObj,String currentJScrollPane ) 
{   
    ////// JComboBox comboBox=null;  
    try 
    {
        String tableName =vectTable_JtblColIdVal_ComboIdentify.get(0);
        String comboId =vectTable_JtblColIdVal_ComboIdentify.get(1);
        String comboValue =vectTable_JtblColIdVal_ComboIdentify.get(2);
        String comboIdentifier =vectTable_JtblColIdVal_ComboIdentify.get(3);
        String comboFilterColName =vectTable_JtblColIdVal_ComboIdentify.get(8);
        String comboFilterColValue =vectTable_JtblColIdVal_ComboIdentify.get(9);
        Vector<String> comboFilter =new Vector<String>();
        comboFilter.add(comboFilterColName);
        comboFilter.add(comboFilterColValue);

        Vector<Vector<String>> comboData=new Vector<Vector<String>>();
        //JComboBox comboBox;  
        DBEngine dbe = new DBEngine();
        if (comboFilterColName.equals("N/A"))
        {
            comboData = dbe.getComboData(tableName, comboId, comboValue,comboValue);
        }
        else 
        {
            comboData = dbe.getComboData(tableName, comboId, comboValue,comboValue,comboFilter);   
        }

        Vector model = new Vector();       

        for (int i =0;i<comboData.size();i ++)
        {                    
            //  model.addElement( new CustomizedComboBox(3, "train" ) );  
            String id =comboData.get(i).elementAt(0).toString();
            String data= comboData.get(i).elementAt(1).toString();
            Item_ it=  new Item_(id,data );

            model.addElement( it );        

        }


        JComboBox comboBox = new JComboBox( model );     


        MyActionListenerForJtableCombo_Utility actionListener = new MyActionListenerForJtableCombo_Utility( 
        vectTable_JtblColIdVal_ComboIdentify, selectedRow,   currentFormObj,    currentJScrollPane);    
        actionListener.util=this;
        actionListener.tableName=tableName;

        comboBox.addActionListener(actionListener);  




        // DefaultTableModel tblModel = (DefaultTableModel)jtblUtilities.getModel();
        TableColumn column= jtbl_General.getColumnModel().getColumn(colIndex);
        column.setCellEditor(new javax.swing.DefaultCellEditor(comboBox));
    } 
    catch (Exception ex)
    {
        ex.printStackTrace();
        //  Logger.getLogger(ComboBoxEditor.class.getName()).log(Level.SEVERE, null, ex);
    }
}

1 个解决方案

#1


2  

Never ever ever ever put Swing components inside table model directly. There is a sea of problems with that, from correct repainting to passing key and mouse events.

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

What you want to do is to declare JComboBox as a default editor. I think that it even is the default editor for arrays of Strings (such as JCheckBox is default editor for boolean values). So you just need to put those arrays in the model and let JTable's magic do all the work for you

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

#1


2  

Never ever ever ever put Swing components inside table model directly. There is a sea of problems with that, from correct repainting to passing key and mouse events.

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

What you want to do is to declare JComboBox as a default editor. I think that it even is the default editor for arrays of Strings (such as JCheckBox is default editor for boolean values). So you just need to put those arrays in the model and let JTable's magic do all the work for you

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