Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: JMenu disappears when JRadioButtons or JCheckBoxes are used

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default JMenu disappears when JRadioButtons or JCheckBoxes are used

    I have created a JMenuBar with a Pizza JMenu. The JMenu has a 3 JRadioButtons and 2 JCheckBoxes that are appear when menuItems are scrolled over. The problem is that when I click on a JRadioButton or JCheckBox, the whole menu disappears. Is there a particular method or any other way I can stop this from happening?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class MenuExamplePizza extends JFrame
    {
    	private JMenuBar menuBar;
    	private JMenu menu, sizeMenu, extrasMenu, helpMenu;
    	private JMenuItem menuItem;
    	private JRadioButtonMenuItem smallrbMenuItem, medrbMenuItem, largerbMenuItem;
    	private JCheckBoxMenuItem peppchMenuItem, anchMenuItem;
     
    	public MenuExamplePizza()                                       						// Default Constructor
    	{
    	   menuBar = new JMenuBar();																		//Create the menu bar
     
    		menu = new JMenu("Pizza");																		//Build the first menu
    		menu.setMnemonic('P');
    		menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
    		menuBar.add(menu);
     
    		menuItem = new JMenuItem("Total");
    		menuItem.setMnemonic('T');
    		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
    		menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
    		menu.add(menuItem);
    		menu.addSeparator();
     
    		sizeMenu = new JMenu("Size");
    		sizeMenu.setMnemonic('S');
    		ButtonGroup group = new ButtonGroup();
    		smallrbMenuItem = new JRadioButtonMenuItem("Small");
    		group.add(smallrbMenuItem);
    		sizeMenu.add(smallrbMenuItem);
    		medrbMenuItem = new JRadioButtonMenuItem("Medium");
    		group.add(medrbMenuItem);
    		sizeMenu.add(medrbMenuItem);
    		largerbMenuItem = new JRadioButtonMenuItem("Large");
    		group.add(largerbMenuItem);
    		sizeMenu.add(largerbMenuItem);
    		menu.add(sizeMenu);
     
    		extrasMenu = new JMenu("Extras");
    		extrasMenu.setMnemonic('E');
    		peppchMenuItem = new JCheckBoxMenuItem("Pepperoni");
    		extrasMenu.add(peppchMenuItem);
    		anchMenuItem = new JCheckBoxMenuItem("Anchovies");
    		extrasMenu.add(anchMenuItem);
    		menu.add(extrasMenu);
     
    		menu.addSeparator();																		// a group of radio button menu items																		
     
    		menuItem = new JMenuItem("Exit");
    		menuItem.setMnemonic('x');
    		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
    		menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
    		menu.add(menuItem);
     
    		helpMenu = new JMenu("Help");															//Build second menu in the menu bar.
     
    		menuBar.add(menu);
    		menuBar.add(helpMenu);
    		add(menuBar);
    		setJMenuBar(menuBar);
    	}
     
    	public static void main(String[] args) 
       {
    	   MenuExamplePizza frame = new MenuExamplePizza();
    	   frame.setTitle("Pizza Menu");
    	   frame.setSize(300, 250);
    	   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	   frame.setLocationRelativeTo(null);	 
    	   frame.setVisible(true);
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JMenu disappears when JRadioButtons or JCheckBoxes are used

    Are you talking about the menu closing when an item is selected in the menu?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: JMenu disappears when JRadioButtons or JCheckBoxes are used

    Yes that's it

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JMenu disappears when JRadioButtons or JCheckBoxes are used

    Yeah, looks stupid to do that.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: JMenu disappears when JRadioButtons or JCheckBoxes are used

    But I don't want the menu to close when I click something, I want it to stay open, how would I do this?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JMenu disappears when JRadioButtons or JCheckBoxes are used

    I guess the code must think there is nothing more to do once you have made the selection.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Create JRadioButtons from HashMap by loop
    By poundnmonitor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 30th, 2013, 09:00 AM
  2. [SOLVED] JCheckboxes Item Listener
    By remedys in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 4th, 2012, 08:14 AM
  3. JMenuBar disappears half the time when using a certain line of code?
    By !Marcus in forum Java Theory & Questions
    Replies: 2
    Last Post: December 26th, 2011, 04:05 PM
  4. Liitle help with Jmenu
    By cute in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 13th, 2011, 08:09 PM
  5. setVerifyInputWhenFocusTarget problem with JMenu
    By Jack_Maloney in forum AWT / Java Swing
    Replies: 0
    Last Post: March 25th, 2011, 10:44 AM