aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Modified Query on JProgressBar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Modified Query on JProgressBar" Watch "Modified Query on JProgressBar" New topic
Author

Modified Query on JProgressBar

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
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
------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Modified Query on JProgressBar
 
Similar Threads
Please help me
Query on ProgressBar(Help plz)
Error:java.lang.StringIndexOutOfBoundsException: String index out of range: -170
Error:java.lang.StringIndexOutOfBoundsException: String index out of range: -170
Query on Updating of ProgressBar