Reputation: 127
I have a class called StatusContext that multiple packages use to display a status/error to the main window using a Label. They did this by binding to the Label's text property. I'd like to add a color setting to StatusContext so I can change the Label's textFill color based on the status/error that is set. I can't figure out how to go about it and bind to the Label's 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);
}
}
Upvotes: 0
Views: 438
Reputation: 449
You can add a listener to the textProperty and handle the value when it changes.
E.g.:
label.textProperty().addListener((obs, oldValue, newValue)->{
switch(newValue){
case "yourValue":
label.setTextFill(Color.AQUA);
break;
//Your rules
}
});
Upvotes: 3