如何绑定标签的textFillProperty?

时间:2019-08-12 00:06:12

标签: javafx label bind

我有一个名为StatusContext的类,多个包使用该类使用Label向主窗口显示状态/错误。他们通过绑定到Label的text属性来完成此操作。我想向StatusContext添加颜色设置,以便可以根据设置的状态/错误来更改Label的textFill颜色。我不知道该怎么做并绑定到Label的textFillProperty。

FXMLDocument.fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="labelbindtest.FXMLDocumentController">
    <children>
        <Button fx:id="textButton" layoutX="93.0" layoutY="88.0" onAction="#handleTextButtonAction" text="Show Error" />
        <Label fx:id="label" layoutX="142.0" layoutY="122.0" minHeight="16" minWidth="69" />
      <Button fx:id="colorButton" layoutX="176.0" layoutY="88.0" mnemonicParsing="false" onAction="#handleColorButtonAction" text="Set Error RED" />
    </children>
</AnchorPane>

FXMLDocumentController.java:

package labelbindtest;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class FXMLDocumentController implements Initializable {
    @FXML
    private Label label;
    @FXML
    private Button textButton;
    @FXML    
    private Button colorButton;
    @FXML
    private void handleColorButtonAction(ActionEvent event) {
        // Something like StatusContext.setColor(Color.RED); ??
    }
    @FXML
    private void handleTextButtonAction(ActionEvent event) {
        StatusContext.setStatus("Here's the error");
    }
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        label.textProperty().bind(StatusContext.getProperty());
        // Something like label.textFillProperty().bind(StatusContext.getColor()); ??
    }
}

LabelBindTest.java:

package labelbindtest;

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

public class LabelBindTest extends Application {

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

StatusContext.java:

package labelbindtest;

import javafx.beans.property.SimpleStringProperty;

public class StatusContext {
    private static final SimpleStringProperty status = new SimpleStringProperty("");

    public static SimpleStringProperty getProperty(){
        return status;
    }

    public static void clear(){
        status.setValue("");
    }

    public static void setStatus(String st){
        status.setValue(st);
    }
}

1 个答案:

答案 0 :(得分:3)

您可以将侦听器添加到textProperty并在其更改时处理该值。

例如:

    label.textProperty().addListener((obs, oldValue, newValue)->{
        switch(newValue){
            case "yourValue":
                label.setTextFill(Color.AQUA);
                break;
            //Your rules    
        }
    });