JProgressBar and my thread

Hey,

I am searching for a good snippet on how to bind a JProgressBar to my Thread for a while now. The forum posts i have read are not realy helping me out here.

can anyone tell me how i can monitor the progress of my thread. and perhaps give me a little snippet that could work?

Here is my thread.

/*

* FtpStream.java

*

* Created on 20 maart 2007, 14:38

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package ftpclient.models;

import com.jscape.inet.ftp.Ftp;

import com.jscape.inet.ftp.FtpException;

/**

*

* @author berts

*/

publicclass FtpStreamextends Thread{

publicfinalint UPLOAD = 20;

publicfinalint DOWNLOAD = 10;

publicfinalint FILE = 100;

publicfinalint DIRECTORY = 200;

publicint streamType;

publicint fileType;

public String path;

public Ftp ftp;

public FtpStream(Ftp ftp,String itemPath,int fileType,int streamType){

setPath(itemPath);

this.streamType = streamType;

this.fileType = fileType;

this.ftp = ftp;

}

publicvoid setStreamType(int streamType){

this.streamType = streamType;

}

publicint getStreamType(){

return streamType;

}

public String getPath(){

return path;

}

publicvoid setPath(String path){

this.path = path;

}

publicvoid run(){

switch (streamType){

case DOWNLOAD:

if ( fileType == FILE ){

System.out.println("Downloading file: " + path );

try{ ftp.download(path);}catch (FtpException ex){ ex.printStackTrace();}

System.out.println("File recieved: " + path );

}else{

System.out.println("Downloading dir: " + path );

try{ ftp.downloadDir(path);}catch (FtpException ex){ ex.printStackTrace();}

System.out.println("Directory recieved: " + path );

}

break;

}

}

}

[4539 byte] By [S1lv3ra] at [2007-11-26 22:16:29]
# 1
Looks like you would use Ftp.addFtpListener() to add a listener to your Ftp, andthen in the listener's progress() method, you could change the value of yourprogress bar. This latter would need to be done using SwingUtilities.invokeLater()so it gets done on the EDT.
JayDSa at 2007-7-10 11:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 2

Can't you just pass a JProgressBar to it in the constructor and call setValue as needed?

I've never used com.jscape.inet.ftp before but since you are using the single statement ftp.download(path); to download (so you can't for example, change the value every time the stream's buffer is full), the only way you will be able to modify the progressbar's value is if there is some kind of progress event listener that you can use to callback your code.

