TableCell未始终如一地应用 [英] TableCell not applied consistently

查看:146
本文介绍了TableCell未始终如一地应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行下面的程序时,不会应用TableCell(数字左对齐而不是右对齐)。单击更改值按钮可更正对齐方式。

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.

使用jdk1.8.0-ea-b114

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);
            }
        }
    }
}


推荐答案

这是一个关于jira的错误: https:/ /javafx-jira.kenai.com/browse/RT-34237

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

这篇关于TableCell未始终如一地应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