I have been able to show a popup menu in my frame and the menu items, a group of radio buttons may be navigated with the arrow keys. When the user presses Enter/Return the popup menu is no longer visible and I assume that it is possible to determine which button was chosen and assign an action.
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);
I have also implemented key mapping for the whole frame like this:
/**
* 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);
}
How do I determine what radio button was selected before the user pressed Enter?