I'm using the same file menu with the same menu items in multiple places. As such, they should have the same actionListeners etc since the same windows should open when they are clicked. How do I get netbeans to automatically do this (make them all use the same events and such)? Note: In some cases, I made the menu a template and re-used it, in other places, I just added a new menu and added the menu items again.

thanks

Design specific class to hold only menu items and all it needs (action listeners) and just add it to frames as you need it. If you design it properly you will also able to add some menu items to specific menu sections on the go as you need or do you need code example?

commented: Good suggestion :) +3

I understand the logic of what you are saying. But I'm not sure how this can be accomplished in netbeans. . through drag and drop, and their 'auto' type features. I could edit the code myself but it might cause layout problems since I'm using GroupLayout. I created the menu bar and all of the menu items just by dragging and dropping.

I guess somebody else will need to fill in this gap. I'm able to design my own menu bar with menus and menu items. However so far I did not figure out how to attach this custom menu bar to frame. There must by a way I'm just looking in wrong place...

You're using netbeans or no? Either way, JFrame has a method called setJMenuBar, and you pass the JMenuBar you created as the argument.

Still no success in adding the menu bar. Have look at attachment see if you can work it out.

Basically to create custom menu bar I did as follows:

  • Create new Class as right click on directory, select New, select JPanel Form (this will open in designer view)
  • Select Source and change extends javax.swing.JPanel to extends javax.swing.JMenuBar
  • back to Design
  • In Inspector tab (if you have default IDE layout in left side down, bellow Projects, Files and Services) or Windows, Navigating, Inspector to open this tab. Here right click on Form Test and do Reload Form
  • After this you can see that JPanel changed to JMenuBar and by right clicking on it you can add only Menu

From this point onwards you are on your own or maybe somebody can help you add this JMenuBar to frame. I tried, but as you can see fro code no luck

I'm really not sure what you are asking. To attach a JMenuBar to a JFrame, you just make a new JFrame form then drag a menu bar onto it. Then you can highlight the menu bar and add things to it with the palette.

You asked how to do menu that you can reuse in first place. I showed you how it can be done, my only issue with it was I couldn't attach it to frame.
Now it is up to you if you will do anything with it or wait for some other answers.

Oh, I see what you are saying now. I think that will work. . I'm going to just try adding "new JMenuBarClassName". . or adding the JPanel class to the panel as a template. . one of those things might work. I'll let you know.

You can create that menu bar as a reusable bean form by selecting the following: New > Swing GUI Forms > Bean Form and specifying javax.swing.JMenuBar as the super class in the final step of the wizard.

After you have it built, you can add it to a JFrame by right-clicking it and selecting Add From Palette > Beans > Choose Bean and then typing in the full class name of the bean you created.

Hope that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.