Java Swing drop down list

user21 picture user21 · Feb 26, 2014 · Viewed 11.7k times · Source

I have created a drop down list using JAVA Swing. When I select "Keep track of status of RCM:", I want to create another drop down list next to the option selected. should I use mouseactionlistener instead? I trying to accomplish something like in this, when I click menu options, there is another list which I can select under menu options category. Example : http://smoothjazztampabay.com/wp-content/rockettheme/rt_metropolis_wp/menu-options/dropdownmenu.jpg

I tried using this code but couldn't.

         if (state == ItemEvent.SELECTED)
        {
            ItemSelectable itemS = itemEvent.getItemSelectable();
            String cmd = selectedString(itemS);
            if ( cmd.equals("Keep track of status of RCM:"))
            {
                RCMCombo2.addItem(RCMCombo);
                selectionPanel.add(RCMCombo2);
            }

The full version of the code is shown as below:

    String [] RCM2 = {"Keep track of status of RCM:", "Add and activate RCM", "Remove RCM", 
     "Display the usage statistics for RCM", 
"Update capabilities of RCMs", "Show RCM used most frequently in the last n days",
"Display number of times the RCM was emptied in n hours"};


RCMCombo2 = new JComboBox(RCM2); 
RCMCombo2.addItemListener(itemListener);  
    RCMCombo2.setEditable(false);
    RCMCombo2.setBounds(10,10,10,30);

    //"Updates capabilities of RCM"); Get the location of RCM");

    selectionPanel.add(RCMCombo2);

ItemListener itemListener = new ItemListener() 
{
      public void itemStateChanged(ItemEvent itemEvent) 
      {
        int state = itemEvent.getStateChange();
        //System.out.println((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
        //System.out.println("Item: " + itemEvent.getItem());
        if (state == ItemEvent.SELECTED)
        {
            ItemSelectable itemS = itemEvent.getItemSelectable();
            String cmd = selectedString(itemS);
            if ( cmd.equals("Keep track of status of RCM:"))
            {
                RCMCombo2.addItem(RCMCombo2);
                selectionPanel.add(RCMCombo2);
            }

Any help will be appreciated. Thank you.

Answer

SebastianH picture SebastianH · Feb 26, 2014

It looks like you are on a completely wrong track. I think you are mixing the usage of ItemListener with the setup of the lists model. Check out this guide to ComboBox - it should contain all the information you need.