Fórum Contagem regressiva de um jFrame podendo alterar dados #507203
13/01/2015
0
Boa noite,
Caros tenho um botão que após o clique o mesmo deve abrir um JFrame onde o mesmo irá atualizar a informação(recuperada do bd) dentro de alguns segundos(definido pelo usuário). Alguém dá uma ajuda, eu fiz até o momento de exibir o JFrame porém o mesmo trava pq eu fiz um Loop com threads.
Segue abaixo o meu código.
Botão:
Teste t = new Teste();
t.setVisible(true);
t.setExtendedState(JFrame.MAXIMIZED_BOTH);
JFrame Teste:
public Teste() {
initComponents();
Loop();
}
public void Loop() {
try {
for (int i = 5; i > 0; i--) {
Timer t = new Timer(i, null);
System.out.println(i + " segundos");
jLblTempo.setText(""+i);
System.out.println("");
Thread.sleep(1000); // 1 segundo
}
JOptionPane.showMessageDialog(null, "Voltar Tela Anterior");
} catch (InterruptedException e) {
System.out.println("X");
}
}
Caros tenho um botão que após o clique o mesmo deve abrir um JFrame onde o mesmo irá atualizar a informação(recuperada do bd) dentro de alguns segundos(definido pelo usuário). Alguém dá uma ajuda, eu fiz até o momento de exibir o JFrame porém o mesmo trava pq eu fiz um Loop com threads.
Segue abaixo o meu código.
Botão:
Teste t = new Teste();
t.setVisible(true);
t.setExtendedState(JFrame.MAXIMIZED_BOTH);
JFrame Teste:
public Teste() {
initComponents();
Loop();
}
public void Loop() {
try {
for (int i = 5; i > 0; i--) {
Timer t = new Timer(i, null);
System.out.println(i + " segundos");
jLblTempo.setText(""+i);
System.out.println("");
Thread.sleep(1000); // 1 segundo
}
JOptionPane.showMessageDialog(null, "Voltar Tela Anterior");
} catch (InterruptedException e) {
System.out.println("X");
}
}
Yuri Moraes
Curtir tópico
+ 0
Responder
Posts
13/01/2015
Yuri Moraes
Consegui uma parte....
private void Loop(){
try{
Thread thread = new Thread() {
@Override
public void run() {
// código aqui
try {
for (int i = 5; i > 0; i--) {
Timer t = new Timer(i, null);
System.out.println(i + " segundos");
jLblTempo.setText(""+i);
System.out.println("");
Thread.sleep(1000); // 1 segundo
}
JOptionPane.showMessageDialog(null, "Desligando o sistema");
setVisible(false);
} catch (InterruptedException e) {
System.out.println("X");
}
}};
thread.start();
}catch(Exception e)
{
System.out.println(""+e.getMessage());
}
}
private void Loop(){
try{
Thread thread = new Thread() {
@Override
public void run() {
// código aqui
try {
for (int i = 5; i > 0; i--) {
Timer t = new Timer(i, null);
System.out.println(i + " segundos");
jLblTempo.setText(""+i);
System.out.println("");
Thread.sleep(1000); // 1 segundo
}
JOptionPane.showMessageDialog(null, "Desligando o sistema");
setVisible(false);
} catch (InterruptedException e) {
System.out.println("X");
}
}};
thread.start();
}catch(Exception e)
{
System.out.println(""+e.getMessage());
}
}
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)