hi i have a label that i have set a icon for it, i want to remove this icon after clicking on a button, what is the method for it?
Asked
Active
Viewed 3.9k times
7
-
1Post your existing code, so we can see what you are talking about. – Oded Feb 10 '10 at 09:12
-
1You need to give us more details if you need help... – LiraNuna Feb 10 '10 at 09:12
2 Answers
15
// Create icon
Icon icon = new ImageIcon(getClass().getResource("/foo/bar/baz.png"));
// Create label
final JLabel lbl = new JLabel("Hello, World", icon, JLabel.LEFT_ALIGNMENT);
// Create button
JButton btn = new JButton("Click Me");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// Remove icon when button is clicked.
lbl.setIcon(null);
// **IMPORTANT** to call revalidate() to cause JLabel to resize and be repainted.
lbl.revalidate();
}
});

Tomáš Zato
- 50,171
- 52
- 268
- 778

Adamski
- 54,009
- 15
- 113
- 152
9
label.setIcon(null)
in the event handler that handles the button click, if you're using Swing.

ndeuma
- 729
- 7
- 12
-
1+1 for actually posting what comes to mind when reading the question. – Frederik Wordenskjold Feb 10 '10 at 09:26
-