带有-fx-background-color的JavaFX setStyle仅绘制TextArea的边缘

时间:2016-06-17 08:59:31

标签: java css javafx

所以,我有一个动态创建的CheckBox列表,我想根据我选择的TextArea绘制它们。目前,它只对边框进行着色,而不是if (cb.isSelected()) { ta.setStyle("-fx-background-color:#38ee00;"); } if (cb2.isSelected()) { ta.setStyle("-fx-background-color:orangered;"); } if (!cb2.isSelected() && !cb.isSelected()) { ta.setStyle("-fx-background-color:white;"); } 的内部。

这是我的代码:

private void OrderList(List<string> ListaFiles)
{
   List<string> list = ((IEnumerable<string>)Directory.GetFiles(Directories, "*.sql", SearchOption.TopDirectoryOnly)).Select(f =>
    {
      string[] strArray = Path.GetFileName(f).Split('_');
      int result;
      if (strArray.Length < 1 || !int.TryParse(strArray[0], out result))
          result = -1;
      var data = new
      {
        File = f,
        Version = result
      };
      return data;
      }).Where(f => f.Version > -1).OrderBy(f => f.Version).Select(f => f.File).ToList<string>();
}

这是它带来的结果:

enter image description here

如果您需要任何其他信息,请随时告诉我。

2 个答案:

答案 0 :(得分:1)

您实际上需要为.contentsee CSS Reference: TextArea - Substructure)的孩子TextArea区域设置样式。

您可以使用lookup获取该节点,但仅在应用外观之后才能获得该节点,这在布局之前不会发生。然后,你可以做这样的事情:

Region content = (Region) ta.lookup(".content");
content.setStyle("-fx-background-color:orangered;");

另一种方法是使用CSS样式表并更改TextArea的类(仅提供有限数量的颜色):

.text-area.color-orangered .content {
     -fx-background-color: orangered;
}

.text-area.color-white .content {
     -fx-background-color: white;
}

.text-area.color-green .content {
    ...
// remove old class for coloring
ta.getStyleClass().removeIf(s -> s.startsWith("color-"));

// add new class
ta.getStyleClass().add("color-orangered");

答案 1 :(得分:1)

您正在寻找的CSS样式类是.text-area .content。您有几个选项可以影响此样式类,一种可能是使用PseudoClass,这使您可以使用CSS选择器。

Main.java

public class Main extends Application {


    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            root.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            // Add the CSS style class
            TextArea ta = new TextArea();
            ta.getStyleClass().add("my-text-area");


            ChoiceBox<String> cb = new ChoiceBox<String>(FXCollections.observableArrayList("green", "orangered"));
            cb.valueProperty().addListener(new ChangeListener<String>() {

                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                // On selection, change the pseudo class of the TextArea
                ta.pseudoClassStateChanged(PseudoClass.getPseudoClass(newValue), true);
                }
            });

            root.setCenter(ta);
            root.setBottom(cb);

            Scene scene = new Scene(root,400,400);

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

application.css

.my-text-area:orangered .content {  
   -fx-background-color: orangered ;  
}  

.my-text-area:green .content {  
   -fx-background-color: green ;  
}  
相关问题