
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Frame After Some Seconds in Java
In this article, we will learn how to display a frame after a few seconds in Java using the Timer class. This example shows how to delay the visibility of a frame by 2 seconds using Swing.
Timer() class
The Timer class is used to schedule tasks that execute after a specific time interval or repeatedly at regular intervals. It allows tasks to be run by a thread that handles the scheduling and execution. Each task can be set to execute either once or repeatedly at fixed intervals. The execution of all tasks is managed by a background thread associated with the Timer object.
- The Timer class is thread-safe, meaning it can safely be used in multi-threaded environments.
- The Timer class uses a binary heap data structure to efficiently store and manage scheduled tasks.
Constructors:
Constructors
-
Timer(): Initializes a new timer with a default configuration.
-
Timer(boolean isDaemon): Initializes a new timer, with the option to specify whether its background thread should be a daemon thread.
-
Timer(String name): Creates a new timer with a specified name for its background thread.
- Timer(String name, boolean isDaemon): Initializes a timer with a specified name for the background thread, and allows setting whether the thread should run as a daemon.
Use Timer() to set seconds for delay i.e. to display the frame after a few seconds ?
Timer tm = new Timer(2000, new ActionListener() { // }
Steps to display frame after some seconds
The following are the steps to display the frame after some seconds ?
-
Step 1. Setting up the Frame: A new frame is created, with the size set and a default close operation defined. A JFrame is initialized and configured with a size of 550x300. The EXIT_ON_CLOSE operation is set, so the program terminates when the frame is closed.
private JFrame frame = new JFrame();
frame.setSize(550, 300);
-
Step 2. Initial Frame Visibility: The frame is initially set to an iconified state, making it minimized when first displayed. This code sets the frame to be visible but starts in a minimized (iconified) state, meaning it won't appear in its full window size immediately.
frame.setVisible(true);
frame.setExtendedState(JFrame.ICONIFIED);
-
Step 3. Using Timer for Delay: A Timer object is created to introduce a 2-second delay before making the frame visible in its normal state. A Timer is initialized with a 2000 millisecond (2-second) delay. When the timer triggers, the frame is restored to its normal state. The setRepeats(false) ensures this only happens once.
Timer tm = new Timer(2000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
frame.setExtendedState(JFrame.NORMAL);
}
});
}
});
tm.setRepeats(false);
tm.start();
Java program to display frame after some seconds
The following is an example to display Frame after some seconds ?
package my; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.Timer; public class SwingDemo extends JFrame { private JFrame frame = new JFrame(); public SwingDemo() { frame.setSize(550, 300); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); frame.setExtendedState(JFrame.ICONIFIED); Timer tm = new Timer(2000, new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { frame.setExtendedState(JFrame.NORMAL); } }); } }); tm.setRepeats(false); tm.start(); } public static void main(String[] args) { new SwingDemo(); } }
The output is as follows displaying that the Frame appears after 2 seconds ?