2012-06-16 4 views
2

목록보기에서 선택 막대 텍스트 색상을 변경할 수있는 방법이 있습니까? CSS를 사용하는 것이 좋습니다. TableView에서 다음을 사용할 수 있습니다.Javafx ListView 선택 막대 텍스트 색상을 CellFactory 사용시

-fx-selection-bar-text: white; 

그러나 ListView에서는 작동하지 않습니다.

업데이트 : 위의 경우는 CellFactories를 사용하여 셀을 렌더링 할 때 발생합니다.

lvRooms.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { 
     @Override public ListCell<String> call(ListView<String> list) { 
      return new RoomCell(); 
     } 
    }); 

셀 팩토리 클래스에서 행을 선택하면 대소 문자를 구분하여 처리합니다.

그러나 : 그것은 선택 막대가 이동하지 때마다, 처음에 한 번만 전화, 따라서에 isSelected() 메소드는 항상 false를 렌더링한다.

업데이트 2는 :

class RoomCell extends ListCell<String> { 
     @Override 
     public void updateItem(String item, boolean empty) { 
      super.updateItem(item, empty); 

      if (item != null) { 
       Log.debug("RoomCell called, item: "+item); 
       final Label lbl = new Label(item); // The room name will be displayed here 
       lbl.setFont(Font.font("Segoe UI", FontWeight.BOLD, 18)); 
       lbl.setStyle("-fx-text-fill: black"); 

       //lbl.setTextFill(isSelected()?Color.WHITE: Color.BLACK); 
       if (isSelected()) // This is always false :(
        lbl.setStyle("-fx-text-fill: yellow"); 

       if (Rooms.getBoolean(item, "OwnerStatus")) { 
        lbl.setEffect(new DropShadow(15, Color.BLUEVIOLET)); 
        lbl.setGraphic(new ImageView(
            new Image(getClass().getResourceAsStream("images/universal.png")))); 
       } else { 
        lbl.setGraphic(new ImageView(
            new Image(getClass().getResourceAsStream("images/yin-yang.png")))); 
        lbl.setEffect(new DropShadow(15, Color.WHITE)); 
       } 
       setGraphic(lbl); 

      } 
     } 
    } 
+0

감사합니다 ... 저를 도왔! – Vikram

답변

5

-fx-selection-bar-textScene의 선택입니다 루트 기본 CSS 선택기에 정의 된 컬러 팔레트 (안 CSS 속성)이다 :이 RoomCell 구현입니다. 당신이 그것을 사용하는 방법을 모르겠어요하지만 당신처럼 (이 장면의 선택이다 세계적 이후)을 정의하는 경우 : CSS 파일에

.root{ 
    -fx-selection-bar-text: red; 
} 

다음 -fx-selection-bar-text를 사용하여 모든 컨트롤 'CSS 속성은 빨간색으로 표시됩니다. ListView도 영향을받습니다 (아래에서 원래 사용법에 대한 설명 참조).
그러나 당신 만의 ListView의 스타일을 사용자 정의 기본 속성을
(참고 : -fx-text-fill-fx-selection-bar-text가 사용되는 경우 원래 값은, 주석 처리되어 오버라이드 (override)합니다.) :이 방법을 무시하려는 경우

/* When the list-cell is selected and focused */ 
.list-view:focused .list-cell:filled:focused:selected { 
    -fx-background-color: -fx-focus-color, -fx-cell-focus-inner-border, -fx-selection-bar; 
    -fx-background-insets: 0, 1, 2; 
    -fx-background: -fx-accent; 
    /* -fx-text-fill: -fx-selection-bar-text; */ 
    -fx-text-fill: red; 
} 

/* When the list-cell is selected and selected-hovered but not focused. 
    Applied when the multiple items are selected but not focused */ 
.list-view:focused .list-cell:filled:selected, .list-view:focused .list-cell:filled:selected:hover { 
    -fx-background: -fx-accent; 
    -fx-background-color: -fx-selection-bar; 
    /* -fx-text-fill: -fx-selection-bar-text; */ 
    -fx-text-fill: green; 
} 

/* When the list-cell is selected, focused and mouse hovered */ 
.list-view:focused .list-cell:filled:focused:selected:hover { 
    -fx-background: -fx-accent; 
    -fx-background-color: -fx-focus-color, -fx-cell-focus-inner-border, -fx-selection-bar; 
    -fx-background-insets: 0, 1, 2; 
    /* -fx-text-fill: -fx-selection-bar-text; */ 
    -fx-text-fill: yellow; 
} 

