• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Modified Query on JProgressBar

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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 ActionDemo4 extends JFrame implements ActionListener
{
public JTextArea textArea;
public JProgressBar progressBar;
public Timer activityMonitor;
public SimulatedActivity activity;
public JButton Button;
URL url;
URLConnection urlConn;
DataOutputStream printout;
public ActionDemo4()
{
setTitle("Upload Interface");
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");

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 ();

int k=(int)l/1000;
progressBar.setMaximum(k);
activity=new SimulatedActivity(k);
activity.start();
activityMonitor.start();
Button.setEnabled(false);


textArea.append("\n"+"Your Selected File Name is "+aa);
textArea.append("\n"+"File Size Is "+l+"bytes");
textArea.append("\n"+ "Please Wait ! Upload Is in Progress");
//textArea.append("\n"+str2);

}
catch (MalformedURLException k) {}
catch (IOException k) {}
}
});

activityMonitor=new Timer(1,
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);
}
}
});

toolBar.add(Button);

}
public void actionPerformed(ActionEvent e)
{
textArea.setText(e.getActionCommand());
}
public static void main(String[] args) {
ActionDemo4 frame = new ActionDemo4();
frame.pack();
frame.setSize(new Dimension(650, 400));
frame.setLocation(100,100);
frame.setVisible(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()
{
while(current<target && !interrupted())>
{
try
{
sleep(10);
}
catch(InterruptedException e)
{
return;
}
current++;
}
}
private int current;
private int 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
------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic