2 Replies - 2210 Views - Last Post: 09 December 2012 - 09:16 AM Rate Topic: -----

#1 nvrBungeeJumped  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-December 12

How to remove a JLabel from a specific JPanel

Posted 08 December 2012 - 11:16 PM

So I'm working on a chess game. I have a layeredPane.

I can add pieces to it with
panel = (JPanel)board.getComponent(9);
Piece = new JLabel(new ImageIcon("king.png"));
panel.add(Piece);


I could not figure out how to move one JLabel (Piece) from one panel to another panel. Instead someone gave me the idea of adding a new piece to another tile and removing the piece from the old tile.

I can't figure out how to remove a piece from the panel I want.

panel.remove(Piece);


will remove the piece from the last tile I added to, but I need to be able to select which tile I want to remove from.
panel = (JPanel)board.getComponent(9);

does not work.

Do I need to select the panel, or move the focus to that panel or what?

Is This A Good Question/Topic? 0
  • +

Replies To: How to remove a JLabel from a specific JPanel

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: How to remove a JLabel from a specific JPanel

Posted 09 December 2012 - 01:50 AM

You need to treat the board as an array, then you can access the pieces by index
Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: How to remove a JLabel from a specific JPanel

Posted 09 December 2012 - 09:16 AM

Please don't open duplicate threads. You have one here already.

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1