简体   繁体   中英

use JavaFx Application in swing project

I have to browse Html files in java windows application software . For it i am using JEditorPane control but its not supporting some tags of HTML and formatting is disturbing in JEditorPane. I have searched on net and net is suggesting me to use JavaFXApplication control for it . In C# there is a control WebBrowser that displays the html file in the same format easily . Is it possible in java too to display the html files with all supporting tags . Can you suggest me the control or something wrong in my code.I am using the following code.

 try
 { 
  File htmlfile= new File("path of the html file");
  JEditorPane htmlPane= new JEditorPane();
  htmlPane.setEditable(false);
  htmlPane.setContentType("text/html");
  htmlPane.setPage(htmlfile.toURI().toURL());
  JScrollPane jsp= new JScrollPane(htmlPane);
  add(jsp);
  }

  catch(Exception ex)
   {


    }

If I understand your question correctly, you are trying to view the HTML SOURCE and not trying to implement a web browser.

If that's the case you can use JavaFX's HTML editor assuming you are using Java 6 with JavaFX OR Java 7 which includes JavaFX

Here's a short sample of using the javafx package using the JFXPanel and HTMLEditor :

public class JavaFXDemo {

    private static void initAndShowGUI() {
        JFrame frame = new JFrame("HTML Editor");
        final JFXPanel fxPanel = new JFXPanel();
        frame.add(fxPanel);
        frame.setSize(600, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                final HTMLEditor htmlEditor = new HTMLEditor();
                Scene scene = new Scene(htmlEditor);
                fxPanel.setScene(scene);
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initAndShowGUI();
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM