JavaFX扩展Button并通过fxml添加属性 [英] JavaFX extend Button and add properties through fxml

查看:508
本文介绍了JavaFX扩展Button并通过fxml添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经像这样扩展了JavaFX Button:

I have extended a JavaFX Button like so:

public class ExtendedButton extends Button {
    private ObjectProperty<Enum> x;
    public final void setX(Enum value) {
        x.set(value); 
    }
    public final String getX() {
        return x.get()
    }
}

public enum MyEnum{
    A,
    B
}

现在,我希望能够在我的FXML中使用它.像这样:

Now i want to be able to use this in my FXML. Something like this:

<ExtendedButton fx:id="xButton" x=MyEnum.A />

我如何实现这一目标.

推荐答案

您可以使用属性元素(而不是属性)以及

You can do this using property elements, instead of attributes, along with the fx:constant attribute:

<ExtendedButton fx:id="xButton" >
    <x><MyEnum fx:constant="A" /></x>
</ExtendedButton>

这是SSCCE:

ExtendedButton.java

ExtendedButton.java

package application;

import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.Button;

public class ExtendedButton extends Button {

    public final ObjectProperty<Enum<?>> buttonState = new SimpleObjectProperty<>(ButtonState.NORMAL);

    public final ObjectProperty<Enum<?>> buttonStateProperty() {
        return this.buttonState;
    }


    public final Enum<?> getButtonState() {
        return this.buttonStateProperty().get();
    }


    public final void setButtonState(final Enum<?> buttonState) {
        this.buttonStateProperty().set(buttonState);
    }

    public ExtendedButton() {
        super();
        styleProperty().bind(Bindings.
                when(buttonState.isEqualTo(ButtonState.CRITICAL)).
                then("-fx-base: red;").
                otherwise(""));


    }

}

ButtonState.java:

ButtonState.java:

package application;

public enum ButtonState { NORMAL, CRITICAL }

ExtendedButtonTest.fxml:

ExtendedButtonTest.fxml:

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

<?import javafx.scene.layout.StackPane?>
<?import application.ExtendedButton?>
<?import application.ButtonState?>
<?import javafx.geometry.Insets?>

<StackPane xmlns:fx="http://javafx.com/fxml/1">
    <padding>
        <Insets top="20" right="20" bottom="20" left="20"/>
    </padding>
    <ExtendedButton text="Test" >
        <buttonState>
            <ButtonState fx:constant="CRITICAL"/>
        </buttonState>
    </ExtendedButton>
</StackPane>

ExtendedButtonTest.java:

ExtendedButtonTest.java:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ExtendedButtonTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("ExtendedButtonTest.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于JavaFX扩展Button并通过fxml添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