Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JMenu Update

 
xavier be
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
xavier be
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually i just call it like
(thanks for your answer)



how do you call it on the event thread
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
( 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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simple demo of prior post

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

 
xavier be
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic