2013-07-09 43 views
3

所以我需要的功能是ComboBox,其在擴張時能正確點擊,然後彈出菜單顯示,顯示不同的動作。的JComboBox與JPopupMenu的作爲右擊選擇

對於我擴展JComboBox時用下面的構造函數:

public HistoryComboBox(DefaultComboBoxModel model) { 
    super(model); 
    super.setUI(new BasicComboBoxUI(this){ 

     @Override protected ComboPopup createPopup() { 
      return new HistoryComboPopup(comboBox); 
     } 
    }); 

並延伸BasicComboPopup創建的類HistoryComboPopup,並具有MouseListener的

@Override 
protected MouseListener createListMouseListener() { 
    if (handler2 == null) 
     handler2 = new Handler2(); 
    return handler2; 
} 

private class Handler2 implements MouseListener { 
    @Override 
    public void mouseClicked(MouseEvent e) { 
     JPopupMenu popup = new JPopupMenu(); 
     ... 
     popup.show(); 
    } 
} 

彈出作品,我可以處理它的菜單單擊。唯一的麻煩是,當我打開JPopupMenu時,組合框關閉,我點擊右鍵的項目不再被看到。

我目前的想法做這個東西,所以任何幫助,將大大受到讚賞。

編輯:我搗碎了一個可編譯例子:

public class MainPanel extends JPanel{ 
public MainPanel() { 
    super(new BorderLayout()); 

    JComboBox combo1 = makeComboBox(5); 
    combo1.setUI(new BasicComboBoxUI() { 
     @Override protected ComboPopup createPopup() { 
      return new HistoryComboPopup(comboBox); 
     } 
    }); 

    add(combo1,BorderLayout.NORTH); 
} 

private static JComboBox makeComboBox(int size) { 
    DefaultComboBoxModel model = new DefaultComboBoxModel(); 
    for(int i=0;i<size;i++) { 
     model.addElement("No."+i); 
    } 
    return new JComboBox(model); 
} 

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override public void run() { 
      JFrame frame = new JFrame("DisableRightClick"); 
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
      frame.getContentPane().add(new MainPanel()); 
      frame.pack(); 
      frame.setLocationRelativeTo(null); 
      frame.setVisible(true); 
     } 
    }); 
} 
} 

class HistoryComboPopup extends BasicComboPopup { 
private Handler2 handler2; 

@Override 
public void uninstallingUI() { 
    super.uninstallingUI(); 
    handler2 = null; 
} 

public HistoryComboPopup(JComboBox combo) { 
    super(combo); 
} 

@Override 
protected MouseListener createListMouseListener() { 
    if (handler2 == null) 
     handler2 = new Handler2(); 
    return handler2; 
} 

private class Handler2 implements MouseListener { 
    @Override public void mouseEntered(MouseEvent e) {} 
    @Override public void mouseExited(MouseEvent e) {} 
    @Override public void mouseClicked(MouseEvent e) {} 
    @Override public void mousePressed(MouseEvent e) {} 

    @Override 
    public void mouseReleased(MouseEvent e) { 
     if (SwingUtilities.isRightMouseButton(e)) { 
      JPopupMenu popup = new JPopupMenu(); 

      JMenuItem copymethod = new JMenuItem("copy"); 
      copymethod.addMouseListener(new MouseAdapter() { 

       @Override 
       public void mousePressed(MouseEvent e) { 
        System.out.println("copy clicked"); 
       } 
      }); 
      popup.add(copymethod); 
      popup.show(HistoryComboPopup.this, 
        e.getXOnScreen() - HistoryComboPopup.this.getLocationOnScreen().x, 
        e.getYOnScreen() - HistoryComboPopup.this.getLocationOnScreen().y); 

     } else { 
      comboBox.setSelectedIndex(list.getSelectedIndex()); 
      comboBox.setPopupVisible(false); 
     } 
    } 
} 
} 
+1

爲了更好地幫助越早,張貼[SSCCE](http://sscce.org/)。 –

+1

相關http://stackoverflow.com/questions/7423533/jtable-with-jpopupmenu?rq=1? – sanbhat

回答

1

所以我需要的功能是ComboBox,其在膨脹時可以 是正確點擊,然後彈出菜單顯示,顯示不同 行動。

  • popup.show();是不妥當的accelator

  • 是不可能直接,它太hard to override common bug

  • 搖擺不允許節目中兩人同時lightwieght彈出式組件

  • 使用JWindows或無裝飾​​的JDialog代替JPopup

代碼示例中示範的正式錯誤

import javax.swing.*; 
    import java.awt.event.*; 

    public class Test { 

     public static void main(String[] args) { 
      JFrame frame = new JFrame(); 
      frame.setSize(400, 400); 
      frame.setVisible(true); 
      String[] list = {"1", "2", "3", "4",}; 
      JComboBox comb = new JComboBox(list); 
      final JPopupMenu pop = new JPopupMenu(); 
      pop.add(comb); 
      frame.addMouseListener(new MouseAdapter() { 

       @Override 
       public void mousePressed(MouseEvent e) { 
        System.out.println("mousePressed"); 
        pop.show(e.getComponent(), e.getX(), e.getY()); 
       } 
      }); 
     } 
    } 
+1

錯誤是:a)不相關(據我理解這個問題,稍微困惑雖然:-)它是關於增加一個組合框的彈出,問題是如何顯示上單擊組合框的彈出式爲B項的另一彈出)封閉,自jdk5以來不可重現,它還在嗎? – kleopatra

+0

如果我們還在談論選擇a或b,在這兩種情況下是這樣的錯誤在這裏並不重要,彈出一走開, – mKorbel

+0

這不是因爲這樣的錯誤(和您引用問題不抱怨這一點,但約在BasicComboPopup)的腸子一個NPE - 彈出式的設計的行爲,正是因爲他們這樣做(ES卡恩淖爾einen geben :-) – kleopatra