PopUp create & Auto close

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
import org.junit.Test;

public class AutoClosePopup {
@Test
public void Login1() throws Exception {
JFrame f = new JFrame();
final JDialog dialog = new JDialog(f, “Test”, true);
JLabel label = new JLabel(“Please wait..” );
dialog.setSize(600, 600);
//Must schedule the close before the dialog becomes visible
ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
dialog.add(label);
dialog.setLocationRelativeTo(f);
dialog.pack(); //Dynamic size increase
s.schedule(new Runnable() {
public void run() {
dialog.setVisible(false); //should be invoked on the EDT
dialog.dispose();
}
}, 1000, TimeUnit.MILLISECONDS);

dialog.setVisible(true); // if modal, application will pause here

System.out.println(“Dialog closed”);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s