2012-08-27 54 views
2

我已經能夠在我的框架和菜單項中顯示一個彈出菜單,一組單選按鈕可以用箭頭鍵導航。當用戶按Enter/Return時,彈出式菜單不再可見,我認爲可以確定選擇哪個按鈕並分配一個動作。如何捕捉用戶在jpopupmenu中選擇jradiobutton?

private static final JPopupMenu popupSyncTIR = new JPopupMenu(); 
    popupSyncTIR.setLabel("Sensor Synced to Clock"); 
    ButtonGroup grpTIR = new ButtonGroup(); 
    JRadioButtonMenuItem rbTIRMenuItem; 
    rbTIRMenuItem = new JRadioButtonMenuItem("Sync TIR-A to clock"); 
    rbTIRMenuItem.setFont(new Font("Dialog", Font.BOLD, 16)); 
    grpTIR.add(rbTIRMenuItem); 
    popupSyncTIR.add(rbTIRMenuItem); 
    rbTIRMenuItem = new JRadioButtonMenuItem("Sync TIR-B to clock"); 
    rbTIRMenuItem.setFont(new Font("Dialog", Font.BOLD, 16)); 
    rbTIRMenuItem.setSelected(true); 
    grpTIR.add(rbTIRMenuItem); 
    popupSyncTIR.add(rbTIRMenuItem); 
    rbTIRMenuItem = new JRadioButtonMenuItem("Sync TIR-C to clock"); 
    rbTIRMenuItem.setFont(new Font("Dialog", Font.BOLD, 16)); 
    grpTIR.add(rbTIRMenuItem); 
    popupSyncTIR.add(rbTIRMenuItem); 

我還實施了鍵映射整個框架是這樣的:

/** 
* alt-T period synch TIR sensor w/clock 
*/ 
@SuppressWarnings("serial") 
private static void registerSyncTIRAction() { 
    javax.swing.Action tirSync = new AbstractAction() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
      setTIRSensorSync(); 
     } 
    }; 
    KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_T, 
      java.awt.event.InputEvent.ALT_DOWN_MASK); 
    ((JComponent) contentPanel).getActionMap().put("TirSync", tirSync); 
    ((JComponent) contentPanel).getInputMap(
      JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "TirSync"); 

} 

/** 
* Show the TIR popup 
*/ 
private static void setTIRSensorSync() { 
    popupSyncTIR.show(pnlTIR, 10, 10); 
} 

如何確定之前用戶按下Enter鍵選擇什麼樣的單選按鈕?

+0

爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

4

就在同一組件層次,你會與單選按鈕其不在使用:-)動作的菜單:

Action actionA = new AbstractAction("Synch A to clock") { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     // do stuff A 
    } 
}; 
JRadioButtonMenuItem buttonA = new JRadioButtonMenuItem(actionA); 
Action actionB = new AbstractAction("Synch B to clock") { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     // do stuff B 
    } 
}; 
JRadioButtonMenuItem buttonB = new JRadioButtonItem(actionB); 
ButtonGroup ... // add buttons to group 
JPopupMenu ... // add buttons to menu 
+0

哈哈哈,好點 – MadProgrammer

+0

愚蠢的問題(對不起,機器人在電腦前在莫),動作事件會導致彈出窗口關閉或不?沒有理由詢問他人好奇 – MadProgrammer

+0

沒有在API中實現鼠標或鍵盤事件,彈出窗口始終關閉(從Java1.4開始)不添加監聽器 – mKorbel

0

結果的值仍然存儲在您添加到彈出菜單的組件中。

您需要實施PopupMenuListener到您的彈出窗口並且監視popupMenuWillBecomeInvisible方法。

當被調用時,您需要詢問您在彈出菜單中選擇的組件的組件。

如果您仍然直接引用它們(即在類上下文中),則可以直接訪問它們。

通過對彈出式菜單進行子分類可以實現下一個最佳效果。

public class ChoicesPopupMenu exends JPopupMenu { 

    JRadioButtonMenuItem rbAClock; 
    JRadioButtonMenuItem rbBClock; 
    JRadioButtonMenuItem rbCClock; 

    // Construct the UI ... 

    public boolean isAClockSelected() { 
     return rbAClock.isSelected(); 
    } 

    // Other selection checkers... 

} 

或者,你可以走的JPopupMenu

public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 
    JPopupMenu menu = (JPopupMenu)e.getSource(); 
    for (Component comp : menu.getComponents()) { 
     if (comp instanceof JRadioButton) { 
      JRadioButton rb = (JRadioButton)comp; 
      if (rb.isSelected()) { 
       // Figure out which one is selected... 
      } 
     } 
    } 
} 
+0

呃...不,不需要popupMenuListener。 – kleopatra