TreeTableView行动态样式格式设置失败

时间:2018-12-08 15:46:43

标签: java css javafx treetableview

在这里许多人的帮助下,我找到了一种使用rowFactory动态更改某些行的样式的方法。也许我走得太远了,以为扩展行的根项不仅具有不同的文本和背景颜色,而且具有不同的大小也很酷。

我的要求是,在扩展根目录时,我不想看到它,但我仍然希望能够单击显示节点以再次将其折叠。此外,某些项目是单个的,将仅在没有公开节点的情况下出现。换句话说,带有子项的rootItem仅代表折叠时隐藏的内容(例如其数量的平均值或总数)

在向下滚动表时操作(折叠/展开)行时出现问题。要重现:运行代码,展开胡萝卜,向下滚动,展开土豆。

我试图添加和删除自定义styleClass,而不是使用伪类,以防万一,但这似乎是在做同样的事情。

问题:有解决方法吗?我在rowFactory中做错了吗?

后续问题:我对Javafx可以做什么抱有太大的抱负吗?我是否应该在游戏UI中使用其他类似OpenGL的东西?

这是一个简单的示例:

public class RowStyling extends Application {

        @Override
        public void start(Stage primaryStage) {

                BorderPane rootPane = new BorderPane();
                Scene scene = new Scene(rootPane,200,150);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                primaryStage.setScene(scene);
                rootPane.setCenter(createTable());
                primaryStage.show();
        }

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

        @SuppressWarnings("unchecked")
        private static TreeTableView<Product> createTable() {

            TreeTableView<Product> table = new TreeTableView<>();
            TreeItem<Product> rootItem = new TreeItem<>(new Product("root",0));

            TreeItem<Product> carrots1Item = new TreeItem<Product>(new Product("carrots", 2));
            TreeItem<Product> carrots2Item = new TreeItem<Product>(new Product("carrots", 4));
            TreeItem<Product> carrots3Item = new TreeItem<Product>(new Product("carrots", 0));

            TreeItem<Product> averageCarrotsItem = new TreeItem<Product>(new Product("carrots", 2));
            averageCarrotsItem.getChildren().addAll(carrots1Item,carrots2Item,carrots3Item);

            TreeItem<Product> potatoes1Item = new TreeItem<Product>(new Product("potatoes", 0));
            TreeItem<Product> potatoes2Item = new TreeItem<Product>(new Product("potatoes", 1));
            TreeItem<Product> potatoes3Item = new TreeItem<Product>(new Product("potatoes", 2));

            TreeItem<Product> averagePotatoesItem = new TreeItem<Product>(new Product("potatoes", 1));
            averagePotatoesItem.getChildren().addAll(potatoes1Item,potatoes2Item,potatoes3Item);

            TreeItem<Product> singleVegy1Item = new TreeItem<Product>(new Product("singleVegy1", 3));
            TreeItem<Product> singleVegy2Item = new TreeItem<Product>(new Product("singleVegy2", 5));

            rootItem.getChildren().addAll(averageCarrotsItem,singleVegy1Item,singleVegy2Item,averagePotatoesItem);

            table.setRoot(rootItem);
            table.setShowRoot(false);

            TreeTableColumn<Product, String> nameCol = new TreeTableColumn<>();
            TreeTableColumn<Product, Number> quantityCol = new TreeTableColumn<>();
            nameCol.setCellValueFactory(new TreeItemPropertyValueFactory<>("name"));
            nameCol.setPrefWidth(120);
            quantityCol.setCellValueFactory(new TreeItemPropertyValueFactory<>("quantity"));

            //row factory to toggle a pseudoclass when the row has multipleChildren
            table.setRowFactory(t-> new TreeTableRow<Product>(){
                @Override
                public void updateItem(Product prod, boolean empty) {
                    super.updateItem(prod, empty);      
                    if(prod==null||empty) {
                        setText(null);
                        setGraphic(null);
                        pseudoClassStateChanged(PseudoClass.getPseudoClass("multipleChildren"), false);
                    }else {
                        boolean multipleChildren = getTreeItem().getChildren().size()>1;
                        pseudoClassStateChanged(PseudoClass.getPseudoClass("multipleChildren"), multipleChildren);
                    }
                }
            });

            table.getColumns().addAll(nameCol,quantityCol);
            return table;
        }
    }

## CSS ##

    .tree-table-row-cell:multipleChildren .tree-table-cell{
        -fx-font-size: 14;
        -fx-text-fill: grey;
        -fx-background-color:   lightGrey ;
    }
    .tree-table-row-cell:expanded:multipleChildren .tree-table-cell{
        -fx-text-fill: lightGrey;
        -fx-background-color:   lightGrey ;
        -fx-cell-size: 0.30em;
    }
    .tree-table-row-cell:expanded:multipleChildren{
        -fx-cell-size: 0.30em;
    }

## Model ##
    public class Product{
        private StringProperty name;
        private DoubleProperty quantity;

        public Product(String name, double quantity) {
            this.name = new SimpleStringProperty(name);
            this.quantity = new SimpleDoubleProperty(quantity);
        }

        public StringProperty nameProperty() {
            return name;
        }

        public DoubleProperty quantityProperty() {
            return quantity;
        }
    }

enter image description here

0 个答案:

没有答案