TableView单元格内的JavaFX形状

时间:2018-08-30 08:59:15

标签: java javafx javafx-8

我目前有一个工作表,但是我想将SimpleIntegerProperty“状态”更改为正方形。基本上,我希望所有的“ 1”都变成正方形。这是我编写的代码:

public ObservableList<PumpSites> list = FXCollections.observableArrayList(
        new PumpSites (1, "Canduman"),
        new PumpSites (1, "Cubacub"),
        new PumpSites (1, "Liloan"),
        new PumpSites (1, "Talamban"),
        new PumpSites (1, "Tisa")
        );

status.setCellValueFactory(new PropertyValueFactory<PumpSites, Integer>("status"));
ps.setCellValueFactory(new PropertyValueFactory<PumpSites, String>("ps"));
table.setItems(list);

public class PumpSites {
    private final SimpleIntegerProperty status;
    private final SimpleStringProperty ps;

    public PumpSites(Integer status, String ps){
        super();
        this.status = new SimpleIntegerProperty(status);
        this.ps = new SimpleStringProperty(ps);
    }

    public Integer getStatus() {
        return status.get();
    }

    public String getPs() {
        return ps.get();
    }

}

我该怎么做?

1 个答案:

答案 0 :(得分:-2)

IntegerProperty方法的ObjectProperty<Shape>中使用@OverrideTableCell代替updateItem(item,empty)来显示形状

这是工作代码:

public class Controller implements Initializable {

    @FXML
    private TableView<Model> tableView;
    @FXML
    private TableColumn<Model, Shape> shapeColumn;
    @FXML
    private TableColumn<Model, String> textColumn;

    @Override

    public void initialize(URL location, ResourceBundle resources) {
        shapeColumn.setCellValueFactory(data -> data.getValue().shapeProperty());
        textColumn.setCellValueFactory(data -> data.getValue().textProperty());

        shapeColumn.setCellFactory(cell -> new ShapeTableCell());

        ObservableList<Model> items = FXCollections.observableArrayList();

        items.add(new Model(new Circle(10), "Circle"));
        items.add(new Model(new Rectangle(20, 20), "Rectangle"));

        tableView.setItems(items);
    }

    private class Model {
        // Instead of shape you can use anything, which inherits from Node,
        // like an ImageView to display a specific image
        private ObjectProperty<Shape> shape;
        private StringProperty text;

        public Model(Shape shape, String text) {
            this.shape = new SimpleObjectProperty<>(shape);
            this.text = new SimpleStringProperty(text);
        }

        public Shape getShape() {
            return shape.get();
        }

        public ObjectProperty<Shape> shapeProperty() {
            return shape;
        }

        public String getText() {
            return text.get();
        }

        public StringProperty textProperty() {
            return text;
        }
    }

    private class ShapeTableCell extends TableCell<Model, Shape> {
        @Override
        protected void updateItem(Shape item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setGraphic(null);
            } else {
                setGraphic(item);
            }
        }
    }

}

提示:尝试避免使用PropertyValueFactory,而改为使用Callback。 PVF使用反射,当您有其他选择时,它永远不是最佳解决方案。