user1642463
user1642463

Reputation:

JLabel not showing

I'm in the process of finishing up a game and I've reached a point that has made me extremely frustrated. It's probably a simple fix and something I'm overlooking, so maybe you guys can help me out. I'm trying to get a series of 5 JLabels to show up on screen with the 5 high scores from the game. However, it keeps showing up with the background and no labels. The following is my code:

import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class EndPanel extends JPanel{

   GameGUI gui;
   JLabel[] array;

   public EndPanel(GameGUI gui) {
       super();
       this.gui = gui;
       array = new JLabel[5];
       for(JLabel i : array) {
          i = new JLabel("");
          i.setVisible(true);
          this.add(i);
       }
       int i = 0;
       for(HighScore h: gui.getGameDriver().getHighScores()) {
          System.out.println(gui.getGameDriver().getHighScores());
          array[i].setText(h.toString());
          i++;
       }

    }

    public void paint(Graphics g) {
       super.paint(g);
       g.drawImage(gui.images.get(32), 0, 0, this);
       g.drawImage(gui.images.get(31), 112, 100, this);
    }
}

Upvotes: 0

Views: 1257

Answers (4)

PhiLho
PhiLho

Reputation: 41132

The initialization of the array is strongly suspicious. Instead of:

   for(JLabel i : array) {
      i = new JLabel("");
      i.setVisible(true);
      this.add(i);
   }

try:

   for (int i = 0; i < array.length; i++) {
      array[i] = new JLabel("");
      array[i].setVisible(true);
      add(array[i]);
   }

Untested as your code cannot be compiled.

[EDIT] Actually, you don't need to create an array of JLabel since you throw them out (array goes out of scope at the end of the method). You can create the labels on the fly:

   for (HighScore h : gui.getGameDriver().getHighScores()) {
      System.out.println(gui.getGameDriver().getHighScores());
      JLabel label = new JLabel(h.toString());
      add(label);
   }

You still need to set the position of these labels, with a layout manager or by absolute position.

Upvotes: 4

Branislav Lazic
Branislav Lazic

Reputation: 14806

Instead of using for each loop try using regular for loop, like this:

JLabel [] label = new JLabel[5];
    for(int i = 0; i<5;i++){
        label[i] = new JLabel("Label number: " +i);
        panel.add(label[i]);
    }

This should work.

Upvotes: 3

AlexR
AlexR

Reputation: 115328

Since you do not use layout manager you have set the label position and size yourself. Otherwise I guess all your labels have size=0 and are positioned at point 0,0.

BTW, do you have a special reason not to use layout manager or you just not aware on it?

Upvotes: 2

Abubakkar
Abubakkar

Reputation: 15644

try moving the Jlabel initialization code inside the loop of high score :

   int i = 0;
   for(HighScore h: gui.getGameDriver().getHighScores()) {
      System.out.println(gui.getGameDriver().getHighScores());
      array[i] = new JLabel(h.toString());
      this.add(array[i]);
      i++;
   }

Upvotes: 0

Related Questions