I'm using netbeans 7.1.1,
to create a JFrame. I want to automatically dispose the JFrame
5 seconds after calling setVisible()
. How can I do this?
Asked
Active
Viewed 2,937 times
1

Mukul Goel
- 8,387
- 6
- 37
- 77

Jayashri
- 366
- 4
- 13
- 25
-
Thread.sleep(5000); but it will wait for 5 second before show the JFrame. – Jayashri Nov 05 '12 at 11:34
-
i think you are looking for splash screen.. – Unni Kris Nov 05 '12 at 11:36
-
1Or a [dialog](http://stackoverflow.com/a/12451673/230513). – trashgod Nov 05 '12 at 11:38
-
@Unni Kris :same as this – Jayashri Nov 05 '12 at 11:38
-
this may be helpful : http://stackoverflow.com/questions/10668308/how-to-simultaneously-display-a-splash-screen-and-then-my-jframe and http://www.roseindia.net/tutorial/java/swing/splashScreen.html – Unni Kris Nov 05 '12 at 11:40
-
Please see [this answer](http://stackoverflow.com/questions/13226164/how-to-move-jlabel-every-second/13226370#13226370) for nice example of Swing Timer. you would need to call `setRepeats(false);` on timer instance. – David Kroukamp Nov 05 '12 at 12:51
2 Answers
3
HINT
Use Swing Timer to wait for 5 seconds before calling setVisible(false)
or dispose()
whichever way you want it implemented. Hidden/Disposed

Mukul Goel
- 8,387
- 6
- 37
- 77
1
Did u do your research on this? Seems straight forward.
new Timer().schedule(new TimerTask() {
public void run() {
// this should be final
jframe.dispose();
}
}, 5000);

shazin
- 21,379
- 3
- 54
- 71
-
-
7Seems straightforward but with this approach you violate the Swing threading rules. Either wrap that `dispose` call in an `SwingUtilities.invokexxx` call or use a `javax.swing.Timer` – Robin Nov 05 '12 at 11:47