简体   繁体   English

JavaFX无法设置自定义属性

[英]JavaFX Unable To Set Custom Properties

I'm trying to implement custom properties into a custom component for my JavaFX application. 我正在尝试在JavaFX应用程序的自定义组件中实现自定义属性。 I've read some tutorials and all have pointed me in the direction of below. 我已经阅读了一些教程,并且都向我指出了下面的方向。

For some reason, it's not working. 由于某种原因,它不起作用。 IntelliJ isn't liking when I try and set the property values inside of the FXML file and the scene builder doesn't show the property either. 当我尝试在FXML文件中设置属性值并且场景构建器也不显示该属性时,IntelliJ不喜欢它。

Label Controller: 标签控制器:

public class LabelController {

    @FXML
    public Label label;

    // Define a variable to store the property
    private DoubleProperty amountDue = new SimpleDoubleProperty();

    // Define a getter for the property's value
    public final double getAmountDue(){return amountDue.get();}

    // Define a setter for the property's value
    public final void setAmountDue(double value){amountDue.set(value);}

    // Define a getter for the property itself
    public DoubleProperty amountDueProperty() {return amountDue;}

    public void onMouseEntered(MouseEvent mouseEvent) {
        FadeToHoverColour();
    }

    public void onMouseExited(MouseEvent mouseEvent) {
        FadeToDefaultColour();
    }

    public void FadeToHoverColour() {
        Timeline timeline = new Timeline();
        timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(0.2), new KeyValue(label.textFillProperty(), Paint.valueOf("E63700"))));
        timeline.play();
    }

    public void FadeToDefaultColour() {
        Timeline timeline = new Timeline();
        timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(0.2), new KeyValue(label.textFillProperty(), Paint.valueOf("FF774D"))));
        timeline.play();
    }
}

Label.fxml: Label.fxml:

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>

<VBox xmlns="http://javafx.com/javafx"
      xmlns:fx="http://javafx.com/fxml"
      fx:controller="madoc.controllers.components.LabelController">
    <Label fx:id="label"
           text="TEXT"
           onMouseEntered="#onMouseEntered"
           onMouseExited="#onMouseExited">
    </Label>
</VBox>

WelcomeSceneBuilder.fxml: WelcomeSceneBuilder.fxml:

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

<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="madoc.controllers.scenes.WelcomeSceneController">
    <fx:include source="./Label.fxml" [NOT WORKING WHEN I TRY TO SET AMOUNT DUE HERE]/>
</AnchorPane>

The attributes/children added to the <fx:include> element apply to the result of loading the other fxml, ie in this case the type of object created is VBox , not LabelController . 添加到<fx:include>元素的属性/子元素适用于加载另一个fxml的结果,即,在这种情况下,创建的对象类型为VBox ,而不是LabelController VBox does not contain the properties you're trying to assign. VBox不包含您要分配的属性。

You cannot do this using fxml only. 您不能仅使用fxml来执行此操作。 You'd need to use the initialize method of the controller to set the property values: 您需要使用控制器的initialize方法来设置属性值:

WelcomeSceneController WelcomeSceneController

@FXML
private LabelController labelController;

@FXML
private void initialize() {
    labelController.setAmountDue(...);
}

WelcomeSceneBuilder.fxml WelcomeSceneBuilder.fxml

...
<fx:include source="./Label.fxml" fx:id="label"/>
...

You could use the Custom Component approach though which would make the controller and node the same object allowing you to make assignments like these. 您可以使用“自定义组件”方法,但这会使控制器和节点成为同一对象,从而使您可以进行此类分配。 Lacking inso about the responsibility of the node, I'm keeping LabelController , but of course you should choose a better name. 由于缺乏节点的责任,我保留LabelController ,但是您当然应该选择一个更好的名称。

package madoc.controllers.components;

...

public class LabelController extends VBox {

    public LabelController() {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/madoc/controllers/components/Label.fxml")); // TODO: replace with correct resoure path?
        loader.setRoot(this);
        loader.setController(this);
        try {
            loader.load();
        } catch(IOException ex) {
            throw new IllegalStateException("Could not load fxml file", ex);
        }
    }

    @FXML
    public Label label;

    // Define a variable to store the property
    private final DoubleProperty amountDue = new SimpleDoubleProperty();

    // Define a getter for the property's value
    public final double getAmountDue(){return amountDue.get();}

    // Define a setter for the property's value
    public final void setAmountDue(double value){amountDue.set(value);}

    // Define a getter for the property itself
    public DoubleProperty amountDueProperty() {return amountDue;}

    ...
}

Label.fxml Label.fxml

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>

<fx:root xmlns="http://javafx.com/javafx"
      xmlns:fx="http://javafx.com/fxml"
      type="javafx.scene.layout.VBox">
    <Label fx:id="label"
           text="TEXT"
           onMouseEntered="#onMouseEntered"
           onMouseExited="#onMouseExited">
    </Label>
</fx:root>

WelcomeSceneBuilder.fxml WelcomeSceneBuilder.fxml

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

<?import javafx.scene.layout.AnchorPane?>
<?import madoc.controllers.components.LabelController?>

<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="madoc.controllers.scenes.WelcomeSceneController">
    <LabelController amountDue="30.05"/>
</AnchorPane>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM