Scene Builder中的自定义控件ClassNotFoundException [英] Custom Control ClassNotFoundException in Scene Builder
问题描述
我通过扩展现有控件创建了一个新控件,并且我想在JavaFX场景中使用此新控件.我希望能够使用Scene Builder编辑场景,但是将新控件添加到FXML文件后,在打开Scene Builder时遇到ClassNotFoundException
.
I have created a new control by extending an existing one, and I would like to use this new control in my JavaFX scenes. I would like to be able to edit my scenes using Scene Builder, but after adding the new control to the FXML file, I am encountering a ClassNotFoundException
when opening Scene Builder.
例如,这是我制作的扩展TextField
的类:
For example, here is a class I made which 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);
}
}
将此控件添加到我的FXML文件中之后...
After adding this control to my 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>
...加载Scene Builder 2.0时出现此错误:
... 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
Scene Builder为什么找不到我的新控件?我需要做什么才能使其能够找到并使用我的新控件?
Why can't Scene Builder find my new control? What do I need to do in order for it to find and be able to use my new control?
如果需要,这里还有其他文件:
Here are the other files if needed:
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*");
}
}
推荐答案
如果仍然有人无法使用SceneBuilder加载其自定义组件,只需传递ClassLoader即可为我解决该问题.
If anyone is still having troubles with SceneBuilder not loading their custom components, simply passing on a ClassLoader solved the issue for me.
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 .
这篇关于Scene Builder中的自定义控件ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!