Java Program to display Frame after some seconds

Java 8Object Oriented ProgrammingProgramming

Use Timer() to set seconds for delay i.e. to display frame after a few seconds −

Timer tm = new Timer(2000, new ActionListener() {
   //
}

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 −

Output

raja
Published on 09-May-2019 09:25:01
Advertisements