HTML in Swing boonritfeu ac th JEditor Pane
HTML in Swing boonrit@feu. ac. th
JEditor. Pane Ø ������ HTML Ø Contructor l l public JEditor. Pane( ) public JEditor. Pane(URL initial. Page) throws IOException public JEditor. Pane(String url) throws IOException public JEditor. Pane(String mime. Type, String text)
JEditor. Pane Ø ������� set. Page() or set. Text( ) public void set. Page(URL page) throws IOException Ø public void set. Page(String url) throws IOException Ø Ø public void set. Text(String html)
JEditor. Pane Ø Ø import javax. swing. text. *; ; import javax. swing. *; ; import java. io. *; ; import java. awt. *; ; Ø public class jweb { public static void main(String[] args) { Ø Ø Ø JEditor. Pane jep = new JEditor. Pane( ); ); jep. set. Editable(false); Ø Ø Ø Ø try { jep. http: //www www. . nectec. jep. set. Page("http nectec. or. th"); ; } catch (IOException ex) { jep. text//html") jep. set. Content. Type("text html"); ; jep. <html>Could not load http: // web page </ jep. set. Text("<html>Could http: //web </html>"); ; } JScroll. Pane scroll. Pane = new JScroll. Pane(jep); JFrame f = new JFrame(" jweb") "); ; JFrame("jweb f. f. set. Default. Close. Operation(Window. Constants. DISPOSE_ON_CLOSE); f. f. set. Content. Pane(scroll. Pane); f. f. set. Size(512, 342); f. f. show( ); ); } }
JEditor. Pane Ø ����
JEditor. Pane Ø ��� ���� URL JFrame f = new JFrame("O'Reilly & Associates"); try { URL u = new URL("http: //www. oreilly. com"); JEditor. Pane jep = new JEditor. Pane(u); jep. set. Editable(false); JScroll. Pane scroll. Pane = new JScroll. Pane(jep); f. set. Content. Pane(scroll. Pane); } catch (IOException ex) { f. get. Content. Pane( ). add( new Label("Could not load http: //www. oreilly. com")); } f. set. Size(512, 342); f. show( );
Handling Hyperlinks Ø Hyperlink. Listener objects ����� Link �� JEditor. Pane Ø javax. swing. event. Hyperlink. Listener interface �� method hyperlink. Update( ) public void hyperlink. Update(Hyperlink. Event evt)
Handling Hyperlinks Ø Ø import javax. swing. *; import javax. swing. event. *; Ø Ø Ø Ø public class Link. Follower implements Hyperlink. Listener { private JEditor. Pane pane; public Link. Follower(JEditor. Pane pane) { this. pane = pane; } public void hyperlink. Update(Hyperlink. Event evt) { if (evt. get. Event. Type( ) == Hyperlink. Event. Type. ACTIVATED) { try { pane. set. Page(evt. get. URL( )); } catch (Exception ex) { pane. set. Text("<html>Could not load " + evt. get. URL( ) + "</html>"); } }
Simple. Web. Browser Ø Ø Ø import javax. swing. text. *; import javax. swing. *; import java. net. *; import java. io. *; import java. awt. *; Ø Ø Ø Ø Ø public class Simple. Web. Browser { public static void main(String[] args) { // get the first URL String initial. Page = "http: //www. thairath. co. th"; if (args. length > 0) initial. Page = args[0]; // set up the editor pane JEditor. Pane jep = new JEditor. Pane( ); jep. set. Editable(false); jep. add. Hyperlink. Listener(new Link. Follower(jep)); try { jep. set. Page(initial. Page); } catch (IOException ex) { System. err. println("Usage: java Simple. Web. Browser url"); System. err. println(ex); System. exit(-1); }
Simple. Web. Browser Ø Ø Ø Ø Ø // set up the window JScroll. Pane scroll. Pane = new JScroll. Pane(jep); JFrame f = new JFrame("Simple Web Browser"); f. set. Default. Close. Operation(Window. Constants. DISPOSE_ON_CLOSE); f. set. Content. Pane(scroll. Pane); f. set. Size(512, 342); Event. Queue. invoke. Later(new Frame. Shower(f)); } // Helps avoid a really obscure deadlock condition. // See http: //java. sun. com/developer/JDCTech. Tips/2003/tt 1208. html#1 private static class Frame. Shower implements Runnable { private final Frame frame; Frame. Shower(Frame frame) { this. frame = frame; } public void run( ) { frame. set. Visible(true); } } }
- Slides: 10