1

I'm having trouble getting my JLabels in a 2D array to update during runtime.

The program I'm working on is a variant of Connect Four. I create a 2D array of JLabels, which all default to an ImageIcon containing an image of a blank slot. Players 1 and 2 choose their colors, and on a player's turn, he can click to drop a piece into a column (gravity causes the piece to fall to the bottom or until it lands atop another piece).

I'm pretty positive that my addToColumn method is working fine. My only problem is that I can't seem to get any of the JLabels to update. Here's the method I'm working on:

p1, p2, and current are Player objects. grid[][] is a 2D array of integers set to 0, 1, or 2 to more easily track who owns which tiles. tiles[][] is my 2D array of JLabels.

public void addToColumn(int column) { // drop a tile in the specified column
int i = 0;
while (grid[column][5-i] != 0) i++; // move upward through the 6 rows of tiles
                                    // until we find an empty one
if (current == p1) grid[column][5-i] = 1; // update to the current player's value
else grid[column][5-i] = 2;

tiles[column][5-i] = new JLabel(findColorIcon(current.getColor()));

tiles[column][5-i].setIcon(findColorIcon(current.getColor()));

repaint();

now with those last two lines changing the JLabel in tiles[][], obviously I don't need both, not sure which way is better... that's just some of what I've tried, to no avail. (my getColor() method returns a Color, and findColorIcon(Color c) returns the corresponding JLabel with that color of tile).

and yes, I have added in my paintComponent method too:

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}

I've been stuck on this for a while now, and I feel like I'm missing something obvious. any suggestions?

thomas
  • 61
  • 1
  • 1
  • 9
  • For better guidance about your particular problem, update your question with an [sscce](http://sscce.org/). – trashgod Apr 02 '11 at 09:49

1 Answers1

3

I don't see that your paintComponent() method does anything. In particular, replacing a JLabel requires that you validate() the container. As an alternative, you might like to see how this simple game uses the Model–View–Controller pattern and draws colored icons.

Addendum: This related example describes how to replace just the Icon, rather than the entire JLabel. In contrast, this example shows how to validate() a container after replacing components.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Sorry, but I'm not seeing the similarity between that program and mine. Maybe I was unclear; I can draw my initial icons just fine (the grid of empty tiles), but when I try to change them during runtime (e.g. Player 1 is red, Player 1 adds to column 3, so the bottom tile in column 3 is now red). Is this just not something I can do with JLabels? I thought I just needed paintComponent() in there so that I could call repaint() after changing the contents of my JLabel at tiles[column][5-i]. (i.e. what both of my last two lines are attempting to do). – thomas Apr 02 '11 at 09:40
  • Instead of _replacing_ a label, the example shows how to _update_ an existing label, also discussed [here](http://stackoverflow.com/questions/5494766). The pattern may help your design. – trashgod Apr 02 '11 at 09:48
  • Could you also explain more what you mean by validating the container, and where in my program I would do this? – thomas Apr 02 '11 at 09:48
  • I wouldn't use that approach; I'd change or replace the icon, as shown in the examples cited. Without your complete example, it's impossible to say where to modify your code. – trashgod Apr 02 '11 at 09:54
  • Got it (and feeling pretty dumb right now!) Nothing clicked for me when I first read through those examples, except I noticed one of them had no calls to repaint(), and neither of them overrode paintComponent(). I actually just REMOVED both of those things from my code; I decided the tiles[i][j] = new JLabel(...) line didn't look right either, and could have been causing problems, so I got rid of that too. Now it's working perfectly, repainting labels with no explicit calls from me. Thanks for the help trashgod! – thomas Apr 02 '11 at 10:10
  • Excellent; I've added more links above. Please consider accepting this answer by clicking the gray check box on the left. – trashgod Apr 02 '11 at 10:14