<< Chapter < Page | Chapter >> Page > |
Listing 12 - The program named Java4655b.
/*File Java4655b.java
Copyright 2014, R.G.BaldwinRev 01/05/14
This is a simple web browser that can follow links.Uses website string to create JEditPane object.
Ignores ENTERED and EXITED hyperlink events. UsesACTIVATED events to follow links.
Ignores links in HTML frames.**********************************************************/
import javax.swing.*;import javax.swing.event.*;
import javax.swing.text.html.*;import java.net.*;
import java.awt.*;class Java4655b{
public static void main(String[]args){
new Java4655b().runner("http://www.dickbaldwin.com");}//end main
//-----------------------------------------------------//void runner(String websiteString){try{
//Pass the website string to the constructornew Java4655bHtmlHandler(websiteString);
}catch(Exception e){e.printStackTrace();
}//end catch}//end runner
}//end class Java4655b//=======================================================//
//This version of the website handler requires the website// to be specified as a String instead of a URL.
class Java4655bHtmlHandler extends JFrameimplements HyperlinkListener{
JEditorPane html;//Constructor
public Java4655bHtmlHandler(String websiteString) {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Copyright 2014, R.G.Baldwin");try{
if(websiteString != null) {html = new JEditorPane(websiteString);
html.setEditable(false);html.addHyperlinkListener(this);
this.getContentPane().add(html,BorderLayout.CENTER);this.setSize(669,669);
this.setVisible(true);}//end if
}catch(Exception e){e.printStackTrace();
}//end catch}//end constructor
//-----------------------------------------------------//public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() ==HyperlinkEvent.EventType.ACTIVATED){
//Ignore ENTERED and EXITED events and process only// ACTIVATED events.
if (e instanceof HTMLFrameHyperlinkEvent) {//Ignore events in HTML frames
System.out.println("HTML Frame events are ignored");
} else {try {
//Display page defined by the HyperlinkEvent.html.setPage(e.getURL());
} catch (Exception ex) {ex.printStackTrace();
}//end catch}//end else
}//end if}//end hyperlinkUpdate method
//------------------------------------------------------//}//end class Java4655bHtmlHandler
Listing 13 - The program named Java4655a.
/*File Java4655a.java
Copyright 2014, R.G.BaldwinRev 01/05/14
This is a simple web browser that can followlinks.
It ignores links in HTML frames.**********************************************************/
import javax.swing.*;import javax.swing.event.*;
import javax.swing.text.html.*;import java.net.*;
import java.awt.*;class Java4655a{
public static void main(String[]args){
new Java4655a().runner("http://www.dickbaldwin.com");}//end main
//-----------------------------------------------------//void runner(String websiteString){try{
new Java4655aHtmlHandler(websiteString);}catch(Exception e){
e.printStackTrace();}//end catch
}//end runner}//end class Java4655a
//=======================================================//class Java4655aHtmlHandler extends JFrame
implements HyperlinkListener{JEditorPane html;
//Constructorpublic Java4655aHtmlHandler(String websiteString) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setTitle("Copyright 2014, R.G.Baldwin");
try{if(websiteString != null) {
html = new JEditorPane(websiteString);html.setEditable(false);
html.addHyperlinkListener(this);JScrollPane scroller = new JScrollPane();JViewport vp = scroller.getViewport();
vp.add(html);this.getContentPane().add(scroller,BorderLayout.CENTER);
this.setSize(669,669);this.setVisible(true);
}//end if}catch(Exception e){
e.printStackTrace();}//end catch
}//end constructor//-----------------------------------------------------//
public void hyperlinkUpdate(HyperlinkEvent e) {if (e.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED){//Ignore ENTERED and EXITED events and process only
// ACTIVATED events.if (e instanceof HTMLFrameHyperlinkEvent) {
//Ignore events in HTML framesSystem.out.println(
"HTML Frame events are ignored");} else {
try {//Display page defined by the HyperlinkEvent.
html.setPage(e.getURL());} catch (Exception ex) {
ex.printStackTrace();}//end catch
}//end else}//end if
}//end hyperlinkUpdate method//------------------------------------------------------//
}//end class Java4655aHtmlHandler
-end-
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?