This example has Radio Buttons located on a Sub Menu as seen here.
What I would like to do is anytime the "Change Radio Button" button is pressed, it will change which button is selected on the menu. This means it has to first retrieve which is currently set then select the other.
Granted for this simple sample the Radio Buttons could be made instance variables to make things easy but image the JMenu and associated sub-menus and radio buttons are generated is some class further down in the bowels of the program. Direct access is not that direct.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
public class RadioButtonsOnMenu
{
public static void main(final String args[])
{
JFrame frame = new JFrame("MenuSample Example");
JButton jButton = new JButton("Change Radio Button");
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("Changing Radion Button");
//How to change the JButton on the menu?
//frame.getMenuBar().......
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jPanel = new JPanel();
jPanel.add(jButton);
frame.add(jPanel);
frame.setJMenuBar(buildMenu());
frame.setSize(350, 250);
frame.setVisible(true);
}
public static JMenuBar buildMenu()
{
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
fileMenu.add(newMenuItem);
JMenu findOptionsMenu = new JMenu("Options");
findOptionsMenu.setMnemonic(KeyEvent.VK_O);
fileMenu.add(findOptionsMenu);
ButtonGroup directionGroup = new ButtonGroup();
JRadioButtonMenuItem forwardMenuItem = new JRadioButtonMenuItem("Forward", true);
forwardMenuItem.setMnemonic(KeyEvent.VK_F);
findOptionsMenu.add(forwardMenuItem);
directionGroup.add(forwardMenuItem);
JRadioButtonMenuItem backwardMenuItem = new JRadioButtonMenuItem("Backward");
backwardMenuItem.setMnemonic(KeyEvent.VK_B);
findOptionsMenu.add(backwardMenuItem);
directionGroup.add(backwardMenuItem);
return menuBar;
}
}
It is not clear how best to access the sub-menu and associated radio button settings within the JButton action.
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("Changing Radion Button");
//How to change the JButton on the menu?
//frame.getMenuBar().......
}
});
I could probably get the Menu Bar from the Frame and drill down figure the code can get messy if there Menu Bar has numerous items, sub items and even multiple radio button groups.
Is there a more direct way to find out which Radio Buttons on the menu are selected as well as a more direct way to change their value?

The "trick" is to create an application model to hold the value of the menu radio buttons.
Here's the GUI I created.
I started the Swing application with a call to the
SwingUtilitiesinvokeLatermethod. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.I created a
JFrameand aJButtonJPanel. I separated the creation of theJFrameandJPanel.I created an application model class to hold a boolean which determines whether forward or backward is selected. The
JButtonActionListenerswitches the state of the boolean. TheupdateRadioButtonMenumethod updates the selected state of the radio button menu items.Here's the complete runnable code.