이 CSS를 특성 및 더 많은 것은 붙박이 caspian.css에서 유효하다.


업데이트 : 난 강력하게 당신이 Cell API를 읽고 조언. 거기에서

... 우리는 극소수의 셀만 사용하여 매우 큰 데이터 세트를 나타냅니다. 각 셀을 "재활용"하거나 다시 사용합니다.

다른 문자열 항목은 코드에서 isSelected()과 같은 오해의 소지가있는 시각 효과/렌더링으로 끝나는 동일한 셀을 사용할 수 있습니다. 세포의 가장 일반적인 사용은 사용자에게 텍스트를 표시하는 것입니다 지금까지이 사용 사례 특별히 셀 내에서 최적화되어 있기 때문에 또한 API에서이

을 말한다. 이것은 입니다. Cell은 Labeled에서 확장했습니다. 즉, Cell의 하위 클래스는 별도의 Label을 만들고 Cell 내에서 설정하기보다는 텍스트 속성 만 설정하면됩니다.다음과 같이

그래서 난 당신의 코드를 리팩토링.

class RoomCell extends ListCell<String> { 
    @Override 
    public void updateItem(String item, boolean empty) { 
     super.updateItem(item, empty); 

     if (item != null) { 
      Log.debug("RoomCell called, item: "+item); 
      setFont(Font.font("Segoe UI", FontWeight.BOLD, 18)); 
      ImageView iView = new ImageView(); 
      if (Rooms.getBoolean(item, "OwnerStatus")) { 
       iView.setEffect(new DropShadow(15, Color.BLUEVIOLET)); 
       iView.setImage(new Image(getClass().getResourceAsStream("images/universal.png"))); 
      } else { 
       iView.setEffect(new DropShadow(15, Color.WHITE)); 
       iView.setImage(new Image(getClass().getResourceAsStream("images/yin-yang.png"))); 
      } 
      setGraphic(iView); // The image will be displayed here 
      setText(item); // The room name will be displayed here 
     } 
    } 
} 

모두 -fx-text-fill CSS 파일의 정의에 따라 셀의 텍스트 스타일이 변경됩니다.

여기에서 지금은 트레이드 오프 셀의 텍스트의 DropShadow 효과와 CSS 파일에서의 채우기 색상 간의이다 :
- 당신의 DropShadow 효과를 사용하려면, 현재의 방법, 즉, 라벨을 만들어 해당 텍스트를 설정처럼 이동해야 할 경우 , 레이블 및 setGraphic (레이블)에 dorpshadow 효과를 부여하십시오. 그러나 이번에는 셀의 텍스트 (setText(item))를 설정하는 것을 선호하지 않으므로 CSS 파일의 텍스트 색상 스타일은 아무 효과가 없습니다.
- 반면에, 당신은 내가 할 CSS 파일에 DropShadow를 위해, 당신은 transparent 또는 null으로 설정하여 (Labeled을 확장) 세포의 -fx-background-color을 비활성화하고 -fx-effect을 설정하려면해야 리팩토링 한 코드를 선호하는 경우 텍스트에 드롭 섀도우 효과를 직접 적용 할 수 있습니다. 세포의 배경을 지우는 것이 선호되는 방법이 아닙니다. 코드에 따른 설명 :

Label lbl = new Label("This text will have a dropshadow on itself directly"); 
lbl.setEffect(new DropShadow(15, Color.BLUE)); 

Label another_lbl = new Label("This text will have a dropshadow applied on the background bounds, not to text"); 
another_lbl.setEffect(new DropShadow(15, Color.BLUE)); 
another_lbl.setStyle("-fx-background-color:gray"); 

차이점을 확인하려면 테스트하십시오. 그게 다야.

+0

Uluk에 감사드립니다. 두 가지 솔루션 모두 정상적으로 작동하지만 내 경우에는 그렇지 않습니다. CellFactory를 사용하여 셀을 렌더링합니다. 위의 질문에 대한 업데이트에서 설명했습니다. 이 사건에서 내가 할 수있는 일은 뭐니? – betaman

+0

예, 저는 귀중한 리소스 인 다른 솔루션을 해킹하려고 시도 할 때 항상 caspian.css 파일을 엽니 다. – betaman

+1

@ 베타 만, 공식 ListView 튜토리얼에서 솔루션을 테스트했다. CellFactory는 커스텀 셀'ColorRectCell' (extends ListCell )에서도 사용됩니다. 'RoomCell'을 게시하거나'ColorRectCell'과 비교하십시오. –