Question:
I have a configuration window that opens on first run of the application I'm developing. After typing the directories that the application will run, the user clicks on save, some tests are executed and finally a Thread runs.
I would like to make the configuration window close by itself after finishing the execution of this Thread, but I tried to execute dispose()
and setVisible()
and it disappears before executing the Thread.
// Evento para salvar os diretórios
btnSalvar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Testa o campo com o diretório a ser indexado
// Se está vazio
if ((txtDirIndexado.getText().length() == 0 || Character
.toString(txtDirIndexado.getText().charAt(0)).equals(
" "))) {
JOptionPane
.showMessageDialog(panel,
"O campo com o diretório a ser indexado não pode estar em branco.");
} else if ((txtDirIndice.getText().length() == 0 || Character
.toString(txtDirIndexado.getText().charAt(0)).equals(
" "))) {
JOptionPane
.showMessageDialog(panel,
"O campo com o diretório que guardará o índice não pode estar em branco.");
} else {
try {
ArquivoDeConfiguracao.defineFonte(txtDirIndexado.getText());
ArquivoDeConfiguracao.defineIndice(txtDirIndice.getText());
ArquivoDeConfiguracao.definePrimeiraExecucao(1);
// Janela de aguardo
final JDialog janelaProgresso = new IndexAndo();
// Cria um novo processo e mostra a janela
new Thread(new Runnable() {
public void run() {
Indexador ind = new Indexador();
ind.iniciaIndexacao();
// Ao terminar fecha
SwingUtilities.invokeLater(new Runnable() {
public void run() {
janelaProgresso.setVisible(false);
}
});
}
}).start();
// Salva a data
dispose();
ArquivoDeConfiguracao.defineIndiceUltAtualizacao();
} catch (IOException e) {
// Gera erro
e.printStackTrace();
}
}
}
});
The full code can be accessed in git .
Edition:
// Evento para salvar os direstórios
btnSalvar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Testa o campo com o diretório a ser indexado
// Se está vazio
if ((txtDirIndexado.getText().length() == 0 || Character
.toString(txtDirIndexado.getText().charAt(0)).equals(
" "))) {
JOptionPane
.showMessageDialog(panel,
"O campo com o diretório a ser indexado não pode estar em branco.");
// Se está vazio
} else if ((txtDirIndice.getText().length() == 0 || Character
.toString(txtDirIndexado.getText().charAt(0)).equals(
" "))) {
JOptionPane
.showMessageDialog(panel,
"O campo com o diretório que guardará o índice não pode estar em branco.");
// Se o diretório existe
} else {
try {
ArquivoDeConfiguracao.defineFonte(txtDirIndexado.getText());
ArquivoDeConfiguracao.defineIndice(txtDirIndice.getText());
ArquivoDeConfiguracao.definePrimeiraExecucao(1);
final JDialog janelaProgresso = new IndexAndo();
new Thread(new Runnable() {
public void run() {
ProcessoIndexacao base = new ProcessoIndexacao();
base.start();
synchronized (base){
try {
base.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
janelaProgresso.setVisible(false);
}
});
}
}).start();
janelaProgresso.setVisible(true);
// Salva a data
ArquivoDeConfiguracao.defineIndiceUltAtualizacao();
} catch (IOException e) {
// Gera erro
e.printStackTrace();
}
}
dispose();
}
public class ProcessoIndexacao extends Thread{
public void run(){
synchronized (this){
Indexador ind = new Indexador();
ind.iniciaIndexacao();
notify();
}
}
}
Answer:
I didn't see your entire code, but if I understand your problem correctly, you want a Thread to wait for another one to finish to be executed.
Basically what you need is to understand the concepts of wait()
and notify()
.
Here is an example I implemented while studying for the Java certification exam:
public class WaitNotify {
public static void main(String[] args) {
//aqui você inicia uma nova Thread e manda ela rodar
ThreadB b = new ThreadB();
b.start();
//Thread atual deve ter o "lock" da thread b,isso é necessário para chamar o wait()
synchronized(b) {
System.out.print("Waiting for b to complete...");
try {
//aqui você diz que a Thread atual deve esperar a Thread b terminar
b.wait();
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("Total is: " + b.total);
}
}
}
class ThreadB extends Thread {
int total;
//aqui sua Thread começa a rodar
@Override
public void run() {
synchronized(this) {
for(int i=0; i<16; i++) {
total += i;
try {
//um temporizador para sua Thread não acabar na velocidade da luz!
Thread.sleep(300);
} catch(InterruptedException e) {
e.printStackTrace();
}
//faz uma frescura
if(i%2 == 0) {
System.out.print(".");
}
}
System.out.println();
//Thread avisa que terminou sua execução para quem possui o lock de b
notify();
}
}
}
Remember that when you run a class that has a main()
method you are running a thread called main , as in the example above, the main thread is a thread like any other, so just make it wait for another thread until it calls notify()
.