swing - Java progress bar not progressing, though the file gets downloaded? -
i have visited this , , several other pages on forum , went through swing on oracle docs. of code doesn't work progressbar, if emulate codes line line , few adjustment in code. don't know why not working! feeling pity on myself can't make work.
i know there issue edt, but, on myself can't figure out. think code working on worker threads updating progress bar.
also, once when code downloads, again if reset fields , selected items, subsequent calls download blocked. file created,but not downloaded. size remains 0. please explain why?
i created swingworker instance , called updation of fileprogressbar doinbackground() method, , calling done(). but, didn't work. file gets downloaded successfully, but, progress bar doesn't move 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- hacker.mkv", "arun_asad.docx", "johnny sins.jpg", "mgh.java.the.complete.reference.9th.edition.pdf", "rabbit4.11-src.tar.gz", "samudra.jpg", "syngress - 7 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 item wanna download"); filestatusbutton.settext("check status of 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 delete already" + " existing file(press yes delete or no quit)???","confirm action", dialogbutton); if(dialogresult==joptionpane.yes_option){ new file(choiceoffile).delete(); } else { finallabel.settext("file exists!!! if wish, can download file..."); system.exit(-1); } } finallabel.settext("file size 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 sure want exit 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..."); } } }
Comments
Post a Comment