2016-10-04 2 views
1

ありTreeViewコントロールによって変更ツリー項目のスタイルは、それぞれの要素が表示可能インタフェースを実装しています:JavaFx。条件

public interface Viewable { 

    enum ViewStyle { 

     NEW("-fx-background-color: b8faa7;"), 
     NEW_PARENT("-fx-background-color: b8ebbb;"), 
     LOCKED("-fx-background-color: adadad; "), 
     HAS_NO_DATA("-fx-background-color: eb8d8d;"); 

     String style; 

     ViewStyle(String style){ 
      this.style = style; 
     } 

     public String getStyle() { 
      return style; 
     } 

    } 

    ViewStyle getViewStyle(); 
    void setViewStyle(ViewStyle style); 
    StringProperty styleProperty(); 

    String getTreeItemTitle(); 
    void setTreeItemTitle(String title); 
    StringProperty titleProperty(); 

} 

各要素は、()独自の.stylePropertyを持っており、ViewStyle.getStyle(から値を取得する)

このプロパティ各TreeCell.styleProperty()に対してバインドします。

問題はツリーセルが選択に醜い表示されることです。つまり、選択されたセルの色は変化しません。文字の色のみを変更する(デフォルトのテーマに従って)が、あまり便利ではない。したがって、おそらく.cssファイルを添付する必要があります。同時に、現在のViewStyleに応じてセルのスタイル(デフォルトと選択時)を変更する方法を理解していません。

答えて

1

あなたは単にのみ非選択セル(-fx-control-inner-background)のために使用されているものにCSSプロパティを変更することができます:

enum ViewStyle { 

    NEW("-fx-control-inner-background: b8faa7;"), 
    NEW_PARENT("-fx-control-inner-background: b8ebbb;"), 
    LOCKED("-fx-control-inner-background: adadad; "), 
    HAS_NO_DATA("-fx-control-inner-background: eb8d8d;"); 

はまた、あなたが上書きされたバージョンでは、あなたがやるべきではない何かをしたことに注意してくださいupdateItemメソッド:必ずしもsuper.updateItemを呼び出すとは限りません。これにより、filled/empty疑似クラスが正しく割り当てられず、コールの項目を含まないTreeCellitemプロパティにつながる可能性があります。代わりに次のようにしてください:

@Override 
protected void updateItem(Viewable item, boolean empty) { 
    textProperty().unbind(); 
    styleProperty().unbind(); 
    if (empty || item == null) { 
     setText(null); 
     setStyle(null); 
    } else { 
     styleProperty().bind(item.styleProperty()); 
     textProperty().bind(item.titleProperty()); 
    } 
    super.updateItem(item, empty); 
} 
+0

ありがとうございます!できます! –