Java Swing 下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22039109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:12:42  来源:igfitidea点击:

Java Swing drop down list

javaswingjcomboboxitemlistenerdropdownlistfor

提问by user21

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

我使用 JAVA Swing 创建了一个下拉列表。当我选择“跟踪 RCM 的状态:”时,我想在所选选项旁边创建另一个下拉列表。我应该改用 mouseactionlistener 吗?我试图完成这样的事情,当我单击菜单选项时,我可以在菜单选项类别下选择另一个列表。示例: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.

任何帮助将不胜感激。谢谢你。

回答by SebastianH

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

看起来你走上了完全错误的轨道。我认为您将 的使用ItemListener与列表模型的设置混合在一起。查看ComboBox 指南- 它应该包含您需要的所有信息。