java - Changing html page inside JEditorPane on button click -
any idea how change html page viewed inside jeditorpane on button click events in jframe, sorry new java basic explanation appreciated.(need chage content page 1 content here written)
package test1; import java.awt.borderlayout; import java.awt.dimension; import java.awt.flowlayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.file; import java.io.ioexception; import java.net.malformedurlexception; import java.net.uri; import java.net.url; import javax.swing.jbutton; import javax.swing.jeditorpane; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.event.hyperlinkevent; import javax.swing.event.hyperlinklistener; public class check1 extends jframe implements actionlistener{ jeditorpane jep; jscrollpane scroll; jpanel p,p1; jbutton b,b1,b2,b3; dimension d; string url; public check1() { d = new dimension(500, 1500); b = new jbutton("click me"); b.addactionlistener(this); b1 = new jbutton("click me"); b2 = new jbutton("click me"); //b2.setbounds(0, 100, 70, 40); b3 = new jbutton("click me"); //b3.setbounds(0, 150, 70, 40); p = new jpanel(); p.setlayout(new flowlayout()); p.add(b); p1 = new jpanel(); p1.setlayout(new gridlayout(4, 2, 1, 1)); p1.add(b1,0,0); p1.add(b2,0,1); p1.add(b3,2,0); p1.setvisible(false); try { jep = new jeditorpane("file:///c:/users/chinmay/workspace/project1/src/page1.html"); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } jep.seteditable(true); scroll = new jscrollpane(jep); setlayout(new borderlayout()); getcontentpane().add(p,borderlayout.north); getcontentpane().add(p1,borderlayout.west); getcontentpane().add(scroll,borderlayout.center); setsize(1000, 800); setvisible(true); setlocationrelativeto(null); } public static void main(string args[]) { new check1().setvisible(true); } @override public void actionperformed(actionevent e) { if(e.getsource().equals(b)) { p1.setvisible(true); } else if(e.getsource().equals(b1)) { url ="file:///c:/users/chinmay/workspace/project1/src/page.html"; try { jep.setpage(url); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } else if(e.getsource().equals(b2)) { url ="file:///c:/users/chinmay/workspace/project1/src/page1.html"; try { jep.setpage(url); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } else if(e.getsource().equals(b3)) { url ="file:///c:/users/chinmay/workspace/project1/src/page2.html"; try { jep.setpage(url); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } } }
when things aren't working, first thing simplify code down simplest displays error. there lot of errors in code removed in process of simplifying it. start working code , change it, bit bit, until breaks again, prepare mcve of error , post here.
other tips before working example.
- don't have 4 buttons identical names! confusing when debugging, , confusing end user. if cannot think of more useful names them, use same or similar text name of attribute.
- the 'invisible / visible on button click' part not relevant problem, simplify code, remove part , have panel containing 3 buttons visible beginning.
- but having said that, necessary have 2 buttons in order see either work or fail.
- don't use magic numbers in layout constraints! doubt buttons ending intended them appear.
- application resources become embedded resources time of deployment, wise start accessing them if were, right now. embedded-resource must accessed url rather file. see info. page embedded resource how form url.
having said that, remaining problem summed in:
url = "file:///c:/users/chinmay/workspace/project1/src/page1.html";
the attribute not url
, string
. calling attribute url
not change that. when jep.setpage(url);
called, method presume string
represents file
path , deal accordingly (or fail accordingly). since string not represent valid file path, fail.
here working code using 2 buttons, , actual urls.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.ioexception; import java.net.url; public class check1 extends jframe implements actionlistener { jeditorpane jep; jscrollpane scroll; jpanel p, p1; jbutton b1, b2; string url; public check1() { setdefaultcloseoperation(jframe.dispose_on_close); b1 = new jbutton("button 1"); b1.addactionlistener(this); b2 = new jbutton("button 2"); b2.addactionlistener(this); p = new jpanel(); p.setlayout(new flowlayout()); p1 = new jpanel(); p1.setlayout(new gridlayout(4, 2, 1, 1)); p1.add(b1); p1.add(b2); try { jep = new jeditorpane( new url("http://docs.oracle.com/javase/8/docs/api/javax/swing/jframe.html#constructor.summary")); } catch (ioexception e) { e.printstacktrace(); } scroll = new jscrollpane(jep); setlayout(new borderlayout()); getcontentpane().add(p, borderlayout.north); getcontentpane().add(p1, borderlayout.west); getcontentpane().add(scroll, borderlayout.center); setsize(1000, 800); setvisible(true); setlocationrelativeto(null); } public static void main(string args[]) { new check1().setvisible(true); } @override public void actionperformed(actionevent e) { if (e.getsource().equals(b1)) { url = "http://docs.oracle.com/javase/8/docs/api/javax/swing/jbutton.html#constructor.summary"; } else if (e.getsource().equals(b2)) { url = "http://docs.oracle.com/javase/8/docs/api/javax/swing/japplet.html#constructor.summary"; } try { jep.setpage(new url(url)); } catch (ioexception e1) { e1.printstacktrace(); } } }
Comments
Post a Comment