Save JavaFX scene into FXML file

后端 未结 3 1134
余生分开走
余生分开走 2021-01-21 02:39

Anyone knows how to save a JavaFX scene into FXML file that can be loaded by the JavaFX FXMLLoader?

The SceneBuilder allows to do it, but if I build the scene manually,

相关标签:
3条回答
  • 2021-01-21 03:03

    .fxml is just file extension,you can create it with any text file editor , i recommend you to use SceneBuilder either way, since you can create your UI and CTRL+C on root component , CTRL+V to notepad++ or other editor , and get source straight from there , edit to your liking.

    To make it short, FXML is an XML based declaration format for JavaFX. JavaFX provides an FXML loader which will parse FXML files and from that construct a graph of Java object. It may sound complex when stated like that but it is actually quite simple. Here is an example of FXML file, which instantiate a StackPane and puts a Button inside it:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.*?>
    <?import java.util.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.paint.*?>
    
    <StackPane prefHeight="150.0" prefWidth="200.0" xmlns:fx="http://javafx.com/fxml">
      <children>
        <Button mnemonicParsing="false" text="Button" />
      </children>
    </StackPane>
    

    You want to specify controller as well with fx:controller="" Or set controller in your code, whatever fits your needs.

    if your question is more towards what pdem wrote , i suggest you to take a look at scenic View , you can get a lot of infomation from running application and recreate it based of it.If that aint sufficient , there is not much you can do.

    0 讨论(0)
  • 2021-01-21 03:13

    There is no such library, AFAIK, and I'm not sure if it's possible to create a generic one. It might be possible for simple cases, but even then it's a lot of work, I guess.

    Here is the FXML format explained, if you want to give it a try: http://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html

    You could traverse your scene graph/ node and generate an FXML file.

    But why do you need the FXML format? It might be easier just to rewrite the layout in FXML instead of writing a library like this.

    If you manage to write such a library - let us know! :-)

    0 讨论(0)
  • 2021-01-21 03:18

    If you mean build a fxml file from a running screen built in Java, the short answer is that you can't.

    The fxmlLoader is designed to work only to load files, it references the class XMLInputFactory but not the XMLOutputFactory.

    If you would like to do it by yourself, it is not only rewrite the classes read by the FXMLLoader, because there is a lot of reflection (java.lang.reflect) in that class.

    So the long answer could be: you can do it by yourself using a lot of reflection and writing dynamic tags from class names, but there will be no guarantee that your fxml gives the expected results.

    0 讨论(0)
提交回复
热议问题