Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fully REMOVE JLabel from JPanel...not setVisible(False)

Tags:

java

jpanel

I have a fairly simple question. I have a JPanel on a JFrame. I have a JLabel on the JPanel. How, I wonder, do i FULLY REMOVE the JLabel from the JPanel during runtime?

ImageIcon image7= new ImageIcon("archmageanim.gif");
JLabel label7 = new JLabel("", image7, JLabel.CENTER);
p.add( label7, "0 , 6" ); //This coordinate has to do with a layout manager I'm using - it 
                          //I'm using - it works fine.

I have looked for this solution...but everyone says "the easiest way" is to set setVisible(false)...but that doesn't truly remove the object -_-. How can I REMOVE it?

like image 421
PinkElephantsOnParade Avatar asked Jul 11 '12 17:07

PinkElephantsOnParade


1 Answers

Can't you just use this to find the parent Container of the JLabel and then use the remove method?

Container parent = label7.getParent();
parent.remove(label7);
parent.validate();
parent.repaint();

That should remove the label altogether and then refresh the parent Container.

like image 108
Bobulous Avatar answered Oct 11 '22 06:10

Bobulous