Created
November 18, 2014 09:06
-
-
Save masnagam/ec6fd335b75bbe87aea7 to your computer and use it in GitHub Desktop.
simple web browser using java.swing.JEditorPane
This file contains hidden or 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
Looks good enough
