The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Query on JProgressBar (Help plz)

Posted by Bikash on December 22, 2001 at 3:31 AM

Hi,

I have one interface on swing from this interface I am uploading file to remote url(back end servlet programme).Now I want to add progress bar in my swing interface programme for showing the status of progressing uploading of file.I want to add my uploading programme in "run" method of Thread class.So that on written of bytes on outputstream progressbar should be updated.I tried but couldn't slove my problem.Any help will be highly appreciated.Below r my codes:-

In my programme I have two class one is extend from JFrame and other is extend from Thread.Now I want to add my uploading programme in run method of Thread class.

public class ActionDemo4 extends JFrame implements ActionListener
{
..............
..............
ImageIcon Upload=new ImageIcon("images/Upload.gif");
Button=new JButton(Upload);
Button.setToolTipText("Upload");
Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog fileDialog = new FileDialog(ActionDemo4.this );
fileDialog.setMode(FileDialog.LOAD);
fileDialog.show();

if (fileDialog.getFile() == null)
{
return;
}
File aa = new File( fileDialog.getDirectory(), fileDialog.getFile());
String aa1=fileDialog.getDirectory();
String aa2=fileDialog.getFile();
String a=aa1+aa2;
try
{
byte buff[]=new byte[(int)aa.length()];
InputStream fileIn=new FileInputStream(aa);
int i=fileIn.read(buff);
String conffile=new String(buff);
long l=aa.length();
textArea3.append(a);
textArea2.append("Local URL:");

String str1=textArea10.getText();


url = new URL ("http://127.0.0.1:7001/servletUpload?x="+str1);


urlConn = url.openConnection();

urlConn.setDoInput (true);

urlConn.setDoOutput (true);

urlConn.setUseCaches (false);

urlConn.setRequestProperty("Content-Type","multipart/form-data;boundary=-----------------------------7d11e410e500f2");

printout = new DataOutputStream (urlConn.getOutputStream ());

String preContent = "-----------------------------7d11e410e500f2\r\nContent-Disposition: form-data;name=\"upload\"; filename=\""+aa+"\"\r\nContent-Type:application/octet-stream\r\n\r\n";
String postContent = "\r\n-----------------------------7d11e410e500f2--\r\n";
printout.writeBytes(preContent);
printout.write(buff);
printout.writeBytes(postContent);

printout.flush ();
printout.close ();
progressBar.setMaximum(k);
activity=new SimulatedActivity(k);
activity.start();
activityMonitor.start();
Button.setEnabled(false);
}
catch (MalformedURLException k) {}
catch (IOException k) {}
}
});
activityMonitor=new Timer(1000,
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int current=activity.getCurrent();
progressBar.setValue(current);

if(current==activity.getTarget())
{
activityMonitor.stop();
try{
BufferedReader input = new BufferedReader (new InputStreamReader(urlConn.getInputStream ()));
String str;
while ((str = input.readLine())!=null)
{
textArea.append(str);
textArea.append("\n");
}

input.close ();
}
catch(Exception e){textArea.append("Error from input:"+e.toString());}

Button.setEnabled(true);

}

}
});
.................
..................
class SimulatedActivity extends Thread
{
public SimulatedActivity(int t)
{
current=0;
target=t;
}
public int getTarget()
{
return target;
}
public int getCurrent()
{
return current;
}
public void run()
{
..............
.............
}
private int current;
private int target;

Thnaks for ur Valuable time
Bikash




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us