On a side note, you should probably use enums for streamtype and filetype and make your instance variables private (what's the point of having accessor and mutator methods if the instance variables are public anyway) The reason for using enums is so that when the FtpStream is used they cant just pass (ftp, "/somepath", 42, 37) but instead have to pass something like (ftp, "/somepath", FileType.File, StreamType.Download) to your constructor which would take the form FtpStream(Ftp, String, FileType, StreamType) because it's type safe, you don't have to worry about handling exceptions for invalid types as any bad calls are caught at compile time. The reason for making your instance variables private and providing accessor (getter) and mutator (setter) methods is so that the user of the class can't access your instance variables in an uncontrolled way, for example in your current code there is nothing to stop the user from calling myFtpStream.ftp = null; but if you provided a setFtp(Ftp) that caught nulls or just plain didn't provide a way of changing the ftp object after the constructor has been called, then they would not be able to do this. It's good that you have some gets and sets but at the moment they can be bypassed.

Ghelyara at 2007-7-10 11:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 3
@ JayDSCould u maybe give a little snippet on how to do this? Never had a progressbar up and running you see. I looked at the listener and the events. but i am not sure how to use the progress event to update the progressbar.
S1lv3ra at 2007-7-10 11:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 4

in that progress method:

Runnable progressUpdateter = new Runnable(){

public void run(){

yourProgressbar.setValue(xyz); //determine xyz from your ftp stuff ...or just increment some variable here...

}

};

SwingUtilities.invokeLater(progressUpdater);

jEti182a at 2007-7-10 11:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanx all,Now i have a nice progressbar inside a table showing me the progress of the up or download. There is only one problem. it only updates if i keep clicking on the table. if i do nothing the progressbar is not moving.. Any idea what this can be?
S1lv3ra at 2007-7-10 11:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 6

Here is the code.

It may look messy i know. any suggestions?

/*

* FtpStream.java

*

* Created on 20 maart 2007, 14:38

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package ftpclient.models;

import com.jscape.inet.ftp.Ftp;

import com.jscape.inet.ftp.FtpChangeDirEvent;

import com.jscape.inet.ftp.FtpCommandEvent;

import com.jscape.inet.ftp.FtpConnectedEvent;

import com.jscape.inet.ftp.FtpConnectionLostEvent;

import com.jscape.inet.ftp.FtpCreateDirEvent;

import com.jscape.inet.ftp.FtpDeleteDirEvent;

import com.jscape.inet.ftp.FtpDeleteFileEvent;

import com.jscape.inet.ftp.FtpDisconnectedEvent;

import com.jscape.inet.ftp.FtpDownloadEvent;

import com.jscape.inet.ftp.FtpException;

import com.jscape.inet.ftp.FtpListener;

import com.jscape.inet.ftp.FtpListingEvent;

import com.jscape.inet.ftp.FtpProgressEvent;

import com.jscape.inet.ftp.FtpRenameFileEvent;

import com.jscape.inet.ftp.FtpResponseEvent;

import com.jscape.inet.ftp.FtpUploadEvent;

import java.io.File;

import java.util.Vector;

import javax.swing.JProgressBar;

import javax.swing.table.DefaultTableModel;

/**

*

* @author berts

*/

public class FtpStream extends Thread implements FtpListener{

public static final int UPLOAD = 20;

public static final int DOWNLOAD = 10;

public static final int FILE = 100;

public static final int DIRECTORY = 200;

public int streamType;

public int fileType;

public String path;

public Ftp streamFtp;

public String currentDir;

public File localDir;

public JProgressBar jbar = new JProgressBar();

public DefaultTableModel queueModel;

public FtpStream(Ftp ftp,String itemPath,int fileType,int streamType,DefaultTableModel tm){

setPath(itemPath);

this.queueModel = tm;

this.streamType = streamType;

this.fileType = fileType;

this.streamFtp = ftp;

jbar.setStringPainted(true);

streamFtp.addFtpListener(this);

try {

currentDir = ftp.getDir();

localDir = ftp.getLocalDir();

} catch (FtpException ex) {

ex.printStackTrace();

}

}

public void setStreamType(int streamType) {

this.streamType = streamType;

}

public int getStreamType() {

return streamType;

}

public String getPath() {

return path;

}

public void setPath(String path) {

this.path = path;

}

public void run(){

switch (streamType){

case DOWNLOAD:

if ( fileType == FILE ){

System.out.println("Downloading file: " + path );

try {

streamFtp.connect();

streamFtp.setDir(currentDir);

streamFtp.setLocalDir(localDir);

streamFtp.download(path);

streamFtp.disconnect();

} catch (FtpException ex) { ex.printStackTrace(); }

System.out.println("File recieved: " + path );

}else{

System.out.println("Downloading dir: " + path );

try {

streamFtp.connect();

streamFtp.setDir(currentDir);

streamFtp.setLocalDir(localDir);

streamFtp.downloadDir(path);

streamFtp.disconnect();

} catch (FtpException ex) { ex.printStackTrace(); }

System.out.println("Directory recieved: " + path );

}

break;

}

//removeFromQueue();

}

public void connected(FtpConnectedEvent ftpConnectedEvent) {

}

public void disconnected(FtpDisconnectedEvent ftpDisconnectedEvent) {

}

public void deleteDir(FtpDeleteDirEvent ftpDeleteDirEvent) {

}

public void deleteFile(FtpDeleteFileEvent ftpDeleteFileEvent) {

}

public void renameFile(FtpRenameFileEvent ftpRenameFileEvent) {

}

public void createDir(FtpCreateDirEvent ftpCreateDirEvent) {

}

public void changeDir(FtpChangeDirEvent ftpChangeDirEvent) {

}

public void upload(FtpUploadEvent ftpUploadEvent) {

}

public void download(FtpDownloadEvent ftpDownloadEvent) {

}

public void progress(FtpProgressEvent ftpProgressEvent) {

jbar.setMaximum((int)ftpProgressEvent.getTotalBytes());

jbar.setValue((int)ftpProgressEvent.getBytes());

}

public void listing(FtpListingEvent ftpListingEvent) {

}

public void commandSent(FtpCommandEvent ftpCommandEvent) {

System.out.println(ftpCommandEvent.getCommand());

if (ftpCommandEvent.getCommand().startsWith("RETR")){

Vector vec = new Vector();

vec.addElement(ftpCommandEvent.getCommand());

vec.addElement("Downloaden");

vec.addElement(jbar);

queueModel.addRow(vec);

}

}

public void responseReceived(FtpResponseEvent ftpResponseEvent) {

}

public void connectionLost(FtpConnectionLostEvent ftpConnectionLostEvent) {

}

}

S1lv3ra at 2007-7-10 11:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 7
Got it.. just fred a propertychange in the progress method..ThanQ all..
S1lv3ra at 2007-7-10 11:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 8

You probably want that to be

public void progress(FtpProgressEvent ftpProgressEvent) {

Runnable progressUpdateter = new Runnable(){

public void run(){

jbar.setMaximum((int)ftpProgressEvent.getTotalBytes());

jbar.setValue((int)ftpProgressEvent.getBytes());

}

};

SwingUtilities.invokeLater(progressUpdater);

}

Since your FtpStream is a Thread different than the Event Dispatch Thread,

you want to queue your Swing related activies to the EDT via invokeLater.

JayDSa at 2007-7-10 11:09:47 > top of Java-index,Desktop,Core GUI APIs...