Save JavaFX scene into FXML file

孤街浪徒 提交于 2020-01-21 12:53:18

问题


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, how I can save it into FXML file?


回答1:


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.




回答2:


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! :-)




回答3:


.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.



来源:https://stackoverflow.com/questions/36284904/save-javafx-scene-into-fxml-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!