如何在JComboBox中打开菜单?

时间:2023-01-29 12:23:21

I have create a custom JComboBox, so if I try to open my comboBox, I can see the check Button near the description. This is ok. But if I want to select n items I must, open the select list, then check one items, re-open the select list, select another items, open the select list etc... I want to open the select list one times then select the list that I want than close the select list. It is possibile to do it?

我已经创建了一个自定义的JComboBox,所以如果我尝试打开我的comboBox,我可以看到描述附近的检查按钮。还行吧。但是,如果我想选择n项,我必须打开选择列表,然后检查一个项目,重新打开选择列表,选择其他项目,打开选择列表等...我想打开选择列表一次然后选择我想要的列表而不是关闭选择列表。这样做有可能吗?

This is the CheckComboStore

这是CheckComboStore

public class CheckComboStore
{
    String id;
    Boolean state;
    String nomeArticolo;

    public CheckComboStore(String id, String nomeArticolo,Boolean state)
    {
        this.id = id;
        this.nomeArticolo=nomeArticolo;
        this.state = state;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Boolean getState() {
        return state;
    }

    public void setState(Boolean state) {
        this.state = state;
    }

    public String getNomeArticolo() {
        return nomeArticolo;
    }

    public void setNomeArticolo(String nomeArticolo) {
        this.nomeArticolo = nomeArticolo;
    }        
}

This is the code to create a comboBox with check button

这是使用复选按钮创建comboBox的代码

   List<Articoli> listaArticoli = modelManager.getArticoliManager().estraiArticoli(false,false,false,false,false);
        CheckComboStore[] stores = new CheckComboStore[listaArticoli.size()];
        int i=0;
        for(Iterator<Articoli>it=listaArticoli.iterator(); it.hasNext();){
            Articoli art = it.next();
            stores[i] = new CheckComboStore(art.getCodArticoloString(),art.getNomeArticolo(),false);
            i++;
        }
    comboBoxArticoli = new ComboFormat(stores);
    comboBoxArticoli.setRenderer(new CheckComboRenderer());

2 个解决方案

#1


1  

May be you can try adding a listener as shown below and when an item is selected you can use invokeLater and keep the popup open. It may not be the exact solution but will give you an idea. Let me know if it works for you?

可能您可以尝试添加一个监听器,如下所示,当选择一个项目时,您可以使用invokeLater并保持弹出窗口打开。它可能不是确切的解决方案,但会给你一个想法。请让我知道这对你有没有用?

combo.addItemListener(new ItemListener() {

      @Override
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          if (e.getItem() == combo.getItemAt(0)) //some condition {

            SwingUtilities.invokeLater(new Runnable() {

              @Override
              public void run() {
                combo.setSelectedItem(lastSelectedItem);
                combo.showPopup();
              }
            });
          } else {
            lastSelectedItem = combo.getSelectedItem();
          }
        }
      }
    });

#2


0  

Add an ItemListener to your combo and override itemStateChanged to call showPopup() (but don't call it on the Event Dispatch Thread, use SwingUtilities.InvokeLater()).

将ItemListener添加到组合并覆盖itemStateChanged以调用showPopup()(但不要在Event Dispatch Thread上调用它,使用SwingUtilities.InvokeLater())。

#1


1  

May be you can try adding a listener as shown below and when an item is selected you can use invokeLater and keep the popup open. It may not be the exact solution but will give you an idea. Let me know if it works for you?

可能您可以尝试添加一个监听器,如下所示,当选择一个项目时,您可以使用invokeLater并保持弹出窗口打开。它可能不是确切的解决方案,但会给你一个想法。请让我知道这对你有没有用?

combo.addItemListener(new ItemListener() {

      @Override
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          if (e.getItem() == combo.getItemAt(0)) //some condition {

            SwingUtilities.invokeLater(new Runnable() {

              @Override
              public void run() {
                combo.setSelectedItem(lastSelectedItem);
                combo.showPopup();
              }
            });
          } else {
            lastSelectedItem = combo.getSelectedItem();
          }
        }
      }
    });

#2


0  

Add an ItemListener to your combo and override itemStateChanged to call showPopup() (but don't call it on the Event Dispatch Thread, use SwingUtilities.InvokeLater()).

将ItemListener添加到组合并覆盖itemStateChanged以调用showPopup()(但不要在Event Dispatch Thread上调用它,使用SwingUtilities.InvokeLater())。