JMenu help for opening an HTML page on a local machine

It’s hard for me to find a tutorial that allows JMenuItem to flag ("Help Content") to open an html page that displays help content. The html page will be stored on the local machine, for example C: \ help. I created the following action listener, which, when clicked on, displays a string. This is for test purposes only to show that my ActionListener is working.

   private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            JOptionPane.showMessageDialog(null, "Help File Popup");
        }

So, how would I replace:

JOptionPane.showMessageDialog(null, "Help File Popup");

Thus, when jMenuItem3 is clicked, the html page is loaded in the browser by default. Thank you very much in advance!

+3
source share
4 answers

Pretty easy when youre in Java 6:

take a look at this method

Desktop.browse("http://www.google.de/);

http://download.oracle.com/javase/6/docs/api/java/awt/Desktop.html#browse (java.net. URI),

+3

html, jdic. swing, JEditorPane, html, , .

( swing):

JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
java.net.URL helpURL = TextSamplerDemo.class.getResource(
                            "TextSamplerDemoHelp.html");
if (helpURL != null) {
try {
    editorPane.setPage(helpURL);
} catch (IOException e) {
    System.err.println("Attempted to read a bad URL: " + helpURL);
}
} else {
System.err.println("Couldn't find file: TextSamplerDemoHelp.html");
}

//Put the editor pane in a scroll pane.
JScrollPane editorScrollPane = new JScrollPane(editorPane);
editorScrollPane.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorScrollPane.setPreferredSize(new Dimension(250, 145));
editorScrollPane.setMinimumSize(new Dimension(10, 10));

jdic, - :

import org.jdesktop.jdic.browser.*;
Desktop.browse(new URL(inputUrl)); // open URL in   default browser
+1

java.awt.Desktop.

+1

, , ! :

public void actionPerformed(ActionEvent a) {
        // TODO add your handling code here:
        try{ 
            String url = "http://www.google.com"; 
            java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); 
          } 
          catch (java.io.IOException e) { 
              System.out.println(e.getMessage()); 
          } 
    }

, , - , .

+1

Source: https://habr.com/ru/post/1768612/


All Articles