JApplet + ActionListener

2 respostas
L

Boa tarde, alguém poderia me tirar uma dúvida em Swing. Veja o código abaixo:

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

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;

public class AppletTest extends JApplet implements ActionListener {

	private static final long	serialVersionUID	= 1L;

	private JButton				button;

	private JLabel				label;

	@Override
	public void start() {
		super.start();
		this.button = new JButton("Testar");
		this.label = new JLabel("Clique no botao");

		this.button.addActionListener(this);

		this.setLayout(new FlowLayout());
		this.add(this.button);
		this.add(this.label);
	}

	public void actionPerformed(final ActionEvent e) {
		this.label.setText("Executando a acao");
		try {
			Thread.sleep(10000);
		} catch (InterruptedException ie) {
			ie.printStackTrace();
		}
		this.label.setText("Ação executada");
	}

}

Como fazer para que quando clicar no botão o texto seja atualizado e depois que o processamento for feito, nesse exemplo o Thread.sleed(), o texto seja modificado novamente?.

Nesse exemplo o texto só é modificado na tela ao final da operação.

Grato.

2 Respostas

E

Já tentou usar o:

SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				//código que tu queres
			}
		});

???

L

Tentei sim, mas é uma Applet e da erro quando vai criar a Thread.

Criado 1 de agosto de 2011
Ultima resposta 2 de ago. de 2011
Respostas 2
Participantes 2