• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

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: 22211
117
Eclipse IDE Spring VI Editor 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.
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic