bircastri
bircastri

Reputation: 2167

How can I hold the menu open in the JComboBox?

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?

This is the 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

   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());

Upvotes: 0

Views: 664

Answers (2)

Gabriel Negut
Gabriel Negut

Reputation: 13960

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()).

Upvotes: 0

akhil_mittal
akhil_mittal

Reputation: 24157

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?

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();
          }
        }
      }
    });

Upvotes: 1

Related Questions