【发布时间】:2015-04-18 11:55:27
【问题描述】:
环境:JDK 7u75、Windows 8.1 x64、JavaFX2.2
示例代码:
public class TreeViewSample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws MalformedURLException {
primaryStage.setTitle("Tree View Sample");
TreeItem<String> rootItem = new TreeItem<String>("RootNode");
for (int i = 1; i < 5; i++) {
TreeItem<String> item = new TreeItem<String>("SubNode" + i);
rootItem.getChildren().add(item);
item.getChildren().add(new TreeItem<String>("SubSubNode" + i + "" + i));
}
rootItem.getChildren().add(new TreeItem<String>("SecondRootNode"));
TreeView<String> tree = new TreeView<String>(rootItem);
StackPane root = new StackPane();
root.getChildren().add(tree);
Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().add((new File("../css/styletest.css").toURI()).toURL().toString());
primaryStage.setScene(scene);
primaryStage.show();
}
}
CSS:
.tree-cell {
-fx-skin: "com.sun.javafx.scene.control.skin.TreeCellSkin";
-fx-background-color: -fx-control-inner-background;
-fx-padding: 0.25em; /* 3 */
-fx-text-fill: -fx-text-inner-color;
-fx-indent: 10;
}
.tree-cell .label {
-fx-padding: 0.0em 0.0em 0.0em 0.25em; /* 0 0 0 3 */
}
.tree-cell .tree-disclosure-node {
-fx-padding: 4 2 4 8;
-fx-background-color: transparent;
}
.tree-cell .tree-disclosure-node .arrow {
-fx-background-color: -fx-mark-color;
-fx-padding: 0.333333em; /* 4 */
-fx-shape: "M 0 -4 L 8 0 L 0 4 z";
}
我们需要删除所有节点的所有空格和箭头。 修改后的 CSS:
.tree-cell {
-fx-skin: "com.sun.javafx.scene.control.skin.TreeCellSkin";
-fx-background-color: -fx-control-inner-background;
-fx-padding: 0px;
-fx-text-fill: -fx-text-inner-color;
-fx-indent: 0px;
}
.tree-cell .label {
-fx-padding: 0.0em 0.0em 0.0em 0.0em;
}
.tree-cell .tree-disclosure-node {
-fx-padding: 0px;
-fx-background-color: transparent;
}
.tree-cell .tree-disclosure-node .arrow {
-fx-background-color: -fx-mark-color;
-fx-padding: 0.0em;
}
如您所见,除叶子外的所有节点都没有填充和缩进。
还有一个问题 - 如何删除\修改此填充?
【问题讨论】:
-
@jmtalarn 这是java,不是html
-
@DarkMental(和 OP)想知道为什么你想要一棵看起来不像树的树?
-
我认为不可能完全删除叶节点的缩进。查看
TreeCellSkin.layoutChildren的源代码,有一个填充(disclosureWidth)被考虑在内。此填充是最大公开宽度,如果存在,否则它是硬编码的默认值18。我相信这可以防止删除左侧的剩余空间,尽管我不完全理解代码。由于在不是叶子时进行的计算,这似乎不会影响非叶子节点。 @kleopatra 你对我的回答提出质疑是对的,因为它确实不起作用。