问题
So this post: Custom FXML component (w/ controller) doesn't appear in SceneBuilder's "Import jar" dialog
It says you cant import custom components which rely on third party components. But I cant believe this (this would be stupid) ... Im struggling now to create a custom component based on components from the Library JFoenix.
So i have this fxml and controller:
SelectableList.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXListView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<fx:root type="AnchorPane" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<VBox alignment="TOP_CENTER" prefHeight="743.2" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label fx:id="titleLabel" text="Title" textFill="#000000de">
<font>
<Font name="Roboto Medium" size="36.0" />
</font>
<VBox.margin>
<Insets bottom="40.0" top="40.0" />
</VBox.margin>
</Label>
<JFXListView fx:id="itemList" prefHeight="660.0" prefWidth="400.0" />
<JFXButton fx:id="addButton" focusTraversable="false" onAction="#addElement" prefHeight="50.0" prefWidth="500.0" styleClass="addbutton" stylesheets="@application.css" text="+" textFill="#21ce12ad">
<font>
<Font name="Roboto" size="33.0" />
</font>
</JFXButton>
</children>
</VBox>
</children>
</fx:root>
SelectableList.java
package de.goehring.components;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXListView;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import java.io.IOException;
public class SelectableList<T> extends AnchorPane {
@FXML
private Label titleLabel;
@FXML
private JFXListView<T> itemList;
@FXML
private JFXButton addButton;
public SelectableList() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SelectableList.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
@FXML
void addElement() {
}
}
Exactly what am I doing wrong. My SceneBuilder does not recognize this component when being imported as jar.
回答1:
The related question posted by @slaw solved my issue.
To look into it: SceneBuilder nested custom nodes
So basically , if you are struck with this topic, clone SceneBuilder and start debugging it by checking under the kit project the class JarExplorer
.
When adding a stacktrace output via x.printStacktrace()
you see the reason why your project hasnt been loaded.
So in my case, it was as mentioned by the post linked : you need to set the correct classloader. (and a missing slash. Curse you java resources.)
So this is what my constructor looks now:
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/SelectableList.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
fxmlLoader.setClassLoader(getClass().getClassLoader());
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
And after that, it worked just fine.
If someone has issues launching the scenebuilder project:
Use IntelliJ. You can click on the build.gradle and start the "run" task. Since java 11 requires modules loaded in the command line you can use this method. Otherwise you get an error which tells you "JavaFX Components" are missing.
Hope this helps.
来源:https://stackoverflow.com/questions/54006482/scenebuilder-add-custom-component-which-relies-on-third-party-components