(resolvido) Atualizar jPanel em intervalo de tempo

3 respostas
Jauns

Bom dia.

Na rotina abaixo, ele retira jbutton de dentro de jpanel, ela funciona corretamente, porém, queria colocar um intervalo de tempo para a remoção de cada componente...
Porém esta acontecendo que, a rotina aguarda o tempo estipulado e so mostra somente o resultado final.

:arrow: Queria, q a medida que fosse retirando cada componente, o mesmo fosse atualizando o jpanel.

Alguém pode me dar uma ajuda :?:

jbRetira.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				while (josephus==false)
				{
					if(matriz[cont][2].equals("x")&&passo==vr)
					{
						try {
							for(int w=0;w<jpInsere.getComponentCount();w++)
							{
								if(jpInsere.getComponent(w).hashCode()==Integer.parseInt(matriz[cont][1]))
								{
									indice=w;								
								}
							}

							Thread.sleep(1000);    
				
							matriz[cont][2]="";
							jpInsere.remove(indice);

							jpInsere.revalidate();
							jpInsere.repaint();
							

						} catch (InterruptedException e) {							
							e.printStackTrace();
						}
					}
					cont++;
					if(cont>=quant)
						cont=0;
					if(matriz[cont][2].equals("x"))
					{	
						passo++;
						if(passo>vr)
							passo=0;
					}

					if(jpInsere.getComponentCount()==1)
						josephus=true;

				}
				
			}			
		});

3 Respostas

R

Recomendo dar uma olhada na classe Timer:

http://java.sun.com/javase/6/docs/api/javax/swing/Timer.html

Jauns
roger_rf:
Recomendo dar uma olhada na classe Timer:

[url]http://java.sun.com/javase/6/docs/api/javax/swing/Timer.html[/url]

public void tempo(){
		ActionListener action = new ActionListener() 
		{
			public void actionPerformed(ActionEvent evt) 
			{
				jpInsere.revalidate();
				jpInsere.repaint();  
			}  
		};  
		t = new Timer(2000, action);  
		t.start(); 

	}
public void tempo2(){

		try {
			Thread.sleep(1000);
			this.jpInsere.revalidate();
			this.jpInsere.repaint(); 		

		} catch (InterruptedException e) {

			e.printStackTrace();
		}
	}

:arrow: nenhum deu certo...

Ele fica preso no while, e so depois q sai q ele atualiza...

Jauns
roger_rf:
Recomendo dar uma olhada na classe Timer:

[url]http://java.sun.com/javase/6/docs/api/javax/swing/Timer.html[/url]

:arrow: Vlw pela dica.... era isso mesmo... deu certo aki... foi tirar while fora e trabalhar direto com a classe...
:arrow: obrigado

ActionListener action = new ActionListener() {
					public void actionPerformed(ActionEvent evt) {

						if (matriz[cont][2].equals("x") && passo == vr) {

							for (int w = 0; w < jpInsere.getComponentCount(); w++) {
								if (jpInsere.getComponent(w).hashCode() == Integer
										.parseInt(matriz[cont][1])) {
									indice = w;
								}
							}

							matriz[cont][2] = "";

							jpInsere.remove(indice);

							jpInsere.revalidate();

							jpInsere.repaint();

						}
						cont++;
						if (cont >= quant)
							cont = 0;
						if (matriz[cont][2].equals("x")) {
							passo++;
							if (passo > vr)
								passo = 0;
						}

						if (jpInsere.getComponentCount() == 1)
							t.stop();
					}
				};

				t = new Timer(1000, action);
				t.start();	
			}
		});
Criado 29 de junho de 2009
Ultima resposta 29 de jun. de 2009
Respostas 3
Participantes 2