Syed Muhammad Mubashir
Syed Muhammad Mubashir

Reputation: 1016

JTableCombo Values Not Changing on runtime

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.

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

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

Upvotes: 0

Views: 308

Answers (1)

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

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.

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

Upvotes: 2

Related Questions