Skip to content

Instantly share code, notes, and snippets.

@tacksoo
Last active December 11, 2015 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tacksoo/4665121 to your computer and use it in GitHub Desktop.
Save tacksoo/4665121 to your computer and use it in GitHub Desktop.
EDT Example
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Unresponsive extends JFrame implements ActionListener{
public Unresponsive() {
super("Unresponsive");
JButton frame = new JButton("Unresponsive");
frame.addActionListener(this);
add(frame);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent arg0) {
/*
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
Thread t = new Thread(new Runnable() {
public void run() {
// do something that takes long here
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// update GUI
}
});
}
});
t.start();
}
public static void main(String[] args) {
/*
Unresponsive edt = new Unresponsive();
edt.setVisible(true);
*/
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Unresponsive edt = new Unresponsive();
edt.setVisible(true);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment