Custom FXML component (w/ controller) doesn't appear in SceneBuilder's “Import jar” dialog

吃可爱长大的小学妹 提交于 2019-12-02 06:15:08

I solved this using the information here:

http://www.cuchazinteractive.com/blog/custom-javafx-controls-and-scene-builder

To summarise:

  1. Create an FXML file and a Java class that extends the root node (I gave them the same name)
  2. Change the FXML file to have fx:root as the base node. (will not work without this)
  3. Remove the fx:controller attribute (will not work without this)
  4. Compile and add the jar to scene builder

However, I have found that if your custom control depends on controls from other libraries, it will fail even if the other library is loaded in scene builder.

Below is a minimum working example

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>


<fx:root id="AnchorPane" prefHeight="73.0" prefWidth="112.0" type="AnchorPane" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">
   <children>
      <Button layoutX="25.0" layoutY="28.0" mnemonicParsing="false" text="Button" />
   </children>
</fx:root>

Java:

package my.amazing.controls;

import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;

public class TestControl extends AnchorPane {


    public TestControl() throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("TestControl.fxml"));
        loader.setRoot(this);
        loader.setController(this);
        loader.load();
    }

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