2

I'm creating a Java swing GUI and I have formatted a JPanel to use a GridLayout. I need to access a specific "box" (i.e. specific coordinate) of the grid, but I cannot see a way to do so.

How can I do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
kubasub
  • 455
  • 1
  • 5
  • 12
  • Any chance you're building a view for the [Towers of Hanoi](http://stackoverflow.com/questions/7747691/solving-recursive-towers-of-hanoi-in-lisp)? – trashgod Dec 06 '11 at 02:17
  • *"need to access a specific "box" (i.e. specific coordinate) of the grid"* 1) What program feature does that provide to the end user? What is in the cells of the grid? – Andrew Thompson Dec 06 '11 at 02:22
  • @trashgod no i'm not working with Towers of Hanoi -- although I am making a GUI for a game. – kubasub Dec 06 '11 at 02:43
  • @Andrew Thompson It allows the players of the game I am making to see if they "hit" or "missed" in the cell that they guessed. This would be marked a JLabel "O" – kubasub Dec 06 '11 at 02:44

2 Answers2

3

You shouldn't depend on GUI code (the View) to give you information about program data (the model). The best solution would be to "know" which component is where from the start--maybe you should have a data structure (2D array?) that holds the components and is updated whenever something's added to the grid.

If you want a quick and very-dirty fix, though, you could start playing games with JPanel.getComponentAt(). This requires pixel coordinates, though, so you'd need to do some reverse-engineering to figure out how much space a given grid square takes up. The space between grid squares is given by your GridLayout object. This is not recommended whatsoever though. I'm just including it in the interest of completeness (and since it's a more literal response to your question).

tsm
  • 3,598
  • 2
  • 21
  • 35
  • Okay, I think I know what I was doing wrong. I was just adding a bunch of JLabels to fill the grid with X's and then as players guess certain coordinates the X's turn into numbers. I was not storing the JLabels in any data structure though. I'll try that now, thank-you! – kubasub Dec 06 '11 at 02:44
  • 3
    @KubaSub, don't store the JLabels...you'd be mixing the model and the view again. Make the game playable without a GUI, then add the GUI to the game...that way you won't be tempted to use UI components as data or data stores. – Paul Dec 06 '11 at 05:57
  • 1
    @Paul raises a good point; see also this [MCV example](http://stackoverflow.com/a/3072979/230513). – trashgod Dec 06 '11 at 13:38
  • Thanks for your help, unfortunately I already completed my MCV assignment before reading not to store the labels. Just so I understand though, what exactly did you mean by having a 2D array which holds my components? I must have misunderstood. – kubasub Dec 10 '11 at 01:06
  • 1
    Let's say that you have the `Mark` class, which stores an `X` or an `O`. To represent a given game state, you could then have a 2D array like this: `{{new Mark("O"), new Mark("X"), new Mark()}, {new Mark("X"), ...} ...}` To display it, you would iterate through the array and do something like `label.setText(marks[r][c].toString())`, where `r` and `c` are indices into the above array, and the `toString()` method returns the appropriate symbol. So you have the model--the collection of `Mark`s--and the view--the `JLabel`s. – tsm Dec 10 '11 at 18:50
1

In GridLayout, "The container is divided into equal-sized rectangles." You can add an empty, transparent component in places you want to appear empty, e.g. new JLabel(""). See also GridBagLayout and Using Layout Managers.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • You might get some ideas from this [game](http://sites.google.com/site/drjohnbmatthews/buttons) that uses a grid of toggle buttons. – trashgod Dec 06 '11 at 02:47