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

Container with only one component

 
Glenn Lasseigne
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic