2016-09-29 15 views
0
TableColumn tc = new TableColumn(); 

tc.getStyleClass.add(".style in css file") 

나는 css 파일로 tablecolumn을 설정합니다. 그리고 나는 각 세포가 다른 배경을 가지게하고 싶다. 그것을 할 방법이 있습니까?JavaFX 테이블 뷰는 특정 셀을 색으로 표시합니다.

TableColumn의 행 1 bakcground에 색상 = 녹색, ROW2 = 빨강, row3 = 파란색 .... 등

답변

5

당신은 당신의 TableView 및 변경 행 스타일 setRowFactory을 사용해야합니다. 이 약간의 예 :

tableView.setRowFactory(new Callback<TableView<Data_type>, TableRow<Data_type>>(){ 
      //There can define some colors. 
      int color = 0; 
      String colors[] = new String[]{"red","blue","green"}; 
      @Override 
      public TableRow<Data_type> call(TableView<Data_type> param) { 
       final TableRow<Data_type> row = new TableRow<Data_type>() { 
        @Override 
        protected void updateItem(Data_type item, boolean empty) { 
         super.updateItem(item, empty); 
         //there write your code to stylize row 
         if(getIndex() > -1){ 
          String color = colors[getIndex() % 3]; 
          setStyle("-fx-background-color: "+ color + ";"); 

         } 
        } 
       }; 
       return row; 
      } 
     }); 

결과 :
enter image description here

+1

프로 팁 : 대신'-fx - 배경 - color'의'-fx - background'를 사용합니다. 이렇게하면 테두리가 올바르게 유지되고 텍스트 색상이 배경색의 밝기/어둠에 자동으로 적응할 수 있습니다. –

+0

스크롤하면 테이블보기에서 색상이 바뀝니다. –