• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Custom JComponent Problem

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it working. At last! I can sleep now!

Thank you very much.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Miles wrote:Got it working. At last! I can sleep now!

Thank you very much.



You're welcome and congrats!!
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic