• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help me  RSS feed

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I want to add progress bar in my programme for showing the status of uploading of file.Can any one plz give me the code for that.I tried like that but couldn't slove my problem.Below r my codes:-
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class trial
{
public static void main(String[] args)
{
JFrame frame = new trial1();
frame.show();
}
}
class trial1 extends JFrame
{
.............
..................
public trial1()
{
setTitle("Upload Interface");
setSize(new Dimension(650, 400));
setLocation(100,100);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
...............
..................
ImageIcon Upload=new ImageIcon("images/Upload.gif");
Button=new JButton(Upload);
Button.setToolTipText("Upload");
toolBar.add(Button);
Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog fileDialog = new FileDialog(trial1.this );
fileDialog.setMode(FileDialog.LOAD);
fileDialog.show();
if (fileDialog.getFile() == null)
{
return;
}
File aa = new File( fileDialog.getDirectory(), fileDialog.getFile());
long l=aa.length();
m=(int)l;

activity.run();
progressBar.setMaximum(100);
activity=new SimulatedActivity(m);
activity.start();
activityMonitor.start();
Button.setEnabled(false);

}
});
activityMonitor=new Timer(50,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
{
private int current;
private int target;
private OutputStreamWriter printout;
URLConnection urlConn;
URL url;
JTextArea textArea;
public SimulatedActivity(int k)
{
current=0;
target=k;
}

public void run()
{

try
{
url = new URL ("http://127.0.0.1:7001/servletUpload");
urlConn = url.openConnection();

urlConn.setDoInput (true);

urlConn.setDoOutput (true);

urlConn.setUseCaches (false);
printout = new OutputStreamWriter(urlConn.getOutputStream ());
trial1 ta=new trial1();
int k=ta.m;
char[] buff = new char[k];
int totLengthWritten = 0;
int lengthWritten= 0;
CharArrayReader fileIn=null;

while ( true )
{
totLengthWritten += lengthWritten = fileIn.read(buff);

// upload this buff[] on stream opened on urlconn
printout.write(buff,0,lengthWritten);


// now the data has being written update current in percentage of file uploaded
current = totLengthWritten * 100 / k;
//textArea.append("\n"+"Your Selected File Name is "+aa);
textArea.append("\n"+"File Size Is "+k+"bytes");
textArea.append("\n"+ "Please Wait ! Upload Is in Progress");
}
}
catch(Exception e1){}
}

public int getCurrent()
{
return this.current;
}
public int getTarget()
{
return target;
}
}
I want to add my uploading programme in run method of SimulatedActivity class.Iam new to swing. Plz help me.
Thanks for ur valuable Time
Bikash
------------------
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!