User Control of ClassNotFoundException in Scene Builder

I created a new control by expanding an existing one and would like to use this new control in JavaFX scripts. I would like to be able to edit my scenes using Scene Builder, but after adding a new control to an FXML file, I encounter it ClassNotFoundExceptionwhen opening Scene Builder.

For example, here is a class I that extends TextField:

RegexLimitingTextField.java

public class RegexLimitingTextField extends TextField {

    private String regexLimiter = ".*";

    public void setRegexLimiter(String regex) {
        this.regexLimiter = regex;
    }

    @Override
    public void replaceText(int start, int end, String text) {
        if (text.matches(regexLimiter))
            super.replaceText(start, end, text);
    }

    @Override
    public void replaceSelection(String replacement) {
        if (replacement.matches(regexLimiter))
            super.replaceSelection(replacement);
    }
}

After adding this control to the FXML file ...

sample.fxml

<?import javafx.scene.layout.GridPane?>
<?import sample.RegexLimitingTextField?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <RegexLimitingTextField fx:id="textField" text="Test" />
</GridPane>

... I get this error when loading Scene Builder 2.0:

Caused by: java.lang.ClassNotFoundException: sample.RegexLimitingTextField
    at java.lang.ClassLoader.findClass(ClassLoader.java:530)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2920)
    at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2909)
    at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2850)
    ... 23 more

Why can't Scene Builder find my new control? What do I need to do to find and use my new control?

If necessary, create other files:

Main.java

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setScene(new Scene(root, 200, 200));
        primaryStage.show();
    }
}

Controller.java

public class Controller implements Initializable {
    public RegexLimitingTextField textField;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        textField.setRegexLimiter("\\w*");
    }
}
+4
2

- SceneBuilder, , ClassLoader .

try {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomComponent.fxml"));
    loader.setRoot(this);
    loader.setController(this);
    loader.setClassLoader(getClass().getClassLoader());
    loader.load();
} catch (IOException e ){
    throw new RuntimeException(e);
}

, javaguy.

+2

... , , . , .

, , , :

  • ClassLoader, , .
  • fx: root .fxml

, , ClassNotFoundException Scene Builder, (.fxml java-) , . , . ... , . , , , , .

+2

Source: https://habr.com/ru/post/1621708/


All Articles