-2

I want to create a small Swing app. It has 2 JFrames. First JFrame has a button and when I click this button, the second JFrame will show . The second JFrame show in 2 seconds then it dispose. I want this action will run many time! I wrote this code, but it does not work:

 private void btnStudyActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    for( int i = 0 ; i< 10 ; i++){
        Show s = new Show();
        s.setVisible(true);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        s.dispose();
    }
}

Please give me a solution.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Vincenzo
  • 1
  • 1
  • 2
  • 1
    What is the purpose of this? By what it looks like, it will open up a new window, wait 2 seconds, then close it. But as you have put it in a loop, you wont notice the second window being closed, probably just a fast, almost unnoticeable blink... If you want to get a slow blink, you should put another Thread.sleep(2000) after calling s.dispose(); – Mateus Viccari May 28 '14 at 18:14
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 28 '14 at 18:20

2 Answers2

0

I suspect that this is not working because you are exceuting all this actions in the Event Dispatch Thread. So if i were you, i will try to put the code in a separate thread (for start you can use Thread class, but later i suggest to use SwingWorkers) and the see if the effect is the one you desired.

private void btnStudyActionPerformed(java.awt.event.ActionEvent evt) {

    new Thread(new Runnable() {

        @Override
        public void run() {
            Show s = new Show();
            s.setVisible(true);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
            }
            s.dispose();
        }
    }).start();
}

I don't know what the loop for. Anyway, i should recommend you to enrich yourself reading this chapter from the Sun/Oracle Tutorials.

Hope it helps!

Victor
  • 3,841
  • 2
  • 37
  • 63
0

are you trying to get a sort of blinking communicate like

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JFrame s = new JFrame();
    s.setSize(300, 200);
    s.add(new JLabel("Warning"));
    setVisible(true);
    new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {

                try {
                    s.setVisible(false);
                    Thread.sleep(100);
                    s.setVisible(true);
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ManyPopups.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            s.dispose();
        }
    }).start();
}

?

T.G
  • 1,913
  • 1
  • 16
  • 29