0

i have 3 JCombobox IN MY Converter program one for the category and two for the selection of function to perform ; what i want the program to do is when the user selects a category in one JCombobox the two JCombobox will automatically change content that is related to the selected category (pls help me with the function of JCombobox Change Items when one is selected the other chage values);

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • 2
    1) [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Mar 04 '12 at 07:55

4 Answers4

5

As JB Nizet says, here is a short example:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class ConnectedComboBoxDemo extends JFrame implements ActionListener {

    private static final long serialVersionUID = 6108579736224814080L;

    private JPanel panel;
    private JComboBox combo1;
    private JComboBox combo2;

    public ConnectedComboBoxDemo() {
        panel = new JPanel();
        String[] combo1Item = new String[]{"val1", "val2", "val3"};
        combo1 = new JComboBox(combo1Item);
        combo1.addActionListener(this);

        String[] combo2Item = new String[]{"val11", "val12", "val13"};
        combo2 = new JComboBox(combo2Item);

        panel.setLayout(new BorderLayout());
        panel.add(combo1, BorderLayout.WEST);
        panel.add(combo2, BorderLayout.EAST);

        setContentPane(panel);
        pack();
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String selectedValue = combo1.getSelectedItem().toString();
        String[] combo2Item = null;

        DefaultComboBoxModel model = (DefaultComboBoxModel) combo2.getModel();      
        model.removeAllElements();

        if(selectedValue.equals("val1")){
            combo2Item = new String[]{"val11", "val12", "val13"};
        } else if(selectedValue.equals("val2")){
            combo2Item = new String[]{"val21", "val22", "val23"};
        } else if(selectedValue.equals("val3")){
            combo2Item = new String[]{"val31", "val32", "val33"};
        }

        for(String val : combo2Item){
            model.addElement(val);
        }
    }   

    public static void main(String... args){
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ConnectedComboBoxDemo();
            }
        });
    }
}

Hope this will help you.

Community
  • 1
  • 1
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • 1
    +1 See also this [example](http://stackoverflow.com/a/3191882/230513) that constructs the models just once. – trashgod Mar 04 '12 at 10:12
3

It's simply a matter of adding an ActionListener to the first combo box and, in the action listener's actionPerformed method, get the selected value in the first combo, and change the values displayed by the second combo.

Read the Swing tutorial about combo boxes.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
3

Use ItemListener rather than ActionListener for JComboBox, put both Listener together example about ActionListener with ItemListener for two JComboBoxes

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Why is your reason to prefer an ItemListener over an ActionListener. Two ItemEvents will be fired for each selection changes, and the swing tutorial recommends using an ActionListener. – JB Nizet Mar 04 '12 at 08:12
  • I think (all my answers everywhere) that all changes from JComboBox to the GUI is about ItemListener(SELECTED/DESELECTED), and to itself is about [ActionListener](http://stackoverflow.com/questions/5911865/jcombobox-determine-if-items-are-arent-visible-in-drop-down-list), – mKorbel Mar 04 '12 at 08:21
  • I'm not sure I have fully understood, but that's not the case. Each time the selection changes, the combo box fires an ActionEvent. – JB Nizet Mar 04 '12 at 08:31
  • 1
    basically you're answered the reason in your last comment, by using ItemListener is possible to filtering from two events, you knows which Item was lastSelected ..., ActionListener is only about one event, – mKorbel Mar 04 '12 at 09:47
-1

try to use itemStateChanged (java.awt.event.ItemEvent evt)

xxx
  • 3,315
  • 5
  • 21
  • 40