2017-03-08 91 views
0

我期待设置单元格列以接收来自observableList中来自数据库的值的替换对象圆。值填充在该列中,以反映另一列(让我们说列A和B(从左到右) - 它们基本上包含相同的信息,除了 - 我想列B改变来表示圆对象。这里是我的代码到目前为止请告知,如果您有任何建议。javafx表列单元格更改

status.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, Circle>, ObservableValue<Circle>>() { 
    @Override 
    public ObservableValue<Circle> call(TableColumn.CellDataFeatures<ObservableList, Circle> param) { 


     String c = (String) param.getValue().get(2); //getting all data in column 2 of the row 
     System.out.println(c); 

     switch(c){ 

      case "High":   
       circle.setFill(Color.GREEN); 
       healthstatus.setStyle("-fx-alignment: CENTER;");    
       break; 

      case "Medium": 
       circle.setFill(Color.YELLOW); 
       healthstatus.setStyle("-fx-alignment: CENTER;");   
       break; 

      case "Low": 
       circle.setFill(Color.RED); 
       healthstatus.setStyle("-fx-alignment: CENTER;");   
       break; 

      default: 
       circle.setFill(Color.BLUEVIOLET); 
      } 
     return new SimpleObjectProperty(circle);       
    }    
}); 

我宁愿继续与我的代码,而无需创建响应类设置的值。

我已经附上图片,以显示我的结果为止

在此先感谢! Image

+0

如果两列包含相同的信息,但他们是如何显示它不同,使用两个相同的'cellValueFactory'。使用不同的'cellFactory'来改变数据的显示方式。 –

回答

-1

我想建议您使用cellFactory()属性而不是cellValueFactory()属性。因为cellValueFactory()处理单元内的显示项目。而不是,cellFactory()处理单元格的自定义外观。

Callback<TableColumn<Person, String>, TableCell<Person, String>> circle_cell_factory = (final TableColumn<Person, String> param) -> { 
     final TableCell<Person, String> circle_cell = new TableCell<Person, String>() 
     { 
      final Circle circle = new Circle(6); 

      @Override 
      public void updateItem(String item, boolean empty) 
      { 
       super.updateItem(item, empty); 
       if(!empty) { 
        switch(param.getCellData(getIndex())) { 
         case "High":   
          circle.setFill(Color.GREEN); 
          healthstatus.setStyle("-fx-alignment: CENTER;");    
          break; 

         case "Medium": 
          circle.setFill(Color.YELLOW); 
          healthstatus.setStyle("-fx-alignment: CENTER;");   
          break; 

         case "Low": 
          circle.setFill(Color.RED); 
          healthstatus.setStyle("-fx-alignment: CENTER;");   
          break; 

         default: 
          circle.setFill(Color.BLUEVIOLET); 
         } 
        } 
        setGraphic(circle); 
       } 
      } 
     }; 
     return circle_cell; 
    }; 

然后加入circle_cell_factory到你的专栏。

status.setCellFactory(circle_cell_factory); 

输出:

enter image description here

+0

天才! @ShekkarRaee - 谢谢,它工作! ...因为我是noob,我仍然需要消化更多的概念 – jerry

+0

@jerry,祝你好运! –