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.
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 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.
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.
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)
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.