0

A problem: have a simple GUI app with a single button, when I press the button it should print a message and after 2 seconds it should print another message, but when I use Thread.sleep(x); it don't execute the code above it and wait, this is the code:

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

 System.out.println("lol 1");

 try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {
        System.out.println("error");
}

System.out.println("lol 2");
}

But this don't work like I want.. cause it wait 2 seconds and then print lol 1 and lol 2.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    PSCGenerator ps = new PSCGenerator();
    String pin = jTextField1.getText();
    String msg[] = {"Cracking the database...\n","Database Cracked succesfully!\n","Running the exploit...\n","Passing '"+pin+"'  to the exploit..\n","New code succesfully spoofed!\n"};
    int time[] = {2000,1400,1000,2500};
    int x=0;
    long k=2000;
    jTextArea1.append(msg[x]);
    try {
        Thread.sleep(k);
    } catch (InterruptedException ex) {
        Logger.getLogger(PSCGUI.class.getName()).log(Level.SEVERE, null, ex);
    }

//creando un sistema che aspetta qualche secondo prima di continuare per la falsa console System.out.println("lol"); }

EzLemon
  • 1
  • 3
  • 2
    I doubt that. Post a minimal, complete program reproducing the problem. My guess is that you're not really using System.out.println(), but instead are trying to display text in your UI. Don't, ever, freeze the UI thread by using sleep(). Use a javax.swing.Timer. This question has been asked a thouand times, but I'm too lazy to find the best duplicate. Read the swing tutorial about concurrency. – JB Nizet Jun 12 '16 at 12:11

1 Answers1

0

You can use Timer

private void button1(java.awt.event.ActionEvent evt){
    Timer timer = new Timer(ms_you_wanna_wait, new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        // do your thing here
      }
    });
    timer.setRepeats(false); 
    timer.start(); 
}

[Edit] Seeing that you are having so much trouble, I put a small sample here:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;


public class Swing extends JFrame {

    JTextArea area;
    public Swing() {
        initUI();
    }

    public final void initUI() {

        JPanel panel = new JPanel();
        getContentPane().add(panel);

        panel.setLayout(null);

        area = new JTextArea();
        area.setBounds(10, 60, 80, 30);
        panel.add(area);

        JButton jButton = new JButton("Click");
        jButton.setBounds(90, 60, 80, 30);
        jButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                delayedAction();
            }
        });

        panel.add(jButton);

        setTitle("Quit button");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        Swing ex = new Swing();
            ex.setVisible(true);

    }

    public void delayedAction(){
        String pin = area.getText();
        String msg[] = {"Cracking the database...\n","Database Cracked succesfully!\n","Running the exploit...\n","Passing '"+pin+"'  to the exploit..\n","New code succesfully spoofed!\n"};
        int time[] = {2000,1400,1000,2500};
        int x=0;
        long k=2000;

        Timer timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO add your handling code here:
                //PSCGenerator ps = new PSCGenerator();

                area.append(msg[x]);
            }
        });
        timer.setRepeats(false);
        timer.start();
    }
}
dumb_terminal
  • 1,815
  • 1
  • 16
  • 27