diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/WebBrowser.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/WebBrowser.java new file mode 100644 index 0000000..c4d53ee --- /dev/null +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/WebBrowser.java @@ -0,0 +1,55 @@ +package net.knarcraft.minecraftserverlauncher.userinterface; + +import javax.swing.*; +import javax.swing.event.HyperlinkEvent; +import java.io.IOException; + +/** + * This class allows for displaying web pages + */ +public class WebBrowser { + + private static JFrame browserFrame; + private static JTextPane editorPane; + + private WebBrowser() {} + + /** + * Instantiates a new web browser + */ + private static void instantiate() { + editorPane = new JTextPane(); + editorPane.setEditable(false); + browserFrame = new JFrame("Web Browser"); + JScrollPane scrollPane = new JScrollPane(editorPane); + browserFrame.add(scrollPane); + + browserFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + browserFrame.setBounds(100, 100, 800, 600); + } + + /** + * Displays the web page with the given URL + * + * @param url
The URL to open
+ */ + public static void displayPage(String url) { + if (browserFrame == null) { + instantiate(); + } + browserFrame.setVisible(true); + try { + browserFrame.setTitle("Browsing: " + url); + editorPane.setPage(url); + editorPane.setContentType("text/html"); + editorPane.addHyperlinkListener(hyperlinkEvent -> { + if (hyperlinkEvent.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) { + displayPage(hyperlinkEvent.getURL().toString()); } + }); + } catch (IOException e) { + editorPane.setContentType("text/html"); + editorPane.setText("Could not load"); + } + } + +}