-
Notifications
You must be signed in to change notification settings - Fork 830
更新 JFXTreeView #5574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
更新 JFXTreeView #5574
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,146 @@ | ||||
| /* | ||||
| * Licensed to the Apache Software Foundation (ASF) under one | ||||
| * or more contributor license agreements. See the NOTICE file | ||||
| * distributed with this work for additional information | ||||
| * regarding copyright ownership. The ASF licenses this file | ||||
| * to you under the Apache License, Version 2.0 (the | ||||
| * "License"); you may not use this file except in compliance | ||||
| * with the License. You may obtain a copy of the License at | ||||
| * | ||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||
| * | ||||
| * Unless required by applicable law or agreed to in writing, | ||||
| * software distributed under the License is distributed on an | ||||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||
| * KIND, either express or implied. See the License for the | ||||
| * specific language governing permissions and limitations | ||||
| * under the License. | ||||
| */ | ||||
|
|
||||
| package com.jfoenix.controls; | ||||
|
|
||||
| import com.jfoenix.utils.JFXNodeUtils; | ||||
| import javafx.beans.InvalidationListener; | ||||
| import javafx.beans.WeakInvalidationListener; | ||||
| import javafx.geometry.Insets; | ||||
| import javafx.scene.Node; | ||||
| import javafx.scene.control.TreeCell; | ||||
| import javafx.scene.control.TreeItem; | ||||
| import javafx.scene.layout.*; | ||||
| import javafx.scene.paint.Color; | ||||
|
|
||||
| import java.lang.ref.WeakReference; | ||||
|
|
||||
| /// JFXTreeCell is simple material design implementation of a tree cell. | ||||
| /// | ||||
| /// @author Shadi Shaheen | ||||
| /// @version 1.0 | ||||
| /// @since 2017-02-15 | ||||
| public class JFXTreeCell<T> extends TreeCell<T> { | ||||
|
|
||||
| protected JFXRippler cellRippler = new JFXRippler(this) { | ||||
| @Override | ||||
| protected Node getMask() { | ||||
| Region clip = new Region(); | ||||
| JFXNodeUtils.updateBackground(JFXTreeCell.this.getBackground(), clip); | ||||
| double width = control.getLayoutBounds().getWidth(); | ||||
| double height = control.getLayoutBounds().getHeight(); | ||||
| clip.resize(width, height); | ||||
| return clip; | ||||
| } | ||||
|
|
||||
| @Override | ||||
| protected void positionControl(Node control) { | ||||
| // do nothing | ||||
| } | ||||
| }; | ||||
| private HBox hbox; | ||||
| private final StackPane selectedPane = new StackPane(); | ||||
|
|
||||
| private final InvalidationListener treeItemGraphicInvalidationListener = observable -> updateDisplay(getItem(), | ||||
| isEmpty()); | ||||
| private final WeakInvalidationListener weakTreeItemGraphicListener = new WeakInvalidationListener( | ||||
| treeItemGraphicInvalidationListener); | ||||
|
|
||||
| private WeakReference<TreeItem<T>> treeItemRef; | ||||
|
|
||||
| public JFXTreeCell() { | ||||
| selectedPane.getStyleClass().add("selection-bar"); | ||||
| selectedPane.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY))); | ||||
| selectedPane.setPrefWidth(3); | ||||
| selectedPane.setMouseTransparent(true); | ||||
| selectedProperty().addListener((o, oldVal, newVal) -> selectedPane.setVisible(newVal ? true : false)); | ||||
|
Comment on lines
+39
to
+72
|
||||
|
|
||||
| final InvalidationListener treeItemInvalidationListener = observable -> { | ||||
| TreeItem<T> oldTreeItem = treeItemRef == null ? null : treeItemRef.get(); | ||||
| if (oldTreeItem != null) { | ||||
| oldTreeItem.graphicProperty().removeListener(weakTreeItemGraphicListener); | ||||
| } | ||||
|
|
||||
| TreeItem<T> newTreeItem = getTreeItem(); | ||||
| if (newTreeItem != null) { | ||||
| newTreeItem.graphicProperty().addListener(weakTreeItemGraphicListener); | ||||
| treeItemRef = new WeakReference<>(newTreeItem); | ||||
| } | ||||
| }; | ||||
| final WeakInvalidationListener weakTreeItemListener = new WeakInvalidationListener(treeItemInvalidationListener); | ||||
| treeItemProperty().addListener(weakTreeItemListener); | ||||
| if (getTreeItem() != null) { | ||||
| getTreeItem().graphicProperty().addListener(weakTreeItemGraphicListener); | ||||
| } | ||||
| } | ||||
|
|
||||
| @Override | ||||
| protected void layoutChildren() { | ||||
| super.layoutChildren(); | ||||
| if (!getChildren().contains(selectedPane)) { | ||||
| getChildren().add(0, cellRippler); | ||||
| cellRippler.rippler.clear(); | ||||
| getChildren().add(0, selectedPane); | ||||
| } | ||||
| cellRippler.resizeRelocate(0, 0, getWidth(), getHeight()); | ||||
| cellRippler.releaseRipple(); | ||||
|
||||
| cellRippler.releaseRipple(); |
Copilot
AI
Feb 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里把 item == null 当作空单元格处理(并在 updateItem 里把 cell 设为 mouseTransparent)会破坏“值为 null 但仍是有效条目”的场景:TreeView/ListView 允许集合/TreeItem 的 value 为 null,此时 empty 通常为 false,但该实现会让行无法交互且不显示 TreeItem 的 graphic。建议仅以 empty 判断是否空行,并对 item == null 的有效条目做正常渲染/交互(例如 setMouseTransparent(empty))。
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,48 @@ | ||||||||||
| /* | ||||||||||
| * Licensed to the Apache Software Foundation (ASF) under one | ||||||||||
| * or more contributor license agreements. See the NOTICE file | ||||||||||
| * distributed with this work for additional information | ||||||||||
| * regarding copyright ownership. The ASF licenses this file | ||||||||||
| * to you under the Apache License, Version 2.0 (the | ||||||||||
| * "License"); you may not use this file except in compliance | ||||||||||
| * with the License. You may obtain a copy of the License at | ||||||||||
| * | ||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
| * | ||||||||||
| * Unless required by applicable law or agreed to in writing, | ||||||||||
| * software distributed under the License is distributed on an | ||||||||||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||||||
| * KIND, either express or implied. See the License for the | ||||||||||
| * specific language governing permissions and limitations | ||||||||||
| * under the License. | ||||||||||
| */ | ||||||||||
|
|
||||||||||
| package com.jfoenix.controls; | ||||||||||
|
|
||||||||||
| import javafx.scene.control.TreeItem; | ||||||||||
| import javafx.scene.control.TreeView; | ||||||||||
|
|
||||||||||
| /// JFXTreeView is the material design implementation of a TreeView | ||||||||||
| /// with expand/collapse animation and selection indicator. | ||||||||||
|
Comment on lines
+25
to
+26
|
||||||||||
| /// JFXTreeView is the material design implementation of a TreeView | |
| /// with expand/collapse animation and selection indicator. | |
| /// JFXTreeView is a material design styled TreeView control | |
| /// that uses JFXTreeCell and a custom style class. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package com.jfoenix.controls.datamodels.treetable; | ||
|
|
||
| import javafx.beans.property.ObjectProperty; | ||
| import javafx.beans.property.SimpleObjectProperty; | ||
| import javafx.collections.FXCollections; | ||
| import javafx.collections.ObservableList; | ||
| import javafx.scene.control.TreeTableColumn; | ||
|
|
||
| /// data model that is used in JFXTreeTableView, it's used to implement | ||
| /// the grouping feature. | ||
| /// | ||
| /// **Note:** the data object used in JFXTreeTableView **must** extends this class | ||
| /// | ||
| /// @param <T> is the concrete object of the Tree table | ||
| /// @author Shadi Shaheen | ||
| /// @version 1.0 | ||
| /// @since 2016-03-09 | ||
| public class RecursiveTreeObject<T> { | ||
|
|
||
| /// grouped children objects | ||
| private ObservableList<T> children = FXCollections.observableArrayList(); | ||
|
|
||
| public ObservableList<T> getChildren() { | ||
| return children; | ||
| } | ||
|
|
||
| public void setChildren(ObservableList<T> children) { | ||
| this.children = children; | ||
| } | ||
|
|
||
| /// Whether or not the object is grouped by a specified tree table column | ||
| ObjectProperty<TreeTableColumn<T, ?>> groupedColumn = new SimpleObjectProperty<>(); | ||
|
|
||
| public final ObjectProperty<TreeTableColumn<T, ?>> groupedColumnProperty() { | ||
| return this.groupedColumn; | ||
| } | ||
|
|
||
| public final TreeTableColumn<T, ?> getGroupedColumn() { | ||
| return this.groupedColumnProperty().get(); | ||
| } | ||
|
|
||
| public final void setGroupedColumn(final TreeTableColumn<T, ?> groupedColumn) { | ||
| this.groupedColumnProperty().set(groupedColumn); | ||
| } | ||
|
|
||
| /// the value that must be shown when grouped | ||
| ObjectProperty<Object> groupedValue = new SimpleObjectProperty<>(); | ||
|
|
||
| public final ObjectProperty<Object> groupedValueProperty() { | ||
| return this.groupedValue; | ||
| } | ||
|
|
||
| public final Object getGroupedValue() { | ||
| return this.groupedValueProperty().get(); | ||
| } | ||
|
|
||
| public final void setGroupedValue(final Object groupedValue) { | ||
| this.groupedValueProperty().set(groupedValue); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
selectedPane背景色被硬编码为Color.RED,这会绕过主题/CSS,且与现有 JFoenix 控件普遍通过样式表定制的方式不一致。建议移除代码里的 Background 设置,只保留 style class(例如selection-bar),并在 CSS 中定义颜色/宽度等样式。