Display problems with custom JComponent

I am having issues displaying two custom JComponents at the same time.

I have created a class which extends JWindow, and runs the following

code:

getContentPane().add(new CustomComponent(20,20));

getContentPane().add(new CustomComponent(120,20));

setSize(640,480);

setVisible(true);

The class CustomComponent extends JComponent and has the following

method:

protectedvoid paintComponent(Graphics g)

{

g.fillOval(x,y,50,50);

}

The problem is that only the second CustomComponent added is

displayed. When the following code is run, there is no problem:

getContentPane().add(new CustomComponent(20,20));

setSize(640,480);

setVisible(true);

getContentPane().add(new CustomComponent(120,20));

setSize(640,480);

setVisible(true);

Note that BOTH setSize and setVisible must be run inbetween each

CustomComponent addition for them all to be displayed.

Can anybody shed any light on what's happening. I don't get this

problem when adding JLabel, so I guess it must be a problem with my

CustomComponent.

Thanks for your help.

[1530 byte] By [CBaumea] at [2007-11-27 4:33:08]
# 1
What LayoutManager do you use?
quittea at 2007-7-12 9:42:59 > top of Java-index,Java Essentials,Java Programming...
# 2
Am not using a layout manager as I need absolute control over x and y positions.Is one required?
CBaumea at 2007-7-12 9:42:59 > top of Java-index,Java Essentials,Java Programming...
# 3
Uhm, no. Does this mean that you explicitly set the LayoutManager to null, or do leave the Container as is?
quittea at 2007-7-12 9:42:59 > top of Java-index,Java Essentials,Java Programming...
# 4
Leaving it. The code you see above is everything related to Swing and displaying the CustomComponent class.
CBaumea at 2007-7-12 9:42:59 > top of Java-index,Java Essentials,Java Programming...
# 5

> Leaving it.

Ok. This way you get a BorderLayout as default, which means that by adding both components without a position constraint, both are placed in the center position, probably overlaying each other.

Try setting the LayoutManager to null, and set both location and size on your custom components manually.

quittea at 2007-7-12 9:42:59 > top of Java-index,Java Essentials,Java Programming...
# 6
Good suggestion.I tried the codegetContentPane().setLayout(null); and now nothing appears instead of the last CustomComponent added! Oh dear. I tried using setBound(x,y,w,h) on each component, but still nothing.?
CBaumea at 2007-7-12 9:42:59 > top of Java-index,Java Essentials,Java Programming...
# 7
Thisg.fillOval(x,y,50,50);should be changed tog.fillOval(0,0,50,50);
quittea at 2007-7-12 9:42:59 > top of Java-index,Java Essentials,Java Programming...
# 8
Lovely! That works.I'm having trouble getting it to work with g.drawString though. Only a few pixels of the string are displayed.Am I right in saying setBounds() determines where the JComponent goes and the size of the 'box' which is rendered?
CBaumea at 2007-7-12 9:42:59 > top of Java-index,Java Essentials,Java Programming...
# 9
> Am I right in saying setBounds() determines where the> JComponent goes and the size of the 'box' which is rendered?Yep. Coordinates for drawing are relative to the upper left corner of this box.
quittea at 2007-7-12 9:42:59 > top of Java-index,Java Essentials,Java Programming...
# 10
Ace, thanks for your help. I'll see if I can sort out this darn drawString thing now.
CBaumea at 2007-7-12 9:42:59 > top of Java-index,Java Essentials,Java Programming...