java - Why do JButtons that open webpages only work the first time one is clicked and then get deactivated? -
i have set of jbuttons, each of opens separate youtube video webpage. when first running program, can click on 1 button , video page. when try video page button click, doesn't work - in fact, buttons deactivated. case whether or not close video webpage.
how can keep buttons activated? in advance.
here's code reference. button links , tags populated text file.
//import statements public class videorecord extends jframe { private file videorecordfile; public videorecord() throws filenotfoundexception { setvisible(true); setdefaultcloseoperation(exit_on_close); setlayout(new gridlayout(2,2)); setsize(new dimension(500, 500)); videorecordfile = new file("videorecord.txt"); getbuttons(); pack(); } public void getbuttons() throws filenotfoundexception { scanner input = new scanner(videorecordfile); while (input.hasnextline()) { scanner lineinput = new scanner(input.nextline()); while (lineinput.hasnext()) { final string urlstring = lineinput.next(); string buttontext = lineinput.next(); jbutton btn = new jbutton(buttontext); add(btn); btn.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { try { url videourl = new url(urlstring); urlconnection videoconnection = videourl.openconnection(); videoconnection.connect(); openwebpage(videourl); } catch (malformedurlexception mue) {} catch (ioexception ioe) {} setenabled(false); } }); } } } public static void openwebpage(uri uri) { desktop desktop = desktop.isdesktopsupported() ? desktop.getdesktop() : null; if (desktop != null && desktop.issupported(desktop.action.browse)) { try { desktop.browse(uri); } catch (exception e) { e.printstacktrace(); } } } public static void openwebpage(url url) { try { openwebpage(url.touri()); } catch (urisyntaxexception e) { e.printstacktrace(); } } public static void main(string[] args) throws filenotfoundexception { videorecord vr = new videorecord(); }
}
take second @ code...
btn.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { try { url videourl = new url(urlstring); urlconnection videoconnection = videourl.openconnection(); videoconnection.connect(); openwebpage(videourl); } catch (malformedurlexception mue) {} catch (ioexception ioe) {} setenabled(false); } });
when click button call setenabled(false);
...
this has disable frame, not button clicked...
- try using
((jbutton)e.getsource()).setenabled(false)
instead - don't throw away
exception
s blindly, provide important , useful information can solve problems
Comments
Post a Comment