Is there a way for me to manipulate the cells of a grid layout? For example lets say i have a grid that has 5 rows and 5 columns. Is it possible for me to manipulate the cell at row 1 column 2 using the layout, sort of like the way you would manipulate the cells of an array? Or is this not possible? Hope someone can help. Thanks.

Recommended Answers

All 5 Replies

Are you talking about the display inside the grid? If so, you simply display a panel inside that row. Then manipulate (repaint or swap out) the panel for different content display.

A grid layout is just an algorithm for placing GUI objects in a container. The "cells" are just an abstract geometrical concept, so there's no meaningful way to "mainipulate" them.
What you can do is create a 5x5 array of some real objects, eg JButton[5][5] or JLabel[5][5], then use the grid layout to place those objects in the container in the right positions. Then you can manipulate the objects however you wish.

Depending on your actual requirements you may be better with a JTable rather then a grid of separate objects.

Are you talking about the display inside the grid? If so, you simply display a panel inside that row. Then manipulate (repaint or swap out) the panel for different content display.

Sorry for the late reply, also it seems i haven't explained my problem fully.

In my program i painted some squares. Each sqaure is in it's own JPanel and i've used the Grid Layout to place the JPanels in a JFrame. My program changes the color of a square when it is clicked but it's also supposed to change the colour of the squares that are directly touching the one that was clicked. But i don't know how to do this. I was trying to manipulate the colour of the squares next to the one that was clicked through the gridlayout but i'm not sure if this could be done.

If you know of another way to do this please let me know.

Thanks

OK, that's the kind of thing I was referring to. Although htis may seems a bit complicated to start with, in the end it will be easier to use a proper model/view structure, a simple form of MVC, in which the logic is implemented in a non-GUI model, and a separate GUI just displays whatever the current state of the model may be. This breaks one big problem into two smaller ones.
Create a 2D array of Square objects. Each Square will have a color attribute (and other attributes depending on your application). Perform all your updating on that array (eg "directly touching" == array index differs by 1).
Create the GUI with each of your JPanels linked to one element of the array of Squares (you can use the JPanels' putClientProperty method to store a direct reference to its corresponding Square in the JPanel). Then updating the GUI is simply a process of looping thru all the JPanels querying their associated Squares to get the current color.

ohhh I get it now! Thank you so much for breaking that down for me. I've been stuck on this for a almost a week now and it's been getting pretty furstrating. Thank you!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.