2016-06-17 4 views
2

したがって、私はTextAreaの動的に作成されたリストを持っており、CheckBoxを選択することによってそれらをペイントしたいと思います。現在のところ、境界線は色付けされますが、TextAreaの内側は色付けされません。-fx-background-colorのJavaFX setStyleは、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;"); 
} 

そして、ここではそれがもたらす結果である:あなたが他のどの情報が必要な場合

enter image description here

は、私に知らせて自由に感じなさい。

答えて

1

TextAreasee CSS Reference: TextArea - Substructure)の子である.contentの領域を実際にスタイルする必要があります。

lookupを使用してそのノードを取得することはできますが、スキンが適用された後で、レイアウトまでは発生しません。 (提供色の限られた数が使用されている)CSSスタイルシートを使用してTextAreaのクラスを変更することになる

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

代替:

.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

あなたが探しているCSSスタイルクラスは.text-area .contentです。このスタイルクラスに影響を与えるオプションがいくつかありますが、その1つは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 ; 
} 
関連する問題