Johanna
Johanna

Reputation: 27628

Why this code doesn't show the HTML file?

I wrote this code for showing the HTML file ,which I have chosen it from my computer!and when I choose the HTML file in my computer like FAQ.html this error messages will be shown:

java.net.MalformedURLException: no protocol: FAQ.html
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at javax.swing.JEditorPane.setPage(Unknown Source)
at org.bihe.com1112.FileViewer.actionPerformed(FileViewer.java:86)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)





public class FileViewer extends JPanel implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;

JFileChooser chooser;

FileNameExtensionFilter filter = null;

JTextField text;

JButton button;

FileInputStream in;

JEditorPane pane;

public FileViewer(JEditorPane pane) {
    this.pane = pane;
    setLayout(new FlowLayout(FlowLayout.RIGHT));
    text = new JTextField("file...", 31);
    text.setColumns(45);
    text.revalidate();
    text.setEditable(true);

    button = new JButton("Browse");
    add(text);
    add(button);
    filter = new FileNameExtensionFilter("html", "html");
    chooser = new JFileChooser();
    chooser.addChoosableFileFilter(filter);

    button.addActionListener(this);

}

public void paintComponent(Graphics g) {
    super.paintComponents(g);
    Graphics2D graphic = (Graphics2D) g;
    graphic.drawString("HTML File:", 10, 20);

}

public void actionPerformed(ActionEvent event) {
    int returnVal = 0;
    if (event.getSource() == button) {
        returnVal = chooser.showOpenDialog(FileViewer.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            text.setText(file.getName());
            if (file != null) {
                try {
                    pane.setPage(file.getName());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            } else

                System.err.println("Couldn't find this HTML file:"
                        + file.getName());

        } else
            System.exit(0);
    }

  }
 }

Upvotes: 1

Views: 838

Answers (2)

Andrew Moore
Andrew Moore

Reputation: 95334

You need to specify the full path of the file using the file protocol as such:

file:///c:/somefolder/FAQ.html

You can use file.toURI() to get an URI, and then uri.toURL() to get an URL:

// file.toURL() has been deprecated, use file.toURI().toURL() instead
pane.setPage(file.toURI().toURL());

Upvotes: 3

David
David

Reputation: 2174

Maybe try pane.setPage(file.toURL()) instead of pane.setPage(file.getName()), because setPage expects a url, judging from a quick search for others trying to get it to work, having not tried it myself.

Upvotes: 0

Related Questions