?
Solved

Paint all the JComponents that are in a JPanel

Posted on 2004-11-04
5
Medium Priority
?
465 Views
Last Modified: 2013-11-23
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.
0
Comment
Question by:sgalzin
  • 3
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 2000 total points
ID: 12494873
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
 
LVL 4

Author Comment

by:sgalzin
ID: 12494941
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
 
LVL 35

Expert Comment

by:TimYates
ID: 12495107
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
 
LVL 4

Author Comment

by:sgalzin
ID: 12495152
Thanks so much,

Stephane.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12495168
No worries :-)

Good luck!

Tim
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question