2016-10-17 2 views
0

조건에 따라 목록보기에서 개별 셀의 스타일을 설정하려고합니다. 목록보기는 Label 유형이며 레이블은 목록보기에 삽입 될 때 다른 클래스를 기반으로 조건이 결정됩니다.JavaFX의 조건에 따라 목록보기 셀의 스타일을 설정하는 방법

현재 목록보기에서 각 레이블의 배경색을 설정하려고했지만 전체 영역을 덮지는 않습니다 (그림 참조).

enter image description here

나는이 기사를 연구했지만 그것은 문자열에 실시되지 않은 목록보기 셀을 강조하기위한 식별자로 내 경우에 적합하지 않습니다. 지금까지

How do I dynamically change the background of an item in a listview in JavaFX

현재 코드 :

for (Consultation c : notifications){ 
      Label l = new Label(": consult reminder - " + c.getReminderTime()); 

      if(c.getReminderRead() == 1){ //if the consultation reminder hasn't been read 
       l.setStyle("-fx-background-color: #33CEFF"); 
       counter = counter + 1; 
      } 


      notificationList.getItems().add(l); 
} 

어떤 아이디어? 당신이를 작성한다면

답변

2

그것은 항상 기본적 등 ListView, TableView, 같은 컨트롤에 대한 데이터 유형으로 (예 : Label 등) 노드의 서브 클래스를 사용하면 오류가 발생합니다 (내가 생각할 수있는 유일한 예외는 데이터가 실제 GUI 노드였던 장면 빌더와 같은 GUI 빌더. 그럼에도 불구하고 잘 작동하지 않을 것입니다. GUI 노드 클래스는 작성하는 데 비용이 많이 듭니다. 일반적으로 스타일 및 CSS 파싱, 이벤트 리스너 등과 관련하여 수백 가지 속성과 오버 헤드가 있습니다. 실제 데이터 클래스 인 Consultation과 비교해보십시오. 아마도 12 개 이하의 속성을 가지며 다른 오버 헤드는 없을 것입니다. Label 또는 기타 노드 클래스를 ListView의 데이터 유형으로 사용하는 경우 항목마다 하나의 노드를 작성합니다. ListView의 요점은 각 보이는 에 대해서만 노드를 만들고 그 셀을 재사용한다는 것입니다. 따라서 목록이 많으면 접근 방식이 성능 문제를 일으킬 수 있습니다. (다른 점은 당신이 모델에서 보기 (프리젠 테이션) (데이터)의 분리를 위반하고 있다는 것입니다.)

그래서 당신은 정말 여기 ListView<Consultation>를 가지고 있고,에 셀 공장을 사용해야합니다 셀의 텍스트와 스타일을 설정하십시오 :

ListView<Consultation> notificationList = ... ; 

notificationList.setCellFactory(lv -> new ListCell<Consultation>() { 
    @Override 
    protected void updateItem(Consultation c, boolean empty) { 
     super.updateItem(c, empty); 
     if (empty) { 
      setText(null); 
      setStyle(""); 
     } else { 
      setText(": consult reminder - " + c.getReminderTime()); 

      // Are you REALLY using an int for the reminderRead property??!? 
      // Surely this should be a boolean... 

      if (c.getReminderRead() == 1) { // I feel dirty just writing that 
       setStyle("-fx-background-color: #33CEFF"); 
      } else { 
       setStyle(""); 
      } 
     } 
    } 
}); 

notificationList.getItems().setAll(notifications); 

스타일을 외부 CSS 파일로 고려하는 것이 약간 낫습니다. 내가 항목을 선택하려면 지금 드릴 수 없습니다 어떤 이유가 있습니까

.list-cell:reminder-read { 
    -fx-background-color: #33CEFF ; 
} 
+0

을 : 이제 외부 CSS 파일에 당신이 할 수있는

ListView<Consultation> notificationList = ... ; PseudoClass reminderRead = PseudoClass.getPseudoClass("reminder-read"); notificationList.setCellFactory(lv -> new ListCell<Consultation>() { @Override protected void updateItem(Consultation c, boolean empty) { super.updateItem(c, empty); if (empty) { setText(null); } else { setText(": consult reminder - " + c.getReminderTime()); } // assumes Consultation.getReminderRead() returns a boolean... pseudoClassStateChanged(reminderRead, c != null && c.getReminderRead()) } }); notificationList.getItems().setAll(notifications); 

: 당신과 떨어져 다른 스타일을 설정하는 CSS의 PseudoClass을 사용할 수 있습니다 이 목록에? – Philayyy

+0

예,'super.updateItem (...)'에 대한 호출을 잊어 버렸습니다. 이제 수정되었습니다. –

+0

감사합니다 James, 다시 한 번 큰 도움이됩니다. – Philayyy

관련 문제