aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Query on ProgressBar(Help plz) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Query on ProgressBar(Help plz)" Watch "Query on ProgressBar(Help plz)" New topic
Author

Query on ProgressBar(Help plz)

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 javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JProgressBar;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.filechooser.*;
import java.io.*;
import java.net.*;
import java.applet.*;
public class ActionDemo4 extends JFrame implements ActionListener
{
public JTextArea textArea,textArea1,textArea2,textArea3,textArea4,textArea5,textArea6,textArea7,
textArea8,textArea9,textArea10,textArea11,textArea12,textArea13,textArea14,textArea15;
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);
}
});
final JFileChooser fc = new JFileChooser();
Container contentPane = getContentPane();
JMenuItem menuItem = null;
JToolBar toolBar = new JToolBar();
JToolBar toolBar1 = new JToolBar();
JToolBar toolBar2 = new JToolBar();
JToolBar toolBar3 = new JToolBar();
JToolBar toolBar4 = new JToolBar();
JToolBar toolBar5 = new JToolBar();
JToolBar toolBar6 = new JToolBar();
JToolBar toolBar7 = new JToolBar();
JToolBar toolBar8 = new JToolBar();
JToolBar toolBar9 = new JToolBar();
JToolBar toolBar10 = new JToolBar();
JMenu FirstMenu = new JMenu("File");
FirstMenu.setMnemonic(KeyEvent.VK_F);
textArea = new JTextArea(5, 20);
textArea1 = new JTextArea(2, 15);
textArea2 = new JTextArea(2, 15);
textArea3 = new JTextArea(2, 15);
textArea4 = new JTextArea(7, 15);
textArea5 = new JTextArea(7, 15);
textArea6 = new JTextArea(7, 15);
textArea7 = new JTextArea(7, 15);
textArea8 = new JTextArea(2, 15);
textArea9 = new JTextArea(7, 15);
textArea10 = new JTextArea(2, 15);
textArea11 = new JTextArea(7, 15);
textArea12 = new JTextArea(2, 15);
textArea13 = new JTextArea(7, 15);
textArea14 = new JTextArea(2, 15);
textArea15 = new JTextArea(7, 15);
textArea8.append("Remote URL:");
JScrollPane scrollPane = new JScrollPane(textArea);
progressBar=new JProgressBar();
progressBar.setStringPainted(true);
contentPane.setLayout(new GridLayout(2,1));
JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.setBackground(Color.blue);
contentPane.add(panel1);
panel1.add(BorderLayout.NORTH, toolBar);
panel1.add(scrollPane, BorderLayout.CENTER);
panel1.add(progressBar,BorderLayout.SOUTH);
JPanel panel2 = new JPanel();
JLabel statusBar= new JLabel();
panel2.setLayout(new GridLayout(1,1));
panel2.setBackground(Color.gray);
contentPane.add(panel2);
JPanel panel3 = new JPanel();
panel3.setLayout(new GridLayout(1,1));
panel3.setBackground(Color.blue);
panel2.add(panel3);
JPanel panel7 = new JPanel();
panel7.setLayout(new BorderLayout());
panel7.setBackground(Color.blue);
panel7.add(textArea2, BorderLayout.NORTH);
panel7.add(BorderLayout.CENTER, toolBar2);
panel7.add(BorderLayout.SOUTH, textArea4);
panel3.add(panel7);
JPanel panel8 = new JPanel();
panel8.setLayout(new BorderLayout());
panel8.setBackground(Color.blue);
panel8.add(textArea3, BorderLayout.NORTH);
panel8.add(BorderLayout.CENTER, toolBar3);
panel8.add(BorderLayout.SOUTH, textArea5);
panel3.add(panel8);
JPanel panel5 = new JPanel();
panel5.setLayout(new BorderLayout());
panel5.add(textArea1, BorderLayout.NORTH);
panel5.add(BorderLayout.CENTER, toolBar5);
panel5.add(BorderLayout.SOUTH, textArea7);
panel5.setBackground(Color.blue);
panel3.add(panel5);
JPanel panel6 = new JPanel();
panel6.setLayout(new BorderLayout());
panel6.add(toolBar1, BorderLayout.NORTH);
panel6.setBackground(Color.blue);
panel6.setSize(new Dimension(5,5));
panel6.add(BorderLayout.CENTER, toolBar4);
panel6.add(BorderLayout.SOUTH, textArea6);
panel3.add(panel6);
JPanel panel4 = new JPanel();
panel4.setLayout(new GridLayout(1,1));
panel4.setBackground(Color.blue);
panel2.add(panel4);
JPanel panel9 = new JPanel();
panel9.setLayout(new BorderLayout());
panel9.setBackground(Color.blue);
panel9.add(textArea8, BorderLayout.NORTH);
panel9.add(BorderLayout.CENTER, toolBar6);
panel9.add(BorderLayout.SOUTH, textArea9);
panel4.add(panel9);
JPanel panel10 = new JPanel();
panel10.setLayout(new BorderLayout());
panel10.setBackground(Color.blue);
panel10.add(textArea10, BorderLayout.NORTH);
panel10.add(BorderLayout.CENTER, toolBar7);
panel10.add(BorderLayout.SOUTH, textArea11);
panel4.add(panel10);
JPanel panel11 = new JPanel();
panel11.setLayout(new BorderLayout());
panel11.add(textArea12, BorderLayout.NORTH);
panel11.add(BorderLayout.CENTER, toolBar8);
panel11.add(BorderLayout.SOUTH, textArea13);
panel11.setBackground(Color.blue);

