|
-
Oct 21st, 2001, 01:12 PM
#1
Thread Starter
Dazed Member
Nesting Menus? [Resolved]
What's the easiest way to nest Menus? You know create
submenus. I have somthing like this in one of my programs but
the last option only get the submenu, im not sure why this is so.
Code:
JMenuBar jmb = new JMenuBar();
JMenu options = new JMenu("Options");
JMenu settings = new JMenu();
JMenu help = new JMenu("Help");
JMenu novice = new JMenu("Novice");
JMenu inter = new JMenu("Intermedeate");
JMenu advanced = new JMenu("Advanced");
JRadioButtonMenuItem min1 = new JRadioButtonMenuItem("Min 1");
JRadioButtonMenuItem min3 = new JRadioButtonMenuItem("Min 3");
JRadioButtonMenuItem min5 = new JRadioButtonMenuItem("Min 5");
jmb.add(options);
jmb.add(help); // add JMenu
options.add(novice); // add the main JMenus
options.add(inter);
options.add(advanced);
bg.add(min1); // add JRadioButtonMenuItems too ButtonGroup
bg.add(min3);
bg.add(min5);
settings.add(min1); // add JRadioButtonMenuItems to JMenu
settings.add(min3);
settings.add(min5);
novice.add(settings); // add JMenu Setting to each JMenu
inter.add(settings);
advanced.add(settings);
Last edited by Dilenger4; Oct 26th, 2001 at 09:46 AM.
-
Oct 23rd, 2001, 02:04 PM
#2
Well ...
I tried a similar example at home, got a doubt, and checked it out in the JDK docs. My doubt was confirmed.
add
public MenuItem add(MenuItem mi)
Adds the specified menu item to this menu. If the menu item has been part of another menu, remove it from that menu
There you are. So, in order to add similar named menu items, you will have to create separate objects.
.
-
Oct 23rd, 2001, 03:54 PM
#3
Thread Starter
Dazed Member
Im not quite sure i follow you. I stripped (sounds kinky dosent it?) most of the code away. The addComps() method is just invoked in the constructor. Im adding a JMenu "options" to a JMenuBar, then im adding the (3) JMenu's to the JMenu "options" next to each of the JMenus im adding the JRadioButtonMenuItems.
I think the problem is when i am adding the (3) JMenus to the "options" JMenu. Sounds right?
Code:
static JMenuBar jmb = new JMenuBar();
static JMenu help = new JMenu("Help");
static JMenu options = new JMenu("Options");
static JMenu novice = new JMenu("Novice");
static JMenu inter = new JMenu("Intermedeate");
static JMenu advanced = new JMenu("Advanced");
static JRadioButtonMenuItem min1 = new JRadioButtonMenuItem("Min 1");
static JRadioButtonMenuItem min3 = new JRadioButtonMenuItem("Min 3");
static JRadioButtonMenuItem min5 = new JRadioButtonMenuItem("Min 5");
static ButtonGroup bg = new ButtonGroup();
static JFrame jf = new JFrame();
static JTextArea jta = new JTextArea();
static JTextField jtf = new JTextField();
private static void addComps(){
Container c = jf.getContentPane();
c.setLayout(new BorderLayout());
bg.add(min1);
bg.add(min3);
bg.add(min5);
jmb.add(options);
jmb.add(help);
options.add(novice);
options.add(inter);
options.add(advanced);
novice.add(min1);
novice.add(min3);
novice.add(min5);
inter.add(min1);
inter.add(min3);
inter.add(min5);
advanced.add(min1);
advanced.add(min3);
advanced.add(min5);
c.add(jmb,BorderLayout.NORTH);
c.add(jta,BorderLayout.CENTER);
c.add(jsb,BorderLayout.EAST);
c.add(jtf,BorderLayout.SOUTH);
}
-
Oct 24th, 2001, 03:04 AM
#4
Well ...
The problem is with the menu objects you are using. You create three objects min1, min3 and min5, and go adding them under each menu. But as soon as you add them to the second menu, they are removed from the first menu, and as you add them to the third menu, they are removed from the second menu.
So, if you add the same objects to a menu, they would be removed from any other menu they had been added to earlier.
And that means in order to show the same menu options, you will have to create separate menu item objects. For e.g. you can use mina1, mina3 and mina5 for the first menu, minb1, minb3 and minb5 for the second menu and so on. There must be separate instances of the menu items.
.
-
Oct 24th, 2001, 12:23 PM
#5
Thread Starter
Dazed Member
Ok i see now. This works pretty good but im going to look
for an alternate way to do this. I created seperate MenuItem
objects like so. And it works good but doing it in this manner i cant see a way to add the JMenuItems to a ButtonGroup because there is nothing to refrence them by. But i think i can fix it.
Code:
novice.add(new JRadioButtonMenuItem("Min 1"));
novice.add(new JRadioButtonMenuItem("Min 3"));
novice.add(new JRadioButtonMenuItem("Min 5"));
inter.add(new JRadioButtonMenuItem("Min 1"));
inter.add(new JRadioButtonMenuItem("Min 3"));
inter.add(new JRadioButtonMenuItem("Min 5"));
advanced.add(new JRadioButtonMenuItem("Min 1"));
advanced.add(new JRadioButtonMenuItem("Min 3"));
advanced.add(new JRadioButtonMenuItem("Min 5"));
-
Oct 24th, 2001, 03:15 PM
#6
Thread Starter
Dazed Member
I managed to condense the code and this seems to be a better alternative. So instead of all of that block coding i just end up with two loops. Thanks for your help.
Code:
static JRadioButtonMenuItem jrbmi;
static String[] minutes = {"Min 1","Min 3","Min 5"};
JMenu[] jm = {novice,inter,advanced};
for(int k = 0; k < 3; ++k){
for(int i = 0; i < 3; ++i){
jrbmi = new JRadioButtonMenuItem(minutes[i]);
bg.add(jrbmi);
jm[k].add(jrbmi);
}
}
Last edited by Dilenger4; Oct 25th, 2001 at 12:14 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|