• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing Menu item with no children?

 
Karl Krasnowsky
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Hopefully a basic question.
I have a form with a Menu bar. What I want is one menu item added that has no children... so when the menu is clicked it simply performs an action (in this case fires up a dialog). So the menu text itself is like this:



to indicated that selection will do something immediately.

It would seem that the standard top menu item is expecting children to be processed since after I perform the action the originally selected menu remains selected (I'm guessing with an empty drop down) and requires two more clicks to get it back to ready state (again, assuming the first click is expecting the selection of a child menu, which doesn't exists and the other? not sure, but something in the processing is needing this).
Really what I want to do is have this top menu item act as a link... and after selected simply return to normal ready state. I do want it to act an look like a menu item on the menu bar however.

Must be a way to do this?

thanks!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> What I want is one menu item added that has no children...

try adding to the menuBar a JMenuItem instead of a JMenu
 
Karl Krasnowsky
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:> What I want is one menu item added that has no children...

try adding to the menuBar a JMenuItem instead of a JMenu


It would seem that it's not possible to put a JMenuItem directly on a menubar (using Netbeans Swing GUI builder) and requires to be in the body of a JMenu object. If there's a process for doing this I'm all ears.
 
Karl Krasnowsky
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, since I've identified other menu items that could be added to a top level JMenu this is sort of a moot point, but it would still be nice to know how to implement this. Is there some process that I can simulate a couple of mouse clicks to pass through in order to reset the state of the menu if I wanted to do this?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl Krasnowsky wrote:It would seem that it's not possible to put a JMenuItem directly on a menubar (using Netbeans Swing GUI builder) and requires to be in the body of a JMenu object. If there's a process for doing this I'm all ears.

Yup, there's a process. It's called "writing code"
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> If there's a process for doing this I'm all ears.

better off asking in a Netbeans/IDE forum.

> Is there some process that I can simulate a couple of mouse clicks to pass through in order to reset the state of the menu if I wanted to do this?

assuming you've added a mouseListener to the 'menu', at the end of the mousePressed/Released/Clicked code put
[menu].setSelected(false);

another option is to use a toolbar instead of menuBar
 
Karl Krasnowsky
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
Karl Krasnowsky wrote:It would seem that it's not possible to put a JMenuItem directly on a menubar (using Netbeans Swing GUI builder) and requires to be in the body of a JMenu object. If there's a process for doing this I'm all ears.

Yup, there's a process. It's called "writing code"


At least one in every bunch, eh? So what in the hell did I do to get that sort of attitude? It was an honest question. If you don't have an answer or anything constructive to provide shut up and go beat your dog.
 
Karl Krasnowsky
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:

assuming you've added a mouseListener to the 'menu', at the end of the mousePressed/Released/Clicked code put
[menu].setSelected(false);

another option is to use a toolbar instead of menuBar


Thanks Michael, I'll check that out.

Darryl, take some direction here, this is how forums work.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl Krasnowsky wrote:
Darryl Burke wrote:Yup, there's a process. It's called "writing code"


At least one in every bunch, eh? So what in the hell did I do to get that sort of attitude? It was an honest question. If you don't have an answer or anything constructive to provide shut up and go beat your dog.

Please BeNice. Darryl only suggested that once in a while you should bypass the GUI builder and write a bit of GUI code manually. And being another "write GUI code manually" advocate, I fully agree.
 
Karl Krasnowsky
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Karl Krasnowsky wrote:
Darryl Burke wrote:Yup, there's a process. It's called "writing code"


At least one in every bunch, eh? So what in the hell did I do to get that sort of attitude? It was an honest question. If you don't have an answer or anything constructive to provide shut up and go beat your dog.

Please BeNice. Darryl only suggested that once in a while you should bypass the GUI builder and write a bit of GUI code manually. And being another "write GUI code manually" advocate, I fully agree.


Me be nice? Geesh, isn't that wild? How do any of you know what I use to code with? Yes, for this particular example I used the builder out of Netbeans, doesn't mean that's what I always do. The code that the framework creates still requires coding knowledge (and I'm happy for that!) in order to properly extend it as it only provides the boiler plate stuff which I'm very happy to not have to do again and again. If this is what you find interesting, well, here's where I'll be "nice". The reality is, using the gui kit requires a bit more knowledge as you need to reverse engineer the darn thing to figure out where and what it generated.

Tell you guys what, using VI or whatever real men do, I challenge you to come up with a solution for my question. The GUI provides the same level of support, i.e., here's JMenu foobar... now write the handlers to something interesting with it.

Hope you all are having a grand weekend!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Tell you guys what, using VI or whatever real men do, I challenge you to come up with a solution for my question.

Honestly, without relying on IDE-generated code, my original suggestion is as simple to do as adding milk to cereal.

You've chosen the 'IDE-generated code' route, no problems there, plenty do it, so you really should now be putting
your question to a Netbeans forum where the people who use Netbeans can show you how to do it.

I'd love to help you, but I can't because I don't use Netbeans (or Eclipse/JBuilder etc).
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick and dirty little program to show how easy it is to add a JMenuItem to a JMenuBar. I've written it in less than 5 minutes, without any GUI builder.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I've written it in less than 5 minutes, without any GUI builder.

what took you so long?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firing up the text editor and command window
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic