0

I have a panel with multiple comboboxes in each row, I need to fetch the combobox values from it.

this is my code:

gridPanel = new JPanel();
        grid = new GridLayout(0,1);
        gridPanel.setLayout(grid);

        gridPanel.add(createChildPanel());

and the createChildPanel method:

JComboBox columnACB = new JComboBox();
        columnACB.addItemListener(this);

thanks for the solution: this is what we used

Component[] comps = gridPanel.getComponents();
             for (Component comp : comps) {
                 if (comp instanceof JPanel) {
                     JPanel panel = (JPanel) comp;
                     Component[] comps1 = panel.getComponents();
                     for (Component comp1 : comps1) {
                         if (comp1 instanceof JComboBox)
                     {
                     JComboBox combp = (JComboBox) comp1;
                     String colA = combp.getSelectedItem().toString();
                     System.out.println("colA"+colA);
                     }
                         else if  (comp1 instanceof JTextField)
                         {
                             JTextField combp = (JTextField) comp1;
                             String colA = combp.getText();
                             System.out.println("colA"+colA);
                             }
                     }

                 }
             }
user2176576
  • 734
  • 3
  • 14
  • 39
  • What's wrong with fetching the values with the `JComboBox` method [getSelectedItem](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html#getSelectedItem%28%29)? – David Yee Jul 07 '14 at 11:12
  • Let me be more specific, my grid adds comboboxes in each row dynamically..Now I need to iterate through all the rows in the grid and access the combobox in each row, only when I have access to the JCombobox can I use getSelectedItem right? – user2176576 Jul 07 '14 at 11:24
  • possible duplicate of [How do I recursively disable my components in Swing?](http://stackoverflow.com/questions/13920279/how-do-i-recursively-disable-my-components-in-swing) – David Yee Jul 07 '14 at 11:37

1 Answers1

1

you can iterate over components in your panel using getComponents().

Component[] comps = gridPanel.getComponents();
for (Compoent comp : comps) {
    if (comp instanceof JComboBox) {
        JComboBox combo = (JComboBox) comp;
        Object selected = combo.getSelectedItem();
    }
}
rob
  • 1,286
  • 11
  • 12