javahtmljavafx

Converting a view from JavaFX to HTML


I wrote a Java desktop application using JavaFX with xml files. Is it possible to "port" it leaving all the code unchanged, just using html files instead (for the web application version)? I would like to keep @FXML tags as they are, for example.


Solution

  • No.

    HTML and FXML (which I assume was what you meant by 'xml' because you need fxml to run a JavaFX application) are similar only on a surface level.

    HTML - HyperText Markup Language
    FXML - FX Markup Language

    They both use 'tags' to 'markup' content, letting whatever program that reads it know how to display the given information, but they are not the same.

    A browser displaying a webpage expects certain HTML tags to be there, like <html> or <head> or <body> and does not understand other tags, such as FXML's <Pane> tag, or <ComboBox> tag. The browser would not know how to display this AT ALL, just as a JavaFX application would not know how to display a <br> tag.


    Now, view aside, could you use the same back-end code for both applications? Yes. You would need to put all the necessary code somewhere they both could access it, so, in your JavaFX application your method might look like this:

    @FXML
    public void doSomething(ActionEvent e){
        MyJavaServer.doSomething();
    }
    

    and in your web based application, you could use Spring Framework or something to produce code that calls the same MyJavaServer.doSomething().