-
-
Save iamabs2001/ad6617599781b9914ac838bf6ea8d737 to your computer and use it in GitHub Desktop.
simple web browser using java.swing.JEditorPane
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import javax.swing.event.*; | |
import javax.swing.text.html.*; | |
public class SwingHTMLBrowser extends JFrame implements ActionListener, HyperlinkListener { | |
private JTextField addressBar; | |
private JEditorPane pane; | |
SwingHTMLBrowser() { | |
super("Swing HTML Browser"); | |
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); | |
addressBar = new JTextField(); | |
addressBar.addActionListener(this); | |
pane = new JEditorPane(); | |
pane.setEditable(false); | |
pane.addHyperlinkListener(this); | |
add(addressBar, BorderLayout.NORTH); | |
add(new JScrollPane(pane)); | |
setSize(new Dimension(400, 400)); | |
} | |
public void actionPerformed(ActionEvent evt) { | |
String url = addressBar.getText(); | |
try { | |
pane.setPage(url); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} | |
} | |
public void hyperlinkUpdate(HyperlinkEvent evt) { | |
if (evt.getEventType() != HyperlinkEvent.EventType.ACTIVATED) { | |
return; | |
} | |
JEditorPane srcPane = (JEditorPane)evt.getSource(); | |
if (evt instanceof HTMLFrameHyperlinkEvent) { | |
HTMLDocument doc = (HTMLDocument)pane.getDocument(); | |
doc.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent)evt); | |
} else { | |
String url = evt.getURL().toString(); | |
addressBar.setText(url); | |
try { | |
pane.setPage(url); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} | |
} | |
} | |
public static void main(String args[]) { | |
SwingHTMLBrowser browser = new SwingHTMLBrowser(); | |
browser.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment