JavaFx tableview滚动错误

时间:2016-09-04 21:38:37

标签: java javafx tableview

我在代码中创建了TableView,但是当我开始向下滚动或向上滚动时,我的单元格就会消失。

但是当我访问另一个标签页并返回包含TableView单元格的标签时,会重新出现。

这是我的代码:

UserCollection collection = new UserCollection();
    collection.load();

 table.setItems(collection.getItems());
    cUserStatsId.setCellValueFactory(cellData -> cellData.getValue().getId().asObject());
    column1.setCellValueFactory(cellData -> cellData.getValue().getLevel().asObject());
    column2.setCellValueFactory(cellData -> cellData.getValue().getIsOnline());
    column3.setCellValueFactory(cellData -> cellData.getValue().getOrigin());
    column4.setCellValueFactory(cellData -> cellData.getValue().getPoints().asObject());
    column5.setCellValueFactory(cellData -> cellData.getValue().getPositionString());
    column6.setCellValueFactory(cellData -> cellData.getValue().getUsername());
    column7.setCellValueFactory(
            new Callback<TableColumn.CellDataFeatures<User, Boolean>,
                    ObservableValue<Boolean>>() {

                @Override
                public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<User, Boolean> p) {
                    return new SimpleBooleanProperty(p.getValue() != null);
                }
            });

    column7.setCellFactory(
            new Callback<TableColumn<User, Boolean>, TableCell<User, Boolean>>() {

                @Override
                public TableCell<User, Boolean> call(TableColumn<User, Boolean> p) {
                    return new ButtonCellForUserStats(userStatsTable);
                }


            });

public class ButtonCellForUserStats extends TableCell<User, Boolean> {
    final Button cellButton = new Button("☆");
    final Button editButton = new Button("✍");
    private HBox sk = new HBox(cellButton, editButton);

    ButtonCellForUserStats(final TableView tblView) {
        cellButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                User record = (User) getTableRow().getItem();
                System.out.println(record.getId().get());
            }
        });
        editButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                User record = (User) getTableRow().getItem();
                boolean buttonOkWasPressed = App.showUserStatsEditDialog(record);
                if (buttonOkWasPressed) {
                    record.save();
                }
                getTableView().refresh();
            }
        });
    }


    @Override
    protected void updateItem(Boolean t, boolean empty) {
        super.updateItem(t, empty);
        if (!empty) {
            setGraphic(sk);
            sk.setSpacing(5);
            cellButton.setPrefWidth(62.5D);
            cellButton.setPrefSize(62.5,10.0);
            cellButton.setPadding(new Insets(0,0,0,0));
            editButton.setPrefWidth(62.5D);
            editButton.setPrefSize(62.5,10.0);
            editButton.setPadding(new Insets(0,0,0,0));
            sk.setStyle("-fx-background-color: transparent; -fx-text-fill: white;");
            cellButton.setStyle("-fx-background-color: lightgrey; -fx-text-fill: black;  -fx-font-size: 20px;");
            editButton.setStyle("-fx-background-color: lightgrey; -fx-text-fill: black;  -fx-font-size: 20px;");
        } else {
            setGraphic(null);
        }
    }
}

以下屏幕截图显示了问题:

enter image description here

0 个答案:

没有答案