Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Hide or remove a JLabel

Tags:

java

swing

jlabel

I have declared a JLable as follows;

l = new JLabel("Hello");
l.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(l);

Now, i want to hide or remove it. What should be the method i should call ?

i tried l.removeAll(); <--- nothing hapend.

there's another one calle remove(int) which takes an int. But i am not sure what to pass as the parameter.

There's also something called hide(). but it's deprecated.

like image 325
Sharon Watinsan Avatar asked Dec 04 '22 00:12

Sharon Watinsan


1 Answers

i tried l.removeAll(); <--- nothing hapend.

you need to call to remove on JPanel which the JLabel was added to :

panel.remove(l);

//after that you need to call this to revalidate and repaint the panel
panel.revalidate(); 
panel.repaint();

just to hide and not to remove call

l.setVisible(false);
like image 102
Nikolay Kuznetsov Avatar answered Dec 05 '22 13:12

Nikolay Kuznetsov