JavaFX:尝试使用 cellfactory 时,数据未显示在表视图的列中

JavaFX : data isn't displayed in tableview's column when trying to use cellfactory

我想在 tableview 的列中使用自定义呈现显示数据。我试着让 this tuto 适应我的需要。

问题: 在下面的代码示例中,当我使用 setCellFactory() 方法时,相应列中的数据没有显示。

您可以评论或取消评论分隔部分以查看控制器中发生的情况class。

主要class

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);
    }
}

型号class

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;}
}

控制器class

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 数据类型)来显示我的数据?

您忘记添加以下语句:

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);

                }
            }
        }
    });