JavaFX TableView属性显示为空

时间:2018-10-18 16:56:32

标签: java javafx

如何使$route显示为空白(空)? SimpleIntegerProperty允许空值显示为空白,但SimpleStringProperty不允许。我真的不想使用SimpleIntegerProperty来代替它,因为如果您将数字视为字符串,则排序实际上无法正常工作。

1 个答案:

答案 0 :(得分:3)

这是一个非常简单的示例应用程序,以演示如何执行此操作。基本上,如果updateItem()的值为0,则将覆盖单元格的IntegerProperty方法以提供一个空单元格。

Item.java:

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Item {
    private final StringProperty name = new SimpleStringProperty();
    private final IntegerProperty quantity = new SimpleIntegerProperty();

    public Item(String name, int qty) {
        this.name.set(name);
        this.quantity.set(qty);
    }

    public String getName() {
        return name.get();
    }

    public StringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public int getQuantity() {
        return quantity.get();
    }

    public IntegerProperty quantityProperty() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity.set(quantity);
    }
}

Main.java:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // Simple TableView
        TableView<Item> tableView = new TableView<>();
        TableColumn<Item, String> colName = new TableColumn<>("Name");
        TableColumn<Item, Number> colQuantity = new TableColumn<>("Quantity");
        // Since IntegerProperty implements ObservableValue<Number> instead of Integer, for some reason, we need
        // to change our column definition to accept that

        // Set the cell value factories
        colName.setCellValueFactory(c -> c.getValue().nameProperty());
        colQuantity.setCellValueFactory(c -> c.getValue().quantityProperty());

        // Override the CellFactory for quantity to leave empty if value is 9
        colQuantity.setCellFactory(param -> new TableCell<Item, Number>() {
            @Override
            protected void updateItem(Number itemQuantity, boolean empty) {
                super.updateItem(itemQuantity, empty);
                if (empty || itemQuantity.equals(0)) {
                    // If the item's quantity is 0, set the cell to display nothing (null)
                    setText(null);
                } else {
                    // Otherwise, the cell should display a label with the value of the item's quantity
                    setText(itemQuantity.toString());

                }
            }
        });

        // Add the columns to the TableView
        tableView.getColumns().addAll(colName, colQuantity);

        // Populate the table with sample items
        tableView.getItems().setAll(
                new Item("Tools", 3),
                new Item("Saws", 0),
                new Item("Ruler", 2)
        );

        root.getChildren().add(tableView);

        // Show the Stage
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

结果:

screenshot

  

编辑

我将每个单元格的CellValueProperty更改为使用Item属性而不是反射。由于出于某种原因,IntegerProperty实现了ObservableValue<Number>而不是ObservableValue<Integer>,因此我也确实更改了colQuantity的定义以使用Number类型。

相关问题