我通过扩展现有控件创建了一个新控件,并且我想在我的JavaFX场景中使用这个新控件。我希望能够使用Scene Builder编辑我的场景,但是在将新控件添加到FXML文件后,打开Scene Builder时遇到了ClassNotFoundException
。场景生成器中的自定义控件ClassNotFoundException
例如,这里是我做了一个类扩展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文件后...
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>
...我得到这个错误,当l Oading 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不能找到我的新控件?为了找到并能够使用我的新控件,我需要做些什么?
这里,如果需要的是其他的文件:
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*");
}
}
可能是http://stackoverflow.com/questions/30063792/adding-a-custom-component-to-scenebuilder-2-0 –
@James_D的副本工作,谢谢。虽然我有点恼火。为什么他们会删除'scenebuilder-classpath-element'标签?这似乎意味着我每次改变我的课程时,都必须重新构建jar并重新导入它。有没有更简单的方法来做到这一点? –