pred
pred

Reputation: 185

Paint JLabel above another JLabel without JLayeredPane

Here's my application structure : one JFrame ---> which contain one Board1 (Board1 extends JPanel) ---> which contains some "Zone" (Zone extends JLabel with FlowLayout manager) ----> which contain some "Personnage" (extends JLabel)

First I create all my JLabels in the JPanel's class:

public class Board1 extends JPanel implements Board {
  private List<Zone> zones = new ArrayList<Zone>();
  private List<Personnage> personnages = new ArrayList<Personnage>();

  public Board1() {
    this.setLayout(null);
    zones.add(new Zone(1, false, true, null, "/zone1D1C.jpg", this));
    zones.add(new Zone(2, false, false, null, "/zone2D1C.jpg", this));
    zones.add(new Zone(3, false, false, null, "/zone3D1C.jpg", this));
    zones.add(new Zone(4, true, false, null, "/zone4D1C.jpg", this));
    zones.add(new Zone(5, false, false, null, "/zone5D1C.jpg", this));
    zones.add(new Zone(6, true, false, null, "/zone6D1C.jpg", this));
    zones.add(new Zone(7, true, false, null, "/zone7D1C.jpg", this));
    zones.add(new Zone(8, false, false, null, "/zone8D1C.jpg", this));
    personnages.add(new Survivant("Phil", zones.get(0), 3, "/phil.jpg"));
    for (Zone zone : zones) {
        this.add(zone);
        for (Personnage personnage : zone.getPersonnages()) {
            zone.add(personnage);
        }
    }
  }

  public void move_personnage(Zone zone) {
    running_personnage.moveZone(zone);
    zone.add(running_personnage);
    this.repaint();
  }

  public void try_add_personnage() {
    Personnage douglas = new Survivant("Douglas", zones.get(3), 3, "/douglas.jpg");
    zones.get(3).add(douglas);
    this.repaint();
  }
}

The constructor is working good, I see my "personnage" above the "zone". move_personnage method is working too !!! It remove automatically the personnage from the previous zone.

The problem is when I'm running try_add_personnage method, there is no JLabel above the zone. However I checked that the method is called etc... Whatever I do after the Board1 constructor, I can't add any kind of new visible Personnage above Zone.

I guess it's with paintComponent method or something like this, but I didn't find the solution. I know that's possible with JLayeredPane but I don't want to use it because I will use a GridBagLayout manager for the board in the future.

I Hope I was understandable

Upvotes: 0

Views: 59

Answers (1)

pred
pred

Reputation: 185

I found the solution. It was my repaint which wasn't enough, I had to make JPanel.revalidate(); and after JPanel.repaint();

Upvotes: 0

Related Questions