If I have a JLabel, how do I remove it after 10 seconds? I want to be able to view the JLabel after I've removed it. I figure it might have something to do with javax.swing.Timer
and JLabel.setVisible(false)
.
Asked
Active
Viewed 2,342 times
1

Andrew Thompson
- 168,117
- 40
- 217
- 433

LazySloth13
- 2,369
- 8
- 28
- 37
-
1You figured right. You want to start a timer, and when it's hit ten second, call the method to hide the `JLabel`. it's really that straight forward.. – christopher May 16 '13 at 20:05
-
You can make use of pseudocode give for [Swing timer not stopping](http://stackoverflow.com/questions/14409868/swing-timer-not-stopping/14410163#14410163) – Smit May 16 '13 at 20:08
-
javax.swing.Timer and JLabel.setVisible(false). is right – mKorbel May 16 '13 at 20:09
-
Why bother with `setVisible(false)`? I'd use `setText("")`. Store the string if you want to view it later. – Andrew Thompson May 17 '13 at 03:53
1 Answers
8
Try this:
final JLabel label = new JLabel("myLabel");
int delay = 10000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
label.setVisible(false)
}
};
new javax.swing.Timer(delay, taskPerformer).start();

Community
- 1
- 1

Ricardo Cacheira
- 797
- 4
- 6
-
1Might be worth noting that `label` should be an instance variable, or a `final` variable. – afsantos May 16 '13 at 20:17