2013-02-24 98 views
1

任何人都可以告訴我如何將一個html文檔加載到JEditorPane中。我嘗試了以下似乎不起作用:將HTML文件加載到JEditorPane中

try{ 
    URL h = new URL("file://test.html"); 
    pane.setPage(h); 
} catch (Exception e) {} 
+3

那空catch語句太危險了!不要這樣做。打印堆棧跟蹤或至少在某處登錄。這可能是你的問題的原因(你不知道失敗後發生了什麼)。 – Sorceror 2013-02-24 14:32:24

+0

我現在已經把一些東西放在catch語句中,這樣我可以看到失敗時會發生什麼,但是html文件仍然沒有加載。 – user2104612 2013-02-24 14:41:25

+0

除了@Sorceror的忠告之外,我還是建議添加以下幾行:File f = new File(「test.html」);的System.out.println(f.exists()); URL h = f.toURI()。toURL(); System.out.println(h); ..' – 2013-02-24 14:41:39

回答

0

你可以按照上搖擺的WebView教程太陽的教程... :) http://docs.oracle.com/javafx/2/swing/SimpleSwingBrowser.java.htm

我粘貼他們的演示的源代碼。在Java中jeditor窗格

import javafx.application.Platform; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.embed.swing.JFXPanel; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.web.WebEngine; 
import javafx.scene.web.WebEvent; 
import javafx.scene.web.WebView; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.net.MalformedURLException; 
import java.net.URL; 

import static javafx.concurrent.Worker.State.FAILED; 

public class SimpleSwingBrowser implements Runnable { 
    private JFXPanel jfxPanel; 
    private WebEngine engine; 

    private JFrame frame = new JFrame(); 
    private JPanel panel = new JPanel(new BorderLayout()); 
    private JLabel lblStatus = new JLabel(); 

    private JButton btnGo = new JButton("Go"); 
    private JTextField txtURL = new JTextField(); 
    private JProgressBar progressBar = new JProgressBar(); 

    private void initComponents() { 
     jfxPanel = new JFXPanel(); 

     createScene(); 

     ActionListener al = new ActionListener() { 
      @Override public void actionPerformed(ActionEvent e) { 
       loadURL(txtURL.getText()); 
      } 
     }; 

     btnGo.addActionListener(al); 
     txtURL.addActionListener(al); 

     progressBar.setPreferredSize(new Dimension(150, 18)); 
     progressBar.setStringPainted(true); 

     JPanel topBar = new JPanel(new BorderLayout(5, 0)); 
     topBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5)); 
     topBar.add(txtURL, BorderLayout.CENTER); 
     topBar.add(btnGo, BorderLayout.EAST); 


     JPanel statusBar = new JPanel(new BorderLayout(5, 0)); 
     statusBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5)); 
     statusBar.add(lblStatus, BorderLayout.CENTER); 
     statusBar.add(progressBar, BorderLayout.EAST); 

     panel.add(topBar, BorderLayout.NORTH); 
     panel.add(jfxPanel, BorderLayout.CENTER); 
     panel.add(statusBar, BorderLayout.SOUTH); 

     frame.getContentPane().add(panel); 
    } 

    private void createScene() { 

     Platform.runLater(new Runnable() { 
      @Override public void run() { 

       WebView view = new WebView(); 
       engine = view.getEngine(); 

       engine.titleProperty().addListener(new ChangeListener<String>() { 
        @Override 
        public void changed(ObservableValue<? extends String> observable, String oldValue, final String newValue) { 
         SwingUtilities.invokeLater(new Runnable() { 
          @Override public void run() { 
           frame.setTitle(newValue); 
          } 
         }); 
        } 
       }); 

       engine.setOnStatusChanged(new EventHandler<WebEvent<String>>() { 
        @Override public void handle(final WebEvent<String> event) { 
         SwingUtilities.invokeLater(new Runnable() { 
          @Override public void run() { 
           lblStatus.setText(event.getData()); 
          } 
         }); 
        } 
       }); 

       engine.locationProperty().addListener(new ChangeListener<String>() { 
        @Override 
        public void changed(ObservableValue<? extends String> ov, String oldValue, final String newValue) { 
         SwingUtilities.invokeLater(new Runnable() { 
          @Override public void run() { 
           txtURL.setText(newValue); 
          } 
         }); 
        } 
       }); 

       engine.getLoadWorker().workDoneProperty().addListener(new ChangeListener<Number>() { 
        @Override 
        public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, final Number newValue) { 
         SwingUtilities.invokeLater(new Runnable() { 
          @Override public void run() { 
           progressBar.setValue(newValue.intValue()); 
          } 
         }); 
        } 
       }); 

       engine.getLoadWorker() 
         .exceptionProperty() 
         .addListener(new ChangeListener<Throwable>() { 

          public void changed(ObservableValue<? extends Throwable> o, Throwable old, final Throwable value) { 
           if (engine.getLoadWorker().getState() == FAILED) { 
            SwingUtilities.invokeLater(new Runnable() { 
             @Override public void run() { 
              JOptionPane.showMessageDialog(
                panel, 
                (value != null) ? 
                engine.getLocation() + "\n" + value.getMessage() : 
                engine.getLocation() + "\nUnexpected error.", 
                "Loading error...", 
                JOptionPane.ERROR_MESSAGE); 
             } 
            }); 
           } 
          } 
         }); 

       jfxPanel.setScene(new Scene(view)); 
      } 
     }); 
    } 

    public void loadURL(final String url) { 
     Platform.runLater(new Runnable() { 
      @Override public void run() { 
       String tmp = toURL(url); 

       if (tmp == null) { 
        tmp = toURL("http://" + url); 
       } 

       engine.load(tmp); 
      } 
     }); 
    } 

    private static String toURL(String str) { 
     try { 
      return new URL(str).toExternalForm(); 
     } catch (MalformedURLException exception) { 
       return null; 
     } 
    } 

    @Override public void run() { 

     frame.setPreferredSize(new Dimension(1024, 600)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     initComponents(); 

     loadURL("http://oracle.com"); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new SimpleSwingBrowser()); 
    } 
} 
0

How to setPage() a JEditorPane with a localfile which is outside of the .jar file?

// get location of the code source 
URL url = yourpackage.Main.class.getProtectionDomain().getCodeSource().getLocation(); 

    try { 
     // extract directory from code source url 
     String root = (new File(url.toURI())).getParentFile().getPath(); 
     File doc = new File(root, "test.htm"); 
     // create htm file contents for testing 
     FileWriter writer = new FileWriter(doc); 
     writer.write("<h1>Test</h1>"); 
     writer.close(); 
     // open it in the editor 
     editor.setPage(doc.toURI().toURL()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
2

打開HTML文件

 JeditorPane editorpane= new JEditorPane(); 
     editorScrollPane = new JScrollPane(editorpane); 
     editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     File file = new File("F://test.htm"); 
     editorpane.setPage(file.toURI().toURL()); 
     editorpane.setEditable(true); 

希望這會幫助你, 這裏這是工作

+0

爲什麼我們不能直接添加路徑? – 2016-06-30 21:20:05

相關問題