JavaFX 8 : shifting table cells when using borders for table row

标签 java css javafx treetableview

我想在我的 TreeTableView 中标记一些行带有红色边框,但我遇到了表格单元格从各自的列移开的问题。

视觉上看起来像这样:

http://i.imgur.com/KBK3hvM.png

样式.css:

.style {
    -fx-border-style: solid line-join round ;
    -fx-border-color: ...;
}

对于树中的每一列,它似乎都从右边移动了一点,看起来是边框的宽度(默认为 1px)。只有 2 列不是问题,但最终应用程序应该包含一打列。

我可以将边框的插入设置在单元格的外部,这样可以修复偏移,但是你再也看不到侧边框了,这看起来也很奇怪。

我猜想为一行设置样式只是为了方便引擎为每个单元格设置它。

有没有办法阻止 TreeTableCells 移动?也许为单元格设置单独的样式而不是为整行设置样式?

最佳答案

假设 .style 应用于 TreeTableRow:

.style {
    -fx-background-color: red, -fx-background ;
    -fx-background-insets: 0, 1 ;
}

我通常发现我必须深入研究默认样式表的源代码才能弄清楚这些。您可能想要弄乱插图并实现某种逻辑以防止应用样式类的相邻行出现双边框。

这是一个 SSCCE:

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableRow;
import javafx.scene.control.TreeTableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class StyledTreeTableView extends Application {

    private static final int MAX_VALUE = 1000 ;

    @Override
    public void start(Stage primaryStage) {
        TreeTableView<Item> treeTable = new TreeTableView<>();
        treeTable.setRoot(createTreeItem(1));

        treeTable.setRowFactory(ttv -> new TreeTableRow<Item>() {
            @Override
            public void updateItem(Item item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setText(null);
                    getStyleClass().remove("highlight");
                } else {
                    setText(item.toString());
                    if (item.getValue() % 10 == 3 || item.getValue() % 10 == 4) {
                        if (! getStyleClass().contains("highlight")) {
                            getStyleClass().add("highlight");
                        }
                    } else {
                        getStyleClass().remove("highlight");
                    }
                }
            }
        });

        TreeTableColumn<Item, String> nameCol = new TreeTableColumn<>("Item");
        nameCol.setCellValueFactory(cellData -> cellData.getValue().getValue().nameProperty());
        treeTable.getColumns().add(nameCol);

        for (int colIndex = 1 ; colIndex < 10 ; colIndex++) {
            TreeTableColumn<Item, Number> valueCol = new TreeTableColumn<>("Value * "+colIndex);
            final int multiplier = colIndex ;
            valueCol.setCellValueFactory(cellData -> cellData.getValue().getValue().valueProperty().multiply(multiplier));
            treeTable.getColumns().add(valueCol);
        }

        BorderPane root = new BorderPane(treeTable);
        Scene scene = new Scene(root, 600, 600);
        scene.getStylesheets().add("styled-tree-table.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private TreeItem<Item> createTreeItem(int value) {
        Item item = new Item("Item "+ value, value);
        TreeItem<Item> treeItem = new TreeItem<>(item);
        if (value < MAX_VALUE) {
            for (int i = 0 ; i < 10; i++) {
                treeItem.getChildren().add(createTreeItem(value * 10 + i));
            }
        }
        return treeItem ;
    }

    public static class Item {
        private final StringProperty name = new SimpleStringProperty();
        private final IntegerProperty value = new SimpleIntegerProperty();

        public Item(String name, int value) {
            setName(name);
            setValue(value);
        }

        public final StringProperty nameProperty() {
            return this.name;
        }

        public final java.lang.String getName() {
            return this.nameProperty().get();
        }

        public final void setName(final java.lang.String name) {
            this.nameProperty().set(name);
        }

        public final IntegerProperty valueProperty() {
            return this.value;
        }

        public final int getValue() {
            return this.valueProperty().get();
        }

        public final void setValue(final int value) {
            this.valueProperty().set(value);
        }


    }

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

使用样式表:

.highlight {
    -fx-background-color: red, -fx-background ;
    -fx-background-insets: 0, 1 ;
}

关于JavaFX 8 : shifting table cells when using borders for table row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779843/

相关文章:

JavaFx 更改窗口,无需新的 Controller 构造函数

javafx - 如何获取 TableView 中 ComboBoxTableCell 中选定的值

java - 运行Java jar应用程序时如何在系统任务栏中重新运行用javafx编写的用户界面

java - 在android中读取Json数组

html - Z-Index 不工作 - 替代技术或修复

javascript - HTML-CSS 选项的背景颜色

javascript - X-UA-Compatible - 使用已安装的 IE 版本渲染页面

Java Swing EDT : How to know which threads are waiting the execution of an EventDisplay via SwingUtilities. invokeAndWait?

java - 如何从java中的组合框中获取整数值?

java - 代码在每一行打印整个字符串?