Paint all the JComponents that are in a JPanel

Hi,

I have two classes : AJPanel (extends JPanel) and AJComponent (extends JComponent). The AJPanel constructor adds 10 AJComponent objects to the panel :

 public AJPanel () {
  this.setLayout ( new java.awt.BorderLayout () );
  for ( int i = 0; i < 10; i ++ ) {
   this.add ( new AJComponent () );
  }      

The problem is these components are not drawn on the screen. So I tried overwriting the AJPanel.paintComponent method like this :

 public void paintComponent ( Graphics g ) {
  super.paintComponent ( g );
  for ( int i = 0; i < this.getComponentCount (); i ++ ) {
   this.getComponent ( i ).setLocation ( new Point ( i*5 + 10, i*5 + 10 ) );
   System.out.println ( "theX : " + this.getComponent ( i ).getX () );
   this.getComponent ( i ).update ( g );
  }
 }

And I added some code for debugging purposes to the AJComponent.paintComponent method :

 public void paintComponent ( Graphics g ) {
  System.out.println ( "myX : " + this.getX () );
// ... the rest of the painting code
 }

However, this is what the output is like :

theX : 10
theX : 15
theX : 20
theX : 25
theX : 30
theX : 35
theX : 40
theX : 45
theX : 50
theX : 55
myX : 55
myX : 55

Whereas I was expecting something like :

theX : 10
myX : 10
theX : 15
myX : 15
theX : 20
myX : 20
theX : 25
myX : 25
...
theX : 55
myX : 55


What is going wrong ? How can I paint all the components of the AJPanel ? Currently, only the last one is painted.

Thanks,

Stephane.
LVL 4
sgalzinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
You are using border layout...

You then add 10 things to it, without constriants...

That's gotta be random at best...

Have you tried;

public AJPanel () {
  this.setLayout ( new java.awt.FlowLayout () );
  for ( int i = 0; i < 10; i ++ ) {
   this.add ( new AJComponent () );
  }    

?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sgalzinAuthor Commented:
wow ! that worked like magic ... Thanks a lot,

Stephane.

PS : if you could give me a word of advice, a link, or any information concerning these layouts I'd be very grateful : this is the second problem I have with layouts and I might be missing some important basic information ...
0
TimYatesCommented:
Unfortunately, there's loads of them... http://www.softbear.com/people/larry/javalm.htm

I always tend to use BorderLayout http://java.sun.com/docs/books/tutorial/uiswing/layout/border.html

The best resource for the standard ones is the sun site:

http://java.sun.com/docs/books/tutorial/uiswing/layout/

But as I said, there are loads of third party ones, and more all the time...

http://www.clientjava.com/blog/2004/11/01.html

for example is a new one that looks quite cool :-)

I'd stick with Grid, Border and Flow till you get used to them though...then branch out :-)

Tim
0
sgalzinAuthor Commented:
Thanks so much,

Stephane.
0
TimYatesCommented:
No worries :-)

Good luck!

Tim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.