Reputation: 3665
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?
Upvotes: 2
Views: 832
Reputation: 51525
Just the same as you would with radioButtons which are not in a menu :-) Use Actions:
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
Upvotes: 4
Reputation: 347234
The value of the result is still stored in the component's you added to the popup menu.
You need to implement a PopupMenuListener to your popup and monitor popupMenuWillBecomeInvisible
method.
When called, you will need to interrogate the components you had on the popup menu for the one that is selected.
If you still have a reference to them directly (ie with in the class context), you can access them directly.
The next best would be achieved by sub classing the popup menu.
public class ChoicesPopupMenu exends JPopupMenu {
JRadioButtonMenuItem rbAClock;
JRadioButtonMenuItem rbBClock;
JRadioButtonMenuItem rbCClock;
// Construct the UI ...
public boolean isAClockSelected() {
return rbAClock.isSelected();
}
// Other selection checkers...
}
Alternatively, you could walk the component hierarchy of the 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...
}
}
}
}
Upvotes: 0