Re: Do I need Threads for this?
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.
//Main Class
//Note: This will probably look different
public class MainClass {
public static void main(String[] args) {
JFrame = new JFrame();
/* ... all your other components and whatever you do to set your
screen */
}
public void buttonClick(Event e) {
//I'll assume you start file copying on a button click or something
FileCopying fc = new FileCopying();
fc.setCallback(this);
new Thread(fc).start();
}
public void updateCopies(int copies) {
label.setText("Files copied: " + copies);
}
}
//FileCopying class
public class FileCopying implements Runnable {
private MainClass callback;
/* whatever else you need for copying */
public void setCallback(MainClass cb) {
callback = cb;
}
public void run() {
for (int i=0; i < filesToBeCopied; i++) {
/* do your file copying here */
callback.updateCopies(i);
}
}
}
I hope that made some sense.. been a while since I did graphical work
and threads and all of that, but should help with some ideas hopefully.
~Colin
nospam@invalid.invalid wrote:
Try calling label.repaint(); after you set the text. Or if you're using
say a JFrame, try calling the JFrame's repaint(); method.
It's possible that even though you updated the component, it was never
painted to the screen.
Thanks for the suggestions - I'd already tried that - it just doesn't update
until the for loop gets to the end then it updates the label and obviously
not as intended (ie only updates it with the finishing number rather than
updating it with the current number as it goes along)
: (