What is to go in the rectangles? You can place JLabels in a GridLayout and give them a line border if you want a grid of actual components.

Or you can simply override paintComponent() and draw lines on your JPanel.

The Oracle tutorials offer more information about both of those possibilities.

Edited
6 Years Ago
by Ezzaral: n/a

begueradj

6 Years Ago

Hello people

I have a JFrame which container is a JPanel: can you give me a piece of code that shows how to create a grid of rectangles ?

I need this information for my poject
Thank you

Thank you,

In fact, I want to draw that grid according to matrix data: I succeeded to do that, but I want to draw a rectangle in black if my matrix element is 1, and in white if my matrix element is 0. I did as follows but it draws all the small rectangles with the the color that corresponds to the last element of my matrix. Can you tell me how I can modify my following code to do that , please ?