我试图将我的gui控件(jar文件)导入到场景构建器中,但没有成功。该文件是使用eclipse export - > jar创建的。在场景构建器“导入JAR / FXML文件”中选择它时,导入对话框中没有任何内容。
我的控件是带TextFormatter的TextField:
import java.text.NumberFormat;
import java.text.ParseException;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.util.StringConverter;
public class CustomTextField extends TextField {
public CustomTextField() {
setText("Custom");
NumberTextFormatter formatter = new NumberTextFormatter(new StringConverter<Number>() {
@Override
public String toString(Number object) {
return object.toString();
}
@Override
public Number fromString(String string) {
try {
return NumberFormat.getInstance().parse(string);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
});
setTextFormatter(formatter);
}
public static class NumberTextFormatter extends TextFormatter<Number> {
public NumberTextFormatter(StringConverter<Number> valueConverter) {
super(valueConverter);
}
}
}
我确信罪魁祸首是使用TextFormatter。因为如果我从代码中删除TextFormatter,控件将起作用(出现在“导入”对话框中)。
怎么了?
更新:即使我简化了我的构造函数以排除setTextFormatter(),只要它仍然有一行TextFormatter声明,问题仍然存在。如:
public CustomTextField() {
setText("Custom");
TextFormatter<Integer> formatter = new TextFormatter<Integer>(new IntegerStringConverter());
}
找到了解决方案。 oracle场景构建器2.0太旧/太笨,无法使用TextFormatter。需要升级到Gluon场景构建器8.5.0。