Re: Do I need Threads for this?
Daniel Dyer wrote:
On Tue, 05 Dec 2006 17:27:44 -0000, Colin Miller <jearil@gmail.com> wrote:
Ok, how about this. Create a new class of type Runnable (You'll need
to read up a bit on threading on this, and it's good to know anyway).
Inside of that class is where you will do your file copying. Also in
that class, have a variable to store whatever class holds your JPanel.
When creating an instance of this new class, pass it a reference to
that class for callback.
In your main class, or wherever you initiate the file copying, create
an instance of this new class and pass it "this" and set it to run. In
the class that does the file copying, after you finish copying a file,
you'll call a callback method to do the form update. Unfortunately, I'm
bad at explaining it.. I'll put some psudocode for you.
<Snipped code>
The basic approach is sound, but you should not call any Swing methods
(with the exception of some text component methods that are explicitly
marked as thread-safe) from any thread other than the Event Dispatch
Thread. So instead of directly calling setText on the label, you need
to submit a Runnable to the Event Dispatch Thread via one of the methods
in SwingUtilities (invokeLater or invokeAndWait). The run method for
this Runnable will perform any necessary GUI updates.
Dan.
--Daniel Dyer
http://www.uncommons.org
So this is how you do it in the simplest form. It looks really complex
but in reality it is very simple. Look at the code pieces and look at
the rules in comments.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test {
//
// EventQueue.invokeLater(r) is used to run a Runnable piece of code on
// the event dispatch thread or EDT.
//
// new Thread(r).start() is used to run a Runnable piece of code on
a new
// thread.
//
// The Swing GUI must be created on the EDT
//
// Code that takes a lot of time cannot be run on the EDT or the update
// of the GUI will be prevented until it is done.
//
// All code that updates the GUI must be run on the EDT
//
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel l = new JLabel(" ");
f.add(l,BorderLayout.NORTH);
JButton b = new JButton("Copy");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Runnable r = new Runnable() {
public void run() {
try {
// the sleep is to simulate copying
files
for (int i=0; i<10; i++) {
final int fn = i;
Runnable r = new Runnable() {
public void run() {
l.setText("Copying File
#" +
Integer.toString(fn));
}
};
EventQueue.invokeLater(r);
// copy file here
Thread.sleep(1000);
}
Runnable r = new Runnable() {
public void run() {
l.setText("All files copied");
}
};
EventQueue.invokeLater(r);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
};
new Thread(r).start();
}
});
f.add(b,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
--
Knute Johnson
email s/nospam/knute/