panel4.add(panel11);
JPanel panel12 = new JPanel();
panel12.setLayout(new BorderLayout());
panel12.add(toolBar9, BorderLayout.NORTH);
panel12.setBackground(Color.blue);
panel12.setSize(new Dimension(5,5));
panel12.add(BorderLayout.CENTER, toolBar10);
panel12.add(BorderLayout.SOUTH, textArea15);
panel4.add(panel12);

JMenuBar mb = new JMenuBar();
setJMenuBar(mb);
mb.add(FirstMenu);
menuItem = new JMenuItem("Quick connect", KeyEvent.VK_Q);
FirstMenu.add(menuItem);
menuItem = new JMenuItem("Reconnect", KeyEvent.VK_R);
FirstMenu.add(menuItem);
menuItem = new JMenuItem("Exit", KeyEvent.VK_E);
FirstMenu.add(menuItem);

JMenu SecondMenu = new JMenu("Edit");
SecondMenu.setMnemonic(KeyEvent.VK_E);
menuItem = new JMenuItem("Cut", KeyEvent.VK_C);
SecondMenu.add(menuItem);
menuItem = new JMenuItem("Copy", KeyEvent.VK_C);
SecondMenu.add(menuItem);
menuItem = new JMenuItem("Paste", KeyEvent.VK_P);
SecondMenu.add(menuItem);
mb.add(SecondMenu);
JMenu ThirdMenu = new JMenu("Compress");
ThirdMenu.setMnemonic(KeyEvent.VK_C);
menuItem = new JMenuItem("By Percentage", KeyEvent.VK_B);
ThirdMenu.add(menuItem);
mb.add(ThirdMenu);
JMenu FourthMenu = new JMenu("Help");
FourthMenu.setMnemonic(KeyEvent.VK_H);
menuItem = new JMenuItem("Help", KeyEvent.VK_B);
FourthMenu.add(menuItem);
mb.add(FourthMenu);
ImageIcon QuickConnect=new ImageIcon("images/Q_connect.gif");
JButton button=new JButton(QuickConnect);
button.setActionCommand("QuickConnect");
button.setToolTipText("Quick Connect");
button.addActionListener(this);
toolBar.add(button);
ImageIcon Reconnect=new ImageIcon("images/Reconnect.gif");
button=new JButton(Reconnect);
button.setActionCommand("Reconnect");
button.setToolTipText("Reconnect");
button.addActionListener(this);
toolBar.add(button);
ImageIcon Disconnect=new ImageIcon("images/Disconnect.gif");
button=new JButton(Disconnect);
button.setActionCommand("Disconnect");
button.setToolTipText("Disconnect");
button.addActionListener(this);
toolBar.add(button);
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);
ImageIcon Suspend=new ImageIcon("images/Suspend.gif");
button=new JButton(Suspend);
button.setActionCommand("Suspend");
button.setToolTipText("Suspend");
button.addActionListener(this);
toolBar.add(button);
ImageIcon Refresh=new ImageIcon("images/Refresh.gif");
button=new JButton(Refresh);
button.setActionCommand("Refresh");
button.setToolTipText("Refresh");
button.addActionListener(this);
toolBar.add(button);
ImageIcon DeleteItem=new ImageIcon("images/DeleteItem.gif");
button=new JButton(DeleteItem);
button.setActionCommand("DeleteItem");
button.setToolTipText("DeleteItem");
button.addActionListener(this);
toolBar.add(button);
ImageIcon Resume=new ImageIcon("images/Resume.gif");
button=new JButton(Resume);
button.setActionCommand("Resume");
button.setToolTipText("Resume");
button.addActionListener(this);
toolBar.add(button);
ImageIcon FilePriority=new ImageIcon("images/FilePriority.gif");
button=new JButton(FilePriority);
button.setActionCommand("FilePriority");
button.setToolTipText("FilePriority");
button.addActionListener(this);
toolBar.add(button);
ImageIcon View=new ImageIcon("images/View.gif");
button=new JButton(View);
button.setActionCommand("View");
button.setToolTipText("View");
button.addActionListener(this);
toolBar.add(button);
ImageIcon Rename=new ImageIcon("images/Rename.gif");
button=new JButton(Rename);
button.setActionCommand("Rename");
button.setToolTipText("Rename");
button.addActionListener(this);
toolBar.add(button);
ImageIcon Scrolling=new ImageIcon("images/left.gif");
button=new JButton(Scrolling);
button.setActionCommand("Scrolling");
button.setToolTipText("Scrolling");
button.addActionListener(this);
toolBar1.add(button);
ImageIcon UScrolling=new ImageIcon("images/right.gif");
button=new JButton( UScrolling);
button.setActionCommand(" UScrolling");
button.setToolTipText("UScrolling");
button.addActionListener(this);
toolBar1.add(button);
ImageIcon Name=new ImageIcon("images/Name.gif");
button=new JButton( Name);
button.setActionCommand("Name");
button.setToolTipText("Name");
button.addActionListener(this);
toolBar2.add(button);
ImageIcon Date=new ImageIcon("images/Date.gif");
button=new JButton( Date);
button.setActionCommand("Date");
button.setToolTipText("Date");
button.addActionListener(this);
toolBar3.add(button);
ImageIcon Time=new ImageIcon("images/Time.gif");
button=new JButton( Time);
button.setActionCommand("Time");
button.setToolTipText("Time");
button.addActionListener(this);
toolBar4.add(button);
ImageIcon Size=new ImageIcon("images/Size.gif");
button=new JButton(Size);
button.setActionCommand("Size");
button.setToolTipText("Size");
button.addActionListener(this);
toolBar5.add(button);
ImageIcon TScrolling=new ImageIcon("images/left.gif");
button=new JButton(TScrolling);
button.setActionCommand("TScrolling");
button.setToolTipText("TScrolling");
button.addActionListener(this);
toolBar9.add(button);
ImageIcon DScrolling=new ImageIcon("images/right.gif");
button=new JButton( DScrolling);
button.setActionCommand(" DScrolling");
button.setToolTipText("DScrolling");
button.addActionListener(this);
toolBar9.add(button);
ImageIcon Name1=new ImageIcon("images/Name.gif");
button=new JButton( Name1);
button.setActionCommand("Name1");
button.setToolTipText("Name1");
button.addActionListener(this);
toolBar6.add(button);
ImageIcon Date1=new ImageIcon("images/Date.gif");
button=new JButton( Date1);
button.setActionCommand("Date1");
button.setToolTipText("Date1");
button.addActionListener(this);
toolBar7.add(button);
ImageIcon Time1=new ImageIcon("images/Time.gif");
button=new JButton( Time1);
button.setActionCommand("Time1");
button.setToolTipText("Time1");
button.addActionListener(this);
toolBar10.add(button);
ImageIcon Size1=new ImageIcon("images/Size.gif");
button=new JButton(Size1);
button.setActionCommand("Size1");
button.setToolTipText("Size1");
button.addActionListener(this);
toolBar8.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: Query on ProgressBar(Help plz)