tableview的单元格内的JavaFX形状

问题描述 投票:-1回答:1

我目前有一个工作表,但我想将SimpleIntegerProperty“status”更改为正方形。基本上,我希望所有的“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();
    }

}

我怎样才能做到这一点?

java javafx javafx-8
1个回答
-2
投票

而不是IntegerPropertyObjectProperty<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使用反射,当你有另一种反射时,反射永远不是最好的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.