I think you have need to the listener, this is an my example app
/*
* This code is under license Creative Commons Attribution-ShareAlike 1.0
* <a href="https://creativecommons.org/licenses/by-sa/1.0/legalcode"></a>
*/
package javaapplication5;
import java.awt.Dimension;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
public class DemoAppWebSwing extends JFrame {
private static final DemoAppWebSwing SINGLETON = new DemoAppWebSwing();
public void init() {
JEditorPane loginPane = new JEditorPane();
loginPane.setEditable(false);
try {
loginPane.setPage("https://github.com/vincenzopalazzo");
loginPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
JEditorPane pane = (JEditorPane) e.getSource();
if (e instanceof HTMLFrameHyperlinkEvent) {
HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
HTMLDocument doc = (HTMLDocument) pane.getDocument();
doc.processHTMLFrameHyperlinkEvent(evt);
} else {
try {
pane.setPage(e.getURL());
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
});
} catch (IOException e) {
loginPane.setContentType("text/html");
loginPane.setText("<html>Could not load</html>");
}
JScrollPane scrollPane = new JScrollPane(loginPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(scrollPane);
this.setPreferredSize(new Dimension(800, 600));
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SINGLETON.init();
}
});
}
}
With the HyperlinkListener you can get another page when you click on link
This is my Listener implementation:
loginPane.setPage("https://jsp-password-checking-unibas.herokuapp.com");
loginPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
JEditorPane pane = (JEditorPane) e.getSource();
if (e instanceof HTMLFrameHyperlinkEvent) {
HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
HTMLDocument doc = (HTMLDocument) pane.getDocument();
doc.processHTMLFrameHyperlinkEvent(evt);
} else {
try {
pane.setPage(e.getURL());
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
})
While discussing with another user in the comment section of this answer we came to the conclusion that the JEditorPane
has certain limitations. However I looked for another solution to import a WebPage into a Java based UI. You can do this, with the use of JavaFX
.
Check the link to the documentaion from oracle and/or this post for reference.
This is a basic example with JavaFX
which basically does the same as the previously shown example using Swing
.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class LoginScreen extends Application {
@Override
public void start(final Stage stage) {
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load("https://jsp-password-checking-unibas.herokuapp.com");
Scene scene = new Scene(browser, 800, 600);
stage.setTitle("Login Example");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}