swing - Java Value must Be Between 0 and 100 -
i have code:
package com.cjburkey.downloads.wie_ein_chef; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.beans.propertychangeevent; import java.beans.propertychangelistener; import java.io.bufferedoutputstream; import java.io.file; import java.net.httpurlconnection; import java.net.url; import java.util.concurrent.executionexception; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jprogressbar; import javax.swing.swingworker; import net.lingala.zip4j.core.zipfile; import net.lingala.zip4j.exception.zipexception; public class download { final static jframe frm = new jframe(); final static jlabel status = new jlabel("downloading..."); final static jbutton cancel = new jbutton("cancel"); static file filedown = null; static string dire = null; public download(string site, file file, string dir) { filedown = file; dire = dir; final jprogressbar current = new jprogressbar(0, 100); current.setsize(50, 100); current.setvalue(0); current.setstringpainted(true); frm.setsize(640, 480); frm.setdefaultcloseoperation(jframe.exit_on_close); frm.setlayout(new flowlayout()); frm.add(status); frm.add(current); frm.add(cancel); frm.pack(); frm.setlocationrelativeto(null); frm.setresizable(false); frm.setvisible(true); final worker worker = new worker(site, file); worker.addpropertychangelistener(new propertychangelistener() { @override public void propertychange(propertychangeevent pcevt) { if ("progress".equals(pcevt.getpropertyname())) { current.setvalue((integer) pcevt.getnewvalue()); } else if (pcevt.getnewvalue() == swingworker.statevalue.done) { try { worker.get(); } catch (interruptedexception | executionexception e) { joptionpane.showmessagedialog(frm, e.getmessage(), null, joptionpane.error_message); frm.getcontentpane().removeall(); frm.dispose(); home.frame.getcontentpane().removeall(); home.frame.repaint(); new home(); } } } }); cancel.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { int option = joptionpane.showconfirmdialog(frm, "are sure wish cancel download?"); if(option == joptionpane.yes_option) { download.filedown.delete(); system.exit(0); } else { joptionpane.showmessagedialog(frm, "resuming"); } } }); worker.execute(); } } class worker extends swingworker<void, void> { private string site; private file file; public worker(string site, file file) { this.site = site; this.file = file; } @override protected void doinbackground() throws exception { url url = new url(site); httpurlconnection connection = (httpurlconnection) url.openconnection(); int filesize = connection.getcontentlength(); int totaldataread = 0; try (java.io.bufferedinputstream in = new java.io.bufferedinputstream(connection.getinputstream())) { java.io.fileoutputstream fos = new java.io.fileoutputstream(file); try (java.io.bufferedoutputstream bout = new bufferedoutputstream(fos, 1024)) { byte[] data = new byte[1024]; int i; while ((i = in.read(data, 0, 1024)) >= 0) { totaldataread = totaldataread + i; bout.write(data, 0, i); int percent = (totaldataread * 100) / filesize; download.status.settext(totaldataread / 1024 + "kb/" + filesize / 1024 + "kb"); setprogress(percent); download.frm.settitle("downloading file"); download.frm.pack(); download.frm.setlocationrelativeto(null); if(percent == 100.0) { jbutton finish = new jbutton("finish install(warning: override current minecraft profiles)"); download.frm.getcontentpane().removeall(); download.frm.repaint(); download.frm.add(finish); download.frm.settitle("extracting..."); download.frm.pack(); download.frm.setlocationrelativeto(null); finish.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { download.status.settext("waiting"); zipfile file = null; try { file = new zipfile(download.filedown); } catch (zipexception e1) { joptionpane.showmessagedialog(null, e1.getmessage(), null, joptionpane.error_message); } system.out.println(download.filedown.tostring()); file.setruninthread(true); try { file.extractall(download.dire); joptionpane.showmessagedialog(null, "completed install!!"); download.filedown.delete(); system.exit(0); } catch (zipexception e1) { joptionpane.showmessagedialog(null, e1.getmessage(), null, joptionpane.error_message); } } }); } } } } return null; } }
and used work fine, now, when reaches 27%, gives error complaining how value must between 0 , 100, thing is, used work, , suddenly, doesn't. why?
the problem experiencing piece of code:
int percent = (totaldataread * 100) / filesize; download.status.settext(totaldataread / 1024 + "kb/" + filesize / 1024 + "kb"); setprogress(percent);
your value percent must outside range 0...100 exception message coming setprogress method: http://docs.oracle.com/javase/7/docs/api/javax/swing/swingworker.html#setprogress(int)
note:
illegalargumentexception - value not 0 100
Comments
Post a Comment