• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why did not JMenuBar change?

 
Ranch Hand
Posts: 56
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to change JMenuBar's looking by using below class :



However, it does not work as I expected. Same code worked like a charm for JMenuItem(i.e. just change the name of the class and its super class names). What am I doing wrong?
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that addNotify() actually gets called?
 
Hope Spartan
Ranch Hand
Posts: 56
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh my god! You are completely right ! I checked that whether it is called or not and it is not! I moved the code inside addNotify() to another method and it works! But why ? Why the same code works, i.e. addNofity is called for JMenuItem but not for JMenuBar ?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Hope, welcome to the Ranch!

In addition to Carey's comment (which you didn't answer), I'm wondering why you chose the addNotify() method to set up the paint characteristics for the component. The documentation for that method just says it registers the component with the keyboard manager, which to me doesn't seem like it has anything to do with painting the component. Was there a reason that the initialization code didn't get put into the constructor?
 
Hope Spartan
Ranch Hand
Posts: 56
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Hello Hope, welcome to the Ranch!

In addition to Carey's comment (which you didn't answer), I'm wondering why you chose the addNotify() method to set up the paint characteristics for the component. The documentation for that method just says it registers the component with the keyboard manager, which to me doesn't seem like it has anything to do with painting the component. Was there a reason that the initialization code didn't get put into the constructor?



Thank you very much for nice welcome.

I am using addNotify() method to be sure that, when component is ready and placed and showed inside its parent container (or at least what I understood that from the java documents and java books). I replied Carey's comment, and it is completely solved my problem.

Also I want to send below code parts to show addNotify() works as I expected in JMenuItem and JMenu. However only for JMenuBar it did not worked ! Why ?



Thanks for all.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know why addNotify() is called for a JMenuItem but not for a JMenuBar, although since addNotify deals with keyboard management perhaps it isn't called for JMenuBar because JMenuBar and keyboards have nothing to do with each other.

So it looks to me like your "addNotify" code is meant to initialize the component to be painted with a particular gradient. Usually initialization code like yours can go in the constructor, so again, why didn't you do that instead of choosing an apparently random method to run it?
 
Hope Spartan
Ranch Hand
Posts: 56
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I don't know why addNotify() is called for a JMenuItem but not for a JMenuBar, although since addNotify deals with keyboard management perhaps it isn't called for JMenuBar because JMenuBar and keyboards have nothing to do with each other.

So it looks to me like your "addNotify" code is meant to initialize the component to be painted with a particular gradient. Usually initialization code like yours can go in the constructor, so again, why didn't you do that instead of choosing an apparently random method to run it?



Yes, you are completely right, my code aims to apply a gradient to a JMenuBar and all of its items. Actually I got used to use it like "component is ready" method. In my code I used getParent() method for JMenuBar and out of addNotify() methods it gives NullPointerException since in the constructor its parent is not ready yet and component is not placed inside its parent so that I could not get the dynamic(i.e. means that I don't like use magic numbers) height value of the component there.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this?
 
Hope Spartan
Ranch Hand
Posts: 56
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just tired your code. Although it did not give any error "gradient painting" didn't work. Actually, when I inspected deeper, I got that in setPaint() method h gives 1.0 which is not correct. It should give the actual height of the component to be correctly paint gradient.
This was the reason I have just used addNotify() method : to get the updated and  component's (i.e. JMenuBar's) valid height.
However, when I passed the height as an parameter to constuctor of MyJMenuBar as



and change the constructor of MyJMenuBar as :


then I can get the height.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or...
This should also  cope with resizing.
 
Hope Spartan
Ranch Hand
Posts: 56
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey, your last code works !

Thanks.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, a better approach than using a random method to attach the desired code. Just one comment... the setPaint() method should probably be declared private, since there doesn't seem to be any reason to call it from outside your menu bar class.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.
 
Hope Spartan
Ranch Hand
Posts: 56
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK ! I found the reason why my code did not work! I was wrong before ! Carey asked me whether addNotify() method works or not, and when I get all code out of it and put into the constructor it worked, and I said that "you are right!". Actually I found that I am wrong !  In reality, addNotify() method was working as I expected, my mistake is trying to assign height value using getParent() method. Although JMenu and JMenuItem getParent() method works, in JMenuBar it does not ! So I fixed my own code and working code  is that :



Thanks for Paul and Carey to guide me to find the root cause of my mistake.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying that the code in my post (just before your last one) doesn't work?

Two things I find potentially unreliable with your code is the use of addNotify() (I don't know how you'd guarantee that it gets called), and getting the height from prefferedSize (the actual size may vary depending on the calculations in a layout manager).

A third thing is you are not addressing potential resize events.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey is right, you will have a resizing problem.

But you can use this.getWidth() and this.getHeight() in your menubar-class For instance:

and to get this working from the start, use for instance:
 
Hope Spartan
Ranch Hand
Posts: 56
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Are you saying that the code in my post (just before your last one) doesn't work?

Two things I find potentially unreliable with your code is the use of addNotify() (I don't know how you'd guarantee that it gets called), and getting the height from prefferedSize (the actual size may vary depending on the calculations in a layout manager).

A third thing is you are not addressing potential resize events.



I said before tthat your code is working Carey. But I just said that addNotify() behaves as expected, which means the last code I posted works very well without any problems including resizing. Dear Piet, your code also works.

Dear Carey, I can guarantee that addNotify() works everytime, since it works for all JComponents automatically when they have been just assigned to a valid parent. Anyone can check details about addNotify() from java docs : addNotify(). Therefore it is like a standard code working background.

Also when I checked resizing issue, I did not have any problem since layout manager of JFrame(i.e. parent of JMenuBar) did everthing automaticall. Until now, I used JMenuBar  without seeing any case where JMenuBar's widht changes since it is bound to JFrame. As far as I know, one can only change its height for better look etc.. So, I think using JMenuBar's setPreferredSize(...) method is enough for handling all resizing issues. However, if you have any example in which resizing causes problem with "my code" I am open to change it, why not?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic