2015-09-05 29 views
0

我想在自定義渲染的tableview列中顯示數據。我試圖根據需要調整this tutoJavaFX:當試圖使用cellfactory時,數據不會顯示在tableview的列中

問題: 在下面的代碼例如,當我使用setCellFactory()方法時,不顯示在相應的列中的數據。

您可以對分隔的部分進行評論或取消註釋,以查看控制器類中發生了什麼。

Main類

public class CellFactory extends Application { 
    @Override 
    public void start(Stage primaryStage) throws Exception { 
     AnchorPane root = FXMLLoader.load(CellFactory.class.getResource("CellFactory_Layout.fxml")); 
     Scene scene = new Scene(root); 
     primaryStage.setScene(scene); 
     primaryStage.setTitle("CellFactory EXAMPLE"); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

Model類

public class Fruit { 
    private final SimpleStringProperty name; 
    private final SimpleIntegerProperty weight; 

    public Fruit(String name, int weight){ 
     this.name = new SimpleStringProperty(name); 
     this.weight = new SimpleIntegerProperty(weight); 
    } 

    public String getName() {return this.name.get();} 
    public void setName(String v) {this.name.set(v);} 
    public SimpleStringProperty nameProperty() {return this.name;} 

    public int getWeight() {return this.weight.get();} 
    public void setWeight(int v) {this.weight.set(v);} 
    public SimpleIntegerProperty weightProperty() {return this.weight;} 
} 

Controller類

public class CellFactory_Controller implements Initializable { 
    @FXML private TableView<Fruit> fruit_tbl; 
    @FXML private TableColumn<Fruit, String> name_cln; 
    @FXML private TableColumn<Fruit, Integer> weight_cln; 

    // array with table data 
    final ObservableList<Fruit> data = FXCollections.observableArrayList(); 

    public CellFactory_Controller() { 
     // some data 
     this.data.add(new Fruit("banana", 120)); 
     this.data.add(new Fruit("apple", 150)); 
     this.data.add(new Fruit("coconut", 500)); 
     this.data.add(new Fruit("orange", 200)); 
    } 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     this.name_cln.setCellValueFactory(new PropertyValueFactory<>("name")); 
     this.weight_cln.setCellValueFactory(new PropertyValueFactory<>("weight")); 
     this.weight_cln.setCellValueFactory(cellData -> cellData.getValue().weightProperty().asObject()); 

     // comment or uncomment to see what happen 
     /////////////////////////////////////////////////////////////////////// 
     this.weight_cln.setCellFactory(column -> new TableCell<Fruit, Integer>() { 
      @Override 
      protected void updateItem(Integer item, boolean empty) { 
       super.updateItem(item, empty); 

       if (item == null || empty) { 
        setText(null); 
        setStyle(""); 
       } else { 
        if (item < 10) { 
         setTextFill(Color.CHOCOLATE); 
        } else { 
         setTextFill(Color.BLACK); 
         setStyle(""); 
        } 
       } 
      } 
     }); 
     /////////////////////////////////////////////////////////////////////// 

     this.fruit_tbl.setItems(this.data); 
    } 
} 

FXML

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CellFactory.CellFactory_Controller"> 
    <children> 
     <TableView fx:id="fruit_tbl" layoutX="189.0" layoutY="93.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <columns> 
      <TableColumn fx:id="name_cln" prefWidth="471.0" text="FRUIT" /> 
      <TableColumn fx:id="weight_cln" prefWidth="75.0" text="WEIGHT" /> 
     </columns> 
     <columnResizePolicy> 
      <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /> 
     </columnResizePolicy> 
     </TableView> 
    </children> 
</AnchorPane> 

問: 從我的代碼示例中,我該如何使用定製單元格呈現正常(與int數據類型),以顯示我的數據?

回答

0

你忘了添加以下語句:

setText(String.valueOf(item)); 

所以,你setCellFactory()方法應該如下所示:

this.weight_cln.setCellFactory(column -> new TableCell<Fruit, Integer>() { 
     @Override 
     protected void updateItem(Integer item, boolean empty) { 
      super.updateItem(item, empty); 

      if (item == null || empty) { 
       setText(null); 
       setStyle(""); 
      } else { 
       setText(String.valueOf(item)); 
       if (item < 200) { 

        setTextFill(Color.CHOCOLATE); 


       } else { 

        setTextFill(Color.BLACK); 

       } 
      } 
     } 
    }); 
+0

非常感謝你。 –

相關問題