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]

# 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) {
}
}