JavaFX自定义控件(TextField)无法正常工作

时间:2013-05-21 15:10:26

标签: custom-controls javafx-2 javafx fxml scenebuilder

我正在尝试使用JavaFX和SceneBuilder 1.1进行自定义控件。

我有这段代码:

FXML


<?import libreria.javaFX.componentes.componenteTextField.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <CustomComponent fx:id="pastaTxt" layoutX="69.0" layoutY="87.0" prefWidth="200.0" />
  </children>
</AnchorPane>

CustomComponent.java


package libreria.javaFX.componentes.componenteTextField;

import javafx.scene.control.TextField;

public class CustomComponent extends TextField {

public CustomComponent() {
    super();
    // TODO Auto-generated constructor stub
}

public CustomComponent(String arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
}

}


当我尝试从SceneBuilder打开它时,它告诉我:

  

缺少的类型有:[CustomComponent]

它让我有机会指定Classpath(它也不能解决问题)。

我也尝试将这个类放在import语句中,如下所示:

<?import libreria.javaFX.componentes.componenteTextField.CustomComponent?>

但它提供了ClassNotFoundException

关于为什么会发生这种情况的任何想法?


更多信息

我已经用这些类完成了一个新项目:

enter image description here

代码如下:

CustomControl.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import custom.CustomControl?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?scenebuilder-classpath-element ../../bin/custom?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <CustomControl layoutX="51.0" layoutY="100.0" prefWidth="200.0" />
  </children>
</AnchorPane>

CustomControl.java

package custom;

import javafx.scene.control.TextField;

public class CustomControl extends TextField {

    public CustomControl() {
        super();
    }

public CustomControl(String arg0) {
    super(arg0);

    }
}

我仍有同样的问题。我用对话框指定了类路径,一切似乎都适合我,但是我在打开SceneBuilder时遇到了同样的错误。


最后信息

试图接近解决方案,我们在Eclipse下尝试了这个项目。结果是Eclipse显示窗口正常,但SceneBuilder继续这些错误。我希望这条线索有所帮助。

如果有人在Scene Builder下完成了这种自定义控件定义,请告诉我们并举个例子,这对我们的项目非常有帮助。

2 个答案:

答案 0 :(得分:4)

这是因为没有指定正确的类路径,这允许运行时运行时的Java运行时场景构建器加载控件类。

如果您正在运行eclipse并且您的类具有名称空间custom.MyControl,那么请指定bin目录而不是自定义目录。在maven项目中,您需要指定目标/类目录。

在此处查看我自己项目中的示例:https://bitbucket.org/atill/estimate/src/22390a2ca034b55f1916e46435b714e5c489b90e/src/main/resources/projmon/gui/workTree.fxml?at=master

相对文件路径通常由场景构建器创建,因此移动文件将破坏类路径,您需要重新指定它。

答案 1 :(得分:1)

也许现在已经晚了但我想告诉我做了什么。 我使用的是J8,Eclipse IDE和Scenebuilder 2.0。

1)在Eclipse IDE上,从Window-&gt; Show View&gt;打开Navigator视图。航海家 2)在您的项目中,右键单击并选择&#34;属性&#34;在您想要在SB(场景构建器)上看到的类上。从“资源”部分验证生成的类的位置。例如,您应该看到如下所示的位置。 /TableViewDemo/bin/com/company/jfx8/example/fxmltableview/FormattedTableCellFactory.class

3)复制此地址并添加fxml,例如

<?scenebuilder-classpath-element ../../../../TableViewDemo/bin/com/company/jfx8/example/fxmltableview/FormattedTableCellFactory.class?> 

4)然后保存并享受你的工作:)

相关问题