aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Custom JComponent Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Custom JComponent Problem" Watch "Custom JComponent Problem" New topic
Author

Custom JComponent Problem

Chris Miles
Greenhorn

Joined: Aug 02, 2005
Posts: 13
Hi.

I have created a JComponent which I just want to fill with a colour. Very simple.

I have a JFrame with a GridLayout with 1 column and 5 rows.

My JComponent code is very simple, and just calls fillRect.

I add 5 of these components to the JFrame's content panel.



When I run the code the above method is invoked for all 5 components. Debugging shows that X, Width and Height are all the same which is what I expect, and the Y value is changing each time, which again is expected as there is only one column of fixed rows.

When painting is done however, I can only see one rectangle drawn.

Thanks

Chris
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I believe that this line here:

the first two parameters, the x and y are supposed to be relative to this component (the JComponent), not the container (the contentPane). So perhaps you want to use 0 for both x and y here.

e.g., my SSCCE:

Chris Miles
Greenhorn

Joined: Aug 02, 2005
Posts: 13
Pete,

Thanks

I am now using.



Thank you, It now honours the X and Y values as expected, however, it does not seem to honour the widths in the grid layout.

I do



and



I would expect it to add a red rectangle into the first column, and then a blue rectangle into the second column.

However it is just adding 1 red rectangle taking up the full width of the frame, and then a blue underneath taking up the full width of the frame.

Chris
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Chris Miles wrote:Pete,
Thanks

You're welcome.


I am now using.

Thank you, It now honours the X and Y values as expected, however, it does not seem to honour the widths in the grid layout.

It is honoring the width of the jcomponent.



I do

and

I would expect it to add a red rectangle into the first column, and then a blue rectangle into the second column.
However it is just adding 1 red rectangle taking up the full width of the frame, and then a blue underneath taking up the full width of the frame.

Please see the changes to my post above as I created and posted an SSCCE, a small compilable program that demonstrates my solution. You should create one that demonstrates your problem. The link on this is here: SSCCE

edit: By the way, it is adding a rd rectangle in the first column and a blue to the right. Your grid layout is set up to create 10 rows and 2 columns, and the red panel is being placed in the left column, the blue in the right. Methinks you may want to read up a little more on GridLayout.
Chris Miles
Greenhorn

Joined: Aug 02, 2005
Posts: 13
Thanks again.

Maybe your right and it is my GridLayout that is the problem.

Using your example. What if I wanted to add three colours filling one column, and three colours filling the next column?

My code is identical to yours, except I am creating GridManager with 2 columns.

Chris
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Chris Miles wrote:Thanks again.

Maybe your right and it is my GridLayout that is the problem.

Using your example. What if I wanted to add three colours filling one column, and three colours filling the next column?

My code is identical to yours, except I am creating GridManager with 2 columns.

Chris


play with your gridlayout and you'll see how to do it. Also, you may want to add some vertical and horizontal gaps into your grid so you can see that your columns are made up of JComponents. i.e, new GridLayout(10, 2, 5, 5)
Chris Miles
Greenhorn

Joined: Aug 02, 2005
Posts: 13
Got it working. At last! I can sleep now!

Thank you very much.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Chris Miles wrote:Got it working. At last! I can sleep now!

Thank you very much.


You're welcome and congrats!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

I suggest you add "super.paintComponent(g);" as the first line in your paintComponent method. That does a clean up of the previous painting. Although in your case it doesn't matter much (since you're painting over everything anyway), it is a good habit to develop.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
wood burning stoves
 
subject: Custom JComponent Problem