• 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

Container with only one component

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

I am trying to ensure that a JPanel has exactly one component. Do I have to override all of the "add" methods and
set them to blank methods or is there a better more elegant way.

Thanks in advance
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
addImpl is enough; it's called from all other add methods. But what do you wish to accomplish? A container that can store 0 or 1 component, or exactly 1? If it is the latter, then why use the JPanel and not use the child directly?
 
Glenn Lasseigne
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First. Just to clarify your answer. I am assuming that you meant that I just had to override the single method addImpl. Is that correct?
This is good info for the future. Thanks.

Second. I am an "advanced beginner," and I was playing around with trying to fade an opaque JLabel that resided in an upper layer of a JLayeredPane. When alpha compositing, "garbage" showed through since the background was not cleared before the JLabel was repainted with a different alpha value. My solution was to wrap the JLabel with a transparent JPanel. I made sure that setOpaque(false) was called for both the JLabel and the wrapping JPanel. I then painted the background of the JLabel as a filled rectangle in the foreground of the JPanel, and then painted the foreground of the JLabel over that. It worked!!

Third. Although I am the only who is using this, I wanted to practice defensive programming. The wrapper should only have a single component, and I wanted to make sure that this was true.

Fourth. I am sure that there are more elegant solutions to do what I did. I will look into JXlayer. By the way, many examples (found on the internet) of how to achieve fading by use of alpha compositing do not work as intended! It appears to be much more complex than any simple example can demonstrate.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glenn Lasseigne wrote:
First. Just to clarify your answer. I am assuming that you meant that I just had to override the single method addImpl. Is that correct?
This is good info for the future. Thanks.


Correct. JPanel inherits its add methods from java.awt.Container which clearly says, for all of its add methods,

This is a convenience method for addImpl(java.awt.Component, java.lang.Object, int).


So override that and it will be called by all add methods.

For the remove methods this is only implied by the "See" clauses, which include remove(int). However, although in Java 6 remove(Component) indeed calls remove(int), removeAll() does not.
 
Onion rings are vegetable donuts. Taste this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic