I have a program, that copies files from one drive to another written in java. Everything is working fine. I have added a progress bar to indicate the progress on file copying. It does not show up. Below is the codes to copy files. Can't figure out where am I going wrong. The file size is displayed correctly but, the progress bar does not show up.
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.lang.*;
public class CopyGUI1 extends JFrame implements ActionListener, ChangeListener
{
private JLabel fromLabel, zoneLabel;
private JTextField fromField, zoneField;
private JButton beginButton, amd, newSO;
private String to = "C:\\DCIN_TER\\DCIN_EPU2\\CIRCUIT FROM BRANCH\\Rainbow orders\\";
private String[] zone = {"SZ","NZ","WZ","EZ","JW","AZ"};
private JComboBox c;
private static JProgressBar progressBar;
int count = 0;
private long fileLength;
//Main method
public static void main(String[] args)
{
new CopyGUI1().setVisible(true);
}
public CopyGUI1()
{
init();
}
private void init()
{
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setBounds(300, 300, 500, 400);
setResizable(false);
setTitle("Copy SO from C:\\01_ORDER_PROCESSING\\ drive to C:\\DCIN_TER\\DCIN_EPU2\\ drive");
JPanel panel = new JPanel(null);
add(panel);
//add button and lebel
fromLabel = new JLabel("SO No.:");
fromLabel.setBounds(40, 20, 50, 20);
fromField = new JTextField();
fromField.setBounds(100, 20, 200, 20);
panel.add(fromLabel);
panel.add(fromField);
zoneLabel = new JLabel("Zone:");
zoneLabel.setBounds(40, 40, 50, 20);
zoneField = new JTextField();
zoneField.setBounds(140, 40, 200, 20);
panel.add(zoneLabel);
panel.add(zoneField);
c = new JComboBox();
for(int i = 0; i < 6; i++)
c.addItem(zone[count++]);
c.setEditable(false);
c.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
zoneField.setText("" + c.getSelectedItem());
}
});
c.setBounds(100, 40, 40, 20);
panel.add(c);
//add progress bar
progressBar = new JProgressBar();
progressBar.setBounds(50, 100, 300, 30);
progressBar.addChangeListener(this);
panel.add(progressBar);
beginButton = new JButton("Begin Copy");
beginButton.setBounds(140, 200, 150, 20);
beginButton.addActionListener(this);
panel.add(beginButton);
amd = new JButton("Copy Amendments");
amd.setBounds(140, 220, 150, 20);
amd.addActionListener(this);
panel.add(amd);
newSO = new JButton("Add a new SO");
newSO.setBounds(140, 240, 150, 20);
newSO.addActionListener(this);
panel.add(newSO);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource () == beginButton)
copy();
else if (e.getSource() == amd)
{
CopyAmd c = new CopyAmd();
c.show();
}
else if (e.getSource() == newSO)
{
CopyGUI1 cg = new CopyGUI1();
cg.show();
}
}
public void copy()
{
if(("").equals(fromField.getText()))
{
JOptionPane.showMessageDialog(this, "Input the SO Number!");
return;
}
if(("").equals(zoneField.getText()))
{
JOptionPane.showMessageDialog(this, "Input the zone!");
return;
}
Runnable r1 = new Runnable()
{
public void run()
{
final File srcFolder = new File("C:\\01_ORDER_PROCESSING\\IN11\\" + zoneField.getText() + "\\" + fromField.getText() + "\\");
File destFolder = new File(to + fromField.getText() + "\\");
if(destFolder.exists())
{
int op = JOptionPane.showConfirmDialog(null, destFolder.getName()+ " file exists! \n Do you want to overwrite it?","Confirm Window", JOptionPane.YES_NO_OPTION);
if (op == JOptionPane.NO_OPTION)
{
JOptionPane.showMessageDialog(null, "Copy Canceled");
fromField.setText("");
zoneField.setText("");
return;
}
else
{
//make sure source exists
if(!(srcFolder.exists()))
{
JOptionPane.showMessageDialog(null, "Directory does not exist. on " + srcFolder.getAbsolutePath() + " and hence, no files to copy.");
//just exit
dispose();
}
else
{
try
{
fileLength = srcFolder.length();
System.out.println("fileLength : " + fileLength);
progressBar.setMaximum((int) fileLength);
final long startTimeMillis = System.currentTimeMillis();
copyFolder(srcFolder, destFolder);
File file = new File(destFolder + "\\PADR Release\\");
if(!(file.exists()))
file.mkdir();
final long elapsedTimeMillis = System.currentTimeMillis() - startTimeMillis;
JOptionPane.showMessageDialog(null, "Copy Task Completed Successfully. The Task took " + elapsedTimeMillis + " milli seconds to complete!!");
dispose();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
else
{
//make sure source exists
if(!(srcFolder.exists()))
{
JOptionPane.showMessageDialog(null, "Directory does not exist. on " + srcFolder.getAbsolutePath() + " and hence, no files to copy.");
//just exit
dispose();
}
else
{
try
{
fileLength = srcFolder.length();
System.out.println("fileLength : " + fileLength);
progressBar.setMaximum((int) fileLength);
final long startTimeMillis = System.currentTimeMillis();
copyFolder(srcFolder, destFolder);
File file = new File(destFolder + "\\PADR Release\\");
if(!(file.exists()))
file.mkdir();
final long elapsedTimeMillis = System.currentTimeMillis() - startTimeMillis;
JOptionPane.showMessageDialog(null, "Copy Task Completed Successfully. The Task took " + elapsedTimeMillis + " milli seconds to complete!!");
dispose();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
};
Thread t1 = new Thread(r1);
t1.start();
}
public void stateChanged(ChangeEvent e)
{
if (e.getSource() == progressBar)
{
if (progressBar.getValue() == progressBar.getMaximum())
{
//JOptionPane.showMessageDialog(this, "Copy Over");
progressBar.setValue(0);
}
}
}
public static void copyFolder(File src, File dest) throws IOException
{
if(src.isDirectory())
{
//if directory does not exist, create it
if(!dest.exists())
{
dest.mkdir();
System.out.println("Directory copied from " + src + " to " + dest);
}
//list all the directory contents
String files[] = src.list();
for (String file : files)
{
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
//recursive copy
copyFolder(srcFile, destFile);
}
}
else
{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
int flag = 0;
while ((length = in.read(buffer)) > 0)
{
out.write(buffer, 0, length);
flag += length;
progressBar.setValue(flag);
}
in.close();
out.close();
System.out.println("File copied from " + src + " to " + dest);
}
}
}
I found an answer and modified a bit to suit to my needs. Thanks,
Following is the link : Need to have JProgress bar to measure progress when copying directories and files