TableCell not applied consistently

旧城冷巷雨未停 提交于 2020-01-06 14:54:05

问题


When running the program below, the TableCell is not applied (the numbers are left aligned instead of right aligned). Clicking on the "Change Values" button corrects the alignment.

Using jdk1.8.0-ea-b114

Am I missing anything obvious or is it a bug?

import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class TestFX extends Application {

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

    @Override
    public void start(final Stage stage) throws Exception {
        MyTable table = new MyTable();
        table.table.getItems().addAll(Arrays.asList(new Item(0), new Item(1), new Item(-1)));

        Scene scene = new Scene(table);

        stage.setScene(scene);
        stage.show();
    }

    static class MyTable extends VBox {

        private final Button button = new Button("Change Values");
        private final TableView<Item> table = new TableView<>();

        MyTable() {
            super();
            TableColumn<Item, Double> value = new TableColumn<>("Value");
            value.setCellValueFactory(new PropertyValueFactory<>("value"));
            value.setCellFactory((p) -> new NumberTableCell<>());

            table.getColumns().add(value);

            button.setOnMouseClicked(this::changeValues);
            getChildren().addAll(button, table);
        }

        public void changeValues(MouseEvent e) {
            for (Item i : table.getItems()) {
                i.value.set(i.value.get() + 1);
            }
        }
    }

    public static class Item {
        private final DoubleProperty value;
        public Item(double value) { this.value = new SimpleDoubleProperty(value); }
        public DoubleProperty valueProperty() { return value; }
    }

    static class NumberTableCell<T> extends TableCell<T, Double> {
        @Override
        protected void updateItem(Double item, boolean empty) {
            super.updateItem(item, empty);
            if (empty || item == null) {
                setText(null);
                setTextFill(null);
            } else {
                setText(String.valueOf(item));
                setAlignment(Pos.CENTER_RIGHT);
            }
        }
    }
}

回答1:


give refernce to object.your code is okey.

try this...i hope its work for you.

https://stackoverflow.com/questions/17264929/how-to-change-the-row-color-based-on-data-that-can-change-in-a-tableview




回答2:


It is a bug which is on jira: https://javafx-jira.kenai.com/browse/RT-34237



来源:https://stackoverflow.com/questions/19918636/tablecell-not-applied-consistently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!