当我们点击自定义时如何关闭JCombobox的下拉列表?

时间:2016-08-02 11:19:14

标签: java swing jcombobox

我有一个JCombobox,当我从JCombobox的下拉列表中选择任何一个时,所选项目正在打开但是当我点击" Custom"在其中一个下拉列表中我必须打开一个daiolg,这里daiolg正在打开,但是下拉列表没有关闭我想在我点击Custom时隐藏下拉列表。这是我的示例代码....

private PropertyChangeSupport   pcs;///here Iam using ActionListener and PopupMenuListener
public void actionPerformed(ActionEvent ae){
if(ae.getSource() instanceof  ComboBox )
{
ComboBox comboBox = (ComboBox)ae.getSource();
Object selectedItem = comboBox.getSelectedItem();
if(selectedItem != null && (!selectedItem.equals("(Custom..)")))
{
pcs.firePropertyChange("ITEM_SELECTED",getCaption(),null);  
}}}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) 
{
ComboBox comboBox = (ComboBox)e.getSource();
Object repeatedSelectedItem = comboBox.getSelectedItem();
if(repeatedSelectedItem != null && repeatedSelectedItem.equals("(Custom..)"))
{
invokeCustomFilterDialog(repeatedSelectedItem, comboBox);
}}  
private void invokeCustomFilterDialog(Object repeatedSelectedItem, ComboBox comboBox)
{
customFilterDialog.showDialog();    //here Iam opening dailog...
if(customFilterDialog.isCustomFilterAppliedFlag() == true)
{                   
pcs.firePropertyChange("ITEM_SELECTED",getCaption(),null);
}
else
{comboBox.setSelectedItem(lastSelectedItem);}}
public void popupMenuCanceled(PopupMenuEvent e) 
{ }
public void popupMenuWillBecomeVisible(PopupMenuEvent e) 
{
ComboBox comboBox = (ComboBox)e.getSource();
this.lastSelectedItem = comboBox.getSelectedItem();
}

2 个答案:

答案 0 :(得分:1)

您可以使用SwingUtilities.invokeLater

例如

public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        final JComboBox comboBox = (JComboBox) e.getSource();
        final Object repeatedSelectedItem = comboBox.getSelectedItem();
        if (repeatedSelectedItem != null
                && repeatedSelectedItem.equals("(Custom..)")) {
            SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      invokeCustomFilterDialog(repeatedSelectedItem, comboBox);
                  }
            });

        }
    }

答案 1 :(得分:0)

combobox.getUI()。setPopupVisible(combobox,false);