This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JMenu Update Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JMenu Update" Watch "JMenu Update" New topic
Author

JMenu Update

xavier be
Greenhorn

Joined: Jun 15, 2009
Posts: 6
Hi guy's

i have a question about JMenuBar in Java on Os X
i'm programming a little application just for fun

Explaination about the program and my problem :

I have a login part and a menu part
when i'm not logged in the menu is like "File - New - Profile - ?"
this is in my menuBar

but everything in NEW and in PROFILE are disabled ( because you can only use this option when you'r logged)
my code is :



as you can see I disable the items that i don't need before login
but after ... i call this method back and .... in theory this items are ENABLE but ...
it doesn't work

i hope you'll help me

thanks

Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Not sure that this is a Mac issue - probably a general Swing issue....


How are you calling these methods to set up the menu bar? Is it on the event thread? If not, that's why the visual state of the JMenuBar isn't updating correctly.

-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
xavier be
Greenhorn

Joined: Jun 15, 2009
Posts: 6
actually i just call it like
(thanks for your answer)



how do you call it on the event thread
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Hmmmm.... still not sure if it's on the event thread.

To call something on the event thread it either gets called from a listener, or through a Runnable that gets invoked through SwingUtilities.invokeLater()/invokeAndWait() (or uses SwingWorker,etc. which uses invokeLater/invokeAndWait).

Instead of embedding all this code directly in a JMenuBar subclass, it would probably be better to write this code as a listener for whatever component the user uses to login - or at least this code should get called from that listener.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Going to move this to the Swing/AWT forum, as I think it has more to do with this issue rather than being Mac specific.
xavier be
Greenhorn

Joined: Jun 15, 2009
Posts: 6
I tried this but

it's not working also
this is my login listener and it check the login and then
call the MenuGui.setMenuBar();

but... nothing i don't get any updates :s

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if the component (frame) is already visible, any time you add or remove components,
you need to validate/revalidate the container and most times also call repaint.

so your code should look something like this
xavier be
Greenhorn

Joined: Jun 15, 2009
Posts: 6
I tried like you said ... but not working
actually

in my Gui class i have this



as you can see i call my setJMenuBar ...
and my setMenuBar() method return a JMenuBar

so when i call it from my class Login
( from the eventListener )
i don't use the return ... ( i was thinking that revalidate(); and repaint(); will work ) but no ...
and i really don't get it.

when i open my programm i have a menu ( like i said here above ) by calling a method
some parts of this menu are hidden this is my class :





when people wanted to login they enter the password and login and click on validate
my class Login is called and the event is checked by




as we can see i check my loggin here and then ( it comes back above to the event to look to the Method set login )
as we can see above ( in my class ) i checke that i'm logged and ( i have a print in shell that i'm logged ) but
the hidden parts of the menu are appearing

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
looks like the basics of the problem is that menuGui is a JMenuBar ("extends JMenuBar"),
but it is not the menubar added to the frame.

the way you are disabling/enabling the menuItems (by remove/add), to me, is totally wrong,
but if you want it to work that way, the below code works

run the code, check items disabled, click 'Login' recheck items - should now be enabled

xavier be
Greenhorn

Joined: Jun 15, 2009
Posts: 6
( i'll try it in a few moments or maybe tomorow morning depends of my work )

you said that it's totally wrong ... i don't know another type to do it
if you have something better just say it ... i like to learn new ways ;)

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I'm off to golf now so just a quick reply:

all you need is to disable them, then enable them when logged in.
one way might be to put a reference to each menuitem (to be dis/enabled)
in an array, then create a method



call this method after you create the menuBar (with false for state)
call it again after login (with true for state)
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
simple demo of prior post

note there's no need to remove, then add back the menuItems

xavier be
Greenhorn

Joined: Jun 15, 2009
Posts: 6
Very sorry for not answering during all this time ...
and thanks for your post


i saw that you run everything in a thread this actually a nice solution ( and workig solution )
i saw also your array wich is a very good idea i'll try it

 
GeeCON Prague 2014
 
subject: JMenu Update