1

With the help of many here I figured out a way to dynamically change the style of certain rows with a rowFactory. Maybe I when too far and thought it would be cool if the root item of expanded rows not only had different text and background colour but also a different size.

My requirement is that I don't want to see the rootItems while it is expanded but I still want to be able to click on the disclosure node to collapse it again. Furthermore, some items are single and will just appear without a disclosure node. In other words, the rootItems with children are only a representation of what is hidden when collapsed (for example the average or total of their quantity)

The problems appear when manipulating (collapse/expand) the rows while the table is scrolled down. To reproduce: run the code, expand carrots, scroll down, expand potatoes.

I tried to add and remove a custom styleClass instead of using a pseudoclass just in case that was the issue but it seems to be doing the exact same thing.

Questions: Is there a fix? Did I do something wrong in my rowFactory?

Followup question : Am I too ambitious with what javafx can do? Should I be using something else like OpenGL for my game UI?

Here is a simple example:

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

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
Ram
  • 115
  • 10
  • Sorry for the pastebin, I don't know why I was not able to post the question with code in it. It was showing properly formatted in the preview but was still showing an error. – Ram Dec 08 '18 at 15:48

0 Answers0