Reputation: 11
I have an "a" element on an html page with an href for a different website. Currently, when I click on the a element on the page displayed in the JEditorPane, the site the JEditorpane doesn't change the site. It won't redirect to the href's location. How would I fix this while keeping the redirected page displayed in the JEditorPane? Thank you!
The site's code in question is:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<a href="https://reddit.com"><img src="cat.jpeg" width="300" height="300" /></a>
</body>
</html>
The JFrame and JEditorPane's code is:
public class Gui
{
JFrame frame = new JFrame();
JEditorPane htmlContent = new JEditorPane();
public void loadScreen() throws MalformedURLException, IOException
{
frame.setUndecorated(true);
frame.setOpacity(1.0F);
htmlContent.setEditable(false);
htmlContent.setPage(new URL("RANDOM SITE"));
frame.add(new JScrollPane(htmlContent));
frame.setVisible(true);
}
}
Upvotes: 0
Views: 35
Reputation: 184
If you want the links displayed in a JEditorPane
to load a new page in the pane on mouse click, you need to add a listener, something like this:
htmlContent.addHyperlinkListener(e->{
if ( e.getEventType () == HyperlinkEvent.EventType.ACTIVATED ) {
URL url = e.getURL ();
// fetch the new page from the URL and load it into the JEditorPane
}
});
Upvotes: 0