benlopas
benlopas

Reputation: 1

How to open a html file from a help menu button

I am try to open a javadoc html file with my new application, however I can not get the javadoc file to open, I have a class name OpenUri, which when called is supposed to open the javadoc:

package gui;

import java.awt.Desktop;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import javax.swing.JFrame;

public class OpenUri extends JFrame {

public static void openWebpage(URI uri) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(uri);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public static void openWebpage(URL url) {
    try {
        openWebpage(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}
}

I am then calling and using this class from another class called Menu, where the help button has an action listener, etc. However when I run the code and press the help button, no javadoc appears, ie, it doesn't open the document, ie, nothing happens, no window, nothing ? The only way I can open it is manually, by clicking on it in eclipse, here is the specific code from the Menu class I am Using:

//Help
        JMenu helpMenu = new JMenu("Help");
        helpMenu.setMnemonic(KeyEvent.VK_H);
        helpMenu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                try {
                    URI uri = new URI("file:///C:/Users/howhowhows/workspace/OPTICS_DROP_MENU/doc/index.html");
                    OpenUri.openWebpage(uri);
                } catch (URISyntaxException e) {

                    e.printStackTrace();
                }
            }
        });

If anyone has any ideas as to what I am doing wrong, ie what I need to add/change, it would be greatly appreciated.

Upvotes: 0

Views: 444

Answers (1)

camickr
camickr

Reputation: 324197

Have you downloaded and tried the demo code from the Swing tutorial on How to Integrate With the Desktop Class.

When I used that code and pasted your URI into the text field no window is displayed and I get a "System cannot find the file" message as expected.

When I then enter a simple URI that I know exists: "c:/java/a.html" the browser opens as expected.

So I suggest you start with known working code and see if your URI works. If it does work then the problem is your code, so compare the working code to your code to see what the difference is. If it doesn't work then the problem is the URI.

If you still have problems then post a proper SSCCE that demonstrates the problem. Given that your OPenURI class extends JFrame for no reason we don't know what other strange things you might be doing in your code.

Upvotes: 2

Related Questions