Search code examples
javaswingjlabel

Java GUI Clear Panel To Blank Image


Quick question, couldn't find a definitive answer from searching so I thought I'd ask here. In my code I set a JLabel to an image and I would like that image to clear and disappear after I perform a certain action such as clicking a button.

JLabel one = new JLabel(ruby); //ruby is a seperate ImageIcon already defined.

I'm not sure what the command to clear the panel is, an answer would be appreciated! Thanks.


Solution

  • Assuming that the label is already on the screen and you have a reference to the label you should be able to either call remove(label) on the parent container that contains the label or label.setIcon(null) if you want to reuse the label. In either case, you may be required to call revalidate on the parent container