سؤال

This is my code:

public Loading(Evento e){
    initComponents();
    setLocationRelativeTo(null);
    this.evento = e;
    new Thread(new thread1()).start();
}
public class thread1 implements Runnable{
    @Override
public void run(){
        for (int i=0; i<=100; i++){ //Progressively increment variable i
            jpb.setValue(i); //Set value
            jpb.repaint(); //Refresh graphics
            try{
                Thread.sleep(30);
                if(i==100){
                    GestaoEvento a = new GestaoEvento(evento);
                    a.setVisible(true);
                    --->>>>this.dispose();<<<<---
                }
            } //Sleep 50 milliseconds
            catch (InterruptedException err){}
        }
}
}

this.dispose() doesn't work there because this refers to the thread, how can I close this frame there?

هل كانت مفيدة؟

المحلول

Related to your question: this refer to the instance of inner class. You have to use Loading.this.dispose().

Most important

Calling jpb.repaint(); in another thread that is not the EDT can cause unexpected behaviour, and calling Thread.sleep(30); is gonna to freeze your gui. Instead use a SwingTimer

Read more: How to use SwingTimers | Concurrency in Swing

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top