File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Container with only one component Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Container with only one component" Watch "Container with only one component" New topic
Author

Container with only one component

Glenn Lasseigne
Greenhorn

Joined: Jun 24, 2010
Posts: 6
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


D. Glenn Lasseigne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Glenn Lasseigne
Greenhorn

Joined: Jun 24, 2010
Posts: 6

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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.
 
Consider Paul's rocket mass heater.
 
subject: Container with only one component