Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

JMenu Construction problem

 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,

I have a problem with JMenu construction.

Have a look at this code


If i uncomment popMenu.add(menuItem) line then only i am able to view "New" menuitem under the "File" menu.

Why is this behavior?
Is there a restriction that we can add component only once. Can anyone explain me this behaviour

I just tried one more implementation in which i tried to add same menuitem on different menu but in that one also i am able to view menuitem on the second menu only

Code which i tried for that implementation is


Waiting for your reply......
 
Author
Posts: 979
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Balasubramanian Chandrasekaran:

Is there a restriction that we can add component only once. Can anyone explain me this behaviour



Yes, there is exactly this restriction. If you try to add a Component
to a second Container, it will automatically be removed from the first
Container first. There are good reasons for this, but they are easiest
to understand if you think of text fields and buttons instead of menu
items. Menu items do behave the same as other Components, though.

One thing you can try is instead of attempting to add a JMenuItem multiple
places, instead add an Action to them. An Action isn't a Component, but
can be added directly to a JMenu (in which case a JMenuItem is created
for you
behind the scenes).

I just tried one more implementation in which i tried to add same menuitem on different menu but in that one also i am able to view menuitem on the second menu only

Code which i tried for that implementation is



But you're still adding menuItem to two different Containers. It should
appear correctly if you just do:


If you want to take my Action suggestion it would look something like:

[ April 17, 2008: Message edited by: Brian Cole ]
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Brian,

I just thought of reusing the same object.Can you explain me why that restriction is there (in detail).
In the mean while i will try to implement your Action on my code.
[ April 17, 2008: Message edited by: Balasubramanian Chandrasekaran ]
 
Brian Cole
Author
Posts: 979
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Balasubramanian Chandrasekaran:
I just thought of reusing the same object.Can you explain me why that restriction is there (in detail).



We discussed this a few months ago in another thread.

If you just want to be convinced that it is true, take a look at the
source for the Container.add() methods. They eventually delegate
to Container.addImpl(), which does this:

If you want to understand the why of this, consider that an instance of
Component has a number of properties that can be accessed with methods
such as getBounds(), getLocation(), getX(), getY(), isFocusOwner(), and
getParent(). How would you suggest these methods be implemented for
Components that have somehow been added to more than one Container?
[ April 17, 2008: Message edited by: Brian Cole ]
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again,
Now i got the point.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic