swing - java catch process time real time progress bar -
new scanner(new url("some_url.txt").openstream(), "utf-8").usedelimiter("\\a").next();
using ^ data .txt-file save in string.
for progress bar
, wondered if possible jobs (or in general methods etc.) count (or sth. that) time needed finished. can show in real time process time
in bar
.
is somehow possible?
edit:
package app.gui; import java.awt.borderlayout; import java.awt.eventqueue; import java.net.url; import java.util.scanner; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jprogressbar; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import org.apache.commons.io.ioutils; public class updater { private jframe frame; private static string rawg; private static string versi; public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { rawg = new scanner(new url("some_url.txt").openstream(), "utf-8").usedelimiter("\\a").next(); versi = ioutils.tostring(getclass().getclassloader().getresourceasstream("version.txt")); } catch (exception e) { system.out.println("error class updater try/catch raw github"); } if (integer.parseint(rawg.split("\\.")[0]) < integer.parseint(versi.split("\\.")[0])) { system.out.println("version check failure, update needed"); try { updater window = new updater(); window.frame.setvisible(true); } catch (exception e) { system.out.println("error class updater try/catch initialize frame"); } } else { system.out.println("version check correct, no update needed"); } } }); } public updater() { initialize(); } private void initialize() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception e) { // todo auto-generated catch block e.printstacktrace(); } frame = new jframe(); frame.setbounds(100, 100, 450, 300); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel panel = new jpanel(); frame.getcontentpane().add(panel, borderlayout.south); panel.setlayout(new borderlayout(0, 0)); jprogressbar progressbar = new jprogressbar(); progressbar.setstringpainted(true); panel.add(progressbar, borderlayout.north); } }
is possible? yes. possible when using scanner.next() read contents of url? no.
you need read bytes yourself, , count them:
url url = new url("some_url.txt"); urlconnection conn = url.openconnection(); bytebuffer buffer = bytebuffer.allocate(conn.getcontentlength()); eventqueue.invokelater(() -> progressbar.setmaximum(buffer.limit())); eventqueue.invokelater(() -> progressbar.setvalue(0)); try (readablebytechannel channel = channels.newchannel(conn.getinputstream())) { while (channel.read(buffer) >= 0) { eventqueue.invokelater(() -> progressbar.setvalue(buffer.position())); } } buffer.flip(); string rawg = standardcharsets.utf_8.decode(buffer).tostring();
Comments
Post a Comment