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

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

私はCSSファイルでtablecolumnを設定します。私は各セルに異なる背景があるようにしたい。それを行う方法はありますか?JavaFX Tableviewは、特定のセルを色付けします。

TableColumnの行1 bakcground色=緑、ROW2 =赤、ROW3 =青....など

答えて

5

あなたのテーブルビューと変更行のスタイルのための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

スクロールすると、テーブルビューで色が変わります –

関連する問題