2
public class TablePanel extends JPanel implements ActionListener,Serializable
{
    JTable m_table;
    JComboBox combo,combo1;
    DefaultTableModel model=new DefaultTableModel();
    DefaultComboBoxModel model1=new DefaultComboBoxModel();
    DefaultComboBoxModel model2=new DefaultComboBoxModel();
        List<String> field;
    List<String> attrCode;
    TablePanel()
    {

            m_table=new JTable(model);
            m_table.setBackground(Color.WHITE);
            model.addColumn("col1");
            model.addColumn("col2");
            model.addColumn("col3");
            model.addColumn("col4");
            model.addColumn("col5");
            model.addColumn("col6");
            JScrollPane scrollpane=new JScrollPane(m_table);
            scrollpane.setBackground(Color.WHITE);
            Dimension d = m_table.getPreferredSize();
            scrollpane.setPreferredSize(
                new Dimension(d.width,m_table.getRowHeight()*15+1));
            add(scrollpane);

            }

                       attrCode = service.getAllAttributes(value);
               combo1=new JComboBox(model2);
               model1.addElement(attrCode.get(0));
               model1.addElement(attrCode.get(1));
            model1.addElement(attrCode.get(2));
            model1.addElement(attrCode.get(3));
            model1.addElement(attrCode.get(4));
            model1.addElement(attrCode.get(5));
            model1.addElement(attrCode.get(6));
            col=m_table.getColumnModel().getColumn(0);
            col.setCellEditor((new DefaultCellEditor(combo1)));
            combo2=new JComboBox(model3);
            model3.addElement(trans.get(0));
            model3.addElement(trans.get(1));
            model3.addElement(trans.get(2));
            model3.addElement(trans.get(3));
            model3.addElement(trans.get(4));
            col=m_table.getColumnModel().getColumn(2);

            col.setCellEditor((new DefaultCellEditor(combo2)));} }

I have a table, there are some columns in table. two columns has the combo box now what I want to do is,when user selects some value from column1 combobox, based on the user selected value column2 combobox should be populated. for example if user selects value1 from column1 combobox then column2 combobox will show values corresponding to value1 only.

Nidhi
  • 217
  • 1
  • 4
  • 14

1 Answers1

5

Render both the columns.

TableColumn comboCol1 = table.getColumnModel().getColumn(0);
TableColumn comboCol2 = table.getColumnModel().getColumn(1);
comboCol1.setCellEditor(new CustomComboBoxEditor());
comboCol2.setCellEditor(new CustomComboBoxEditor());

// This is for 2nd Column which depends on the first column selection.

public class CustomComboBoxEditor extends DefaultCellEditor {

// Declare a model that is used for adding the elements to the `ComboBox`
private DefaultComboBoxModel model;

public CustomComboBoxEditor() {
    super(new JComboBox());
    this.model = (DefaultComboBoxModel)((JComboBox)getComponent()).getModel();
}

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {


   if(column == 0) {
         // Just show the elements in the JComboBox.         
    } else {

           // Remove previous elements every time.
           // So that we can populate the elements based on the selection.
           model.removeAllElements();

           // getValueAt(..) method will give you the selection that is set for column one.
           String selectedItem = table.getValueAt(row, 0);

          // Using the obtained selected item from the first column JComboBox 
          // selection make a call ans get the list of elements.

         // Say we have list of data from the call we made. 
         // So loop through the list and add them to the model like the following.
         for(int i = 0; i < obtainedList.size(); i++) {
                model.addElement(obtainedList.get(i));
         } 
     } // Close else

    // finally return the component.
    return super.getTableCellEditorComponent(table, value, isSelected, row, column);
 }
}
Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • thnx for the reply. :) you mean to say that there should be one renderer like TableColumn comboCol1; and this is used for both the rows of a table like comboCol1 = table.getColumnModel().getColumn(1); comboCol1 = table.getColumnModel().getColumn(0); – Nidhi Dec 04 '12 at 12:55
  • sorry. My doubt is in my code i have already define a cell renderer TableColumn col; Do I need to define different cell renderers for the two columns containing combobox?? – Nidhi Dec 04 '12 at 13:08
  • Not necessary. One renderer class is sufficient. Do both columns require rendering with JComboBox .. – Amarnath Dec 04 '12 at 13:36
  • @Nidhi I have edited my code. Now it does what you want with only one rederer class. – Amarnath Dec 04 '12 at 14:17