2016-03-22 47 views
1

我想为我的项目设置一个模型,以便我的控制器可以相互通信。我希望它有一个setter和getter,以允许从任何一个类别轻松访问样式化某些节点。在JavaFX中将CSS样式属性绑定到节点

我的问题:是否有可能将一个样式属性(例如“-fx-background-color:blue”)绑定到节点上?

从我的研究中,我发现标签的文本值绝对是可能的(James_D在这里解释:JavaFX - How to use a method in a controller from another controller?),但我很难弄清楚用“setStyle”做类似的事情的语法是什么,将会。

模型我到目前为止有:

public class Model { 

    private final StringProperty shadow = new SimpleStringProperty("-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.24), 10,0,0,0)"); 

    public StringProperty shadowProperty() { 
     return shadow; 
    } 

    public final String getShadow() { 
     return shadowProperty().get(); 
    } 

    public final void setShadow(String shadow) { 
     shadowProperty().set(shadow); 
    } 
} 

我明白,我怎么会从一个控制器设定的“影子”的价值,但我不明白的是我怎么能一个节点从另一个控制器绑定听取这一变化。

假设节点是一样的东西:

@FXML AnchorPane appBar 

我想“appBar”采取到模型中的“阴影”所做的任何更改。那将是什么样子?

+1

你能不能给你试了一下一个例子,并解释为什么没有工作?目前还不清楚确切的问题是什么。 – Itai

+1

已更新的问题。 – Shane

回答

2

您需要将侦听器添加到shadowProperty以侦听其更改。

something.shadowProperty() .addListener((observable, oldValue, newValue) -> { 
    //do something with appBar 
}) ; 

我不完全确定你想要达到什么,但是这应该回答你关于如何听取属性更改的问题。

PS:IM上移动,所以没有担保关于错别字

编辑:您还可以将一个对象的属性绑定到他人财产。对此使用bind()

编辑:下面是一个例子:

import javafx.application.Application; 
import javafx.beans.property.Property; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.scene.Scene; 
import javafx.scene.layout.Background; 
import javafx.scene.layout.BackgroundFill; 
import javafx.scene.layout.Pane; 
import javafx.scene.layout.VBox; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class Main extends Application { 

    Property<Background> backgroundProperty; 
    StringProperty styleProperty; 

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

    @Override 
    public void start(Stage primaryStage) throws Exception { 

     VBox root = new VBox(10); 

     backgroundProperty = new SimpleObjectProperty<>(); 
     styleProperty = new SimpleStringProperty(); 

     // Pane that changes background by listener 
     Pane pane1 = new Pane(); 
     pane1.setMinHeight(40); 
     backgroundProperty.addListener((observable, oldValue, newValue) -> { 
      pane1.setBackground(backgroundProperty.getValue()); 
     }); 

     // Pane that changes background by property binding 
     Pane pane2 = new Pane(); 
     pane2.setMinHeight(40); 
     pane2.backgroundProperty().bind(backgroundProperty); 

     // Pane that binds the style property 
     Pane pane3 = new Pane(); 
     pane3.setMinHeight(40); 
     pane3.styleProperty().bind(styleProperty); 

     backgroundProperty.setValue(new Background(new BackgroundFill(Color.RED, null, null))); 
     styleProperty.setValue("-fx-background-color: black"); 

     root.getChildren().add(pane1); 
     root.getChildren().add(pane2); 
     root.getChildren().add(pane3); 

     Scene scene = new Scene(root, 200, 400); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

    } 
} 
+0

感谢您的回复。我的问题是..什么会绑定一个节点的CSS属性看起来像?例如“-fx-background-color”。 – Shane

+0

添加了一个示例 – JohnRW

+0

您当然也可以使用字符串属性,听取并通过在侦听器中设置新的css样式来更改样式 – JohnRW

相关问题