I have visited this and and several other pages like this on this forum and went through Swing on Oracle docs. All of their code doesn't work for my ProgressBar, even if I emulate all their codes line by line and with a few adjustment in my code. I don't know why it is not working! I am really feeling PITY on myself that I can't make it work.
I know that there is some issue with EDT, but, I on myself can't figure it out. I think my code is working on Worker Threads for updating the progress bar.
Also, once when the code downloads, then again if I reset the fields and the selected items, then the subsequent calls to download are also blocked. The file is created,but not downloaded. Their size remains 0. Please explain why?
I created a SwingWorker instance and called updation of FileProgressBar from the doInBackground() method, and then calling done(). But, it didn't work. The file gets downloaded successfully, but, the progress bar doesn't move by an inch!!!
public class Client{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MakeGUI().setVisible(true);
}
});
}
}
class MakeGUI extends JFrame implements PropertyChangeListener{
private JTextField ClientNameTF;
private JLabel DisplayLabel;
private JButton DownloadButton;
private JButton ExitButton;
private JComboBox FileComboBox;
private JProgressBar FileProgressBar;
private JButton FileStatusButton;
private JLabel FinalLabel;
private JTextField StatusTextField;
private JLabel TitleLabel;
private JButton ResetButton;
private JLabel LoginLabel;
private ProgressWorker pw;
private static JFrame jf;
public static String IP_ADDRESS_TO_CONNECT;
public MakeGUI() {
FileComboBox = new javax.swing.JComboBox();
DisplayLabel = new javax.swing.JLabel();
FileStatusButton = new javax.swing.JButton();
StatusTextField = new javax.swing.JTextField();
DownloadButton = new javax.swing.JButton();
FileProgressBar = new javax.swing.JProgressBar();
ExitButton = new javax.swing.JButton();
FinalLabel = new javax.swing.JLabel();
TitleLabel = new javax.swing.JLabel();
LoginLabel = new javax.swing.JLabel();
ClientNameTF = new javax.swing.JTextField();
ResetButton = new javax.swing.JButton();
callPainting();
}
public void callPainting(){
setLocationRelativeTo(null);
setBounds(300, 0, 300, 300);
FileComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ALGORITHM- The Hacker.MKV", "ARUN_ASAD.docx", "Johnny Sins.jpg", "MGH.Java.The.Complete.Reference.9th.Edition.pdf", "rabbit4.11-src.tar.gz", "SAMUDRA.jpg", "Syngress - Seven Deadliest Web Application Attacks (2010) (ATTiCA).pdf", "Tu Hai Ki Nahi (Roy).mp3" }));
FinalLabel.setLabelFor(FinalLabel);
TitleLabel.setText("Distributed File Downloading System");
LoginLabel.setText("Your Login Name");
DisplayLabel.setText("Select the Item you wanna download");
FileStatusButton.setText("Check Status of the File");
FileStatusButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
FileStatusButtonActionPerformed(evt);
}
});
DownloadButton.setText("Start Download");
DownloadButton.setEnabled(false);
DownloadButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
DownloadButtonActionPerformed(evt);
}
});
FileProgressBar.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(187, 55, 55)));
FileProgressBar.setStringPainted(true);
FileProgressBar.setMinimum(0);
FileProgressBar.setMaximum(100);
FileProgressBar.setValue(0);
//FileProgressBar.setIndeterminate(false);
ResetButton.setText("Reset");
ResetButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
ResetButtonActionPerformed(evt);
}
});
ExitButton.setText("Exit Application");
ExitButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitButtonActionPerformed(evt);
}
});
pack();
}
public void FileStatusButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String clientName=ClientNameTF.getText();
int choiceOfFile = FileComboBox.getSelectedIndex();
new Server().requestFile(choiceOfFile,clientName);
IP_ADDRESS_TO_CONNECT=new Server().returnIPAddress();
StatusTextField.setText("Server Available = "+IP_ADDRESS_TO_CONNECT);
if(!(StatusTextField.getText().isEmpty()))
DownloadButton.setEnabled(true);
else
DownloadButton.setEnabled(false);
} catch (IOException ex) {
FinalLabel.setText(ex.getMessage());
}
}
public void DownloadButtonActionPerformed(java.awt.event.ActionEvent evt) {
DownloadButton.setEnabled(false);
boolean COMPLETION=false;
long fsize=new Server().returnFileSize();
String fileSize;
Long initialTime = 0L;
if(fsize <= 0) {
fileSize = "0";
}
final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(fsize)/Math.log10(1024));
fileSize= new DecimalFormat("#,##0.#").format(fsize/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
String choiceOfFile = (String)FileComboBox.getSelectedItem();
int dialogButton=JOptionPane.YES_NO_OPTION;
if(new File(choiceOfFile).exists()){
int dialogResult=JOptionPane.showConfirmDialog(null,"Would you like to delete your already"
+ " existing file(Press Yes to delete OR No to quit)???","Confirm Action", dialogButton);
if(dialogResult==JOptionPane.YES_OPTION){
new File(choiceOfFile).delete();
}
else {
FinalLabel.setText("File Already Exists!!! If you wish, you can download some another file...");
System.exit(-1);
}
}
FinalLabel.setText("File Size to be Downloaded = "+fileSize);
ProgressWorker pw = new ProgressWorker(choiceOfFile,IP_ADDRESS_TO_CONNECT,fsize);
initialTime = System.nanoTime();
pw.addPropertyChangeListener(this);
/*else if (name.equals("state")) {
SwingWorker.StateValue state = (SwingWorker.StateValue)evt.getNewValue();
switch (state) {
case DONE :
{
DownloadButton.setEnabled(true);
break;
}
}
}*/
pw.execute();
try {
initialTime = pw.get();
} catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace();
}
long finalTime=System.nanoTime();
float difference = (float)((finalTime-initialTime)/1000000000.0);
StatusTextField.setText("Time difference = "+difference);
}
public void ResetButtonActionPerformed(java.awt.event.ActionEvent evt) {
ClientNameTF.setText("");
StatusTextField.setText("");
FinalLabel.setText("");
FileComboBox.setSelectedIndex(0);
FileProgressBar.setValue(0);
}
public void ExitButtonActionPerformed(java.awt.event.ActionEvent evt) {
int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit this application?", "Confirm",
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
System.out.println("No button clicked");
} else if (response == JOptionPane.YES_OPTION) {
System.exit(0);
} else if (response == JOptionPane.CLOSED_OPTION) {
System.out.println("JOptionPane closed");
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if("progress" == evt.getPropertyName()){
int progress = (Integer)evt.getNewValue();
FileProgressBar.setValue(progress);
}
}
class ProgressWorker extends SwingWorker<Long,Integer>{
String cof,IP;
long fileSize;
Long initialTime;
File downloadingFile;
public ProgressWorker(String choiceOfFile,String IP_ADDRESS_TO_CONNECT,long fsize) {
this.cof = choiceOfFile;
this.IP = IP_ADDRESS_TO_CONNECT;
this.fileSize = fsize;
downloadingFile = new File("/home/you/Desktop/BASH/Client",cof);
}
@Override
public Long doInBackground() throws Exception {
int temp=1;
boolean flag=true;
int progressPercent = 0;
setProgress(progressPercent);
initialTime=System.nanoTime();
if(SwingUtilities.isEventDispatchThread()==true)
System.out.println("True EDT.");
else
System.out.println("Worker Threads...");
StartFileClient.startDownloading(cof,IP);
while(progressPercent<100 && downloadingFile.length() != fileSize){
//System.out.println("Length = "+downloadingFile.length());
progressPercent = (int)((downloadingFile.length()*100.0)/fileSize);
setProgress(progressPercent);
Thread.sleep(50);
}
return initialTime;
}
@Override
public void done(){
Toolkit.getDefaultToolkit().beep();
DownloadButton.setEnabled(true);
FinalLabel.setText("Downloading Done...");
}
}
}