TreeView扩展/折叠奇怪的行为

时间:2016-05-26 23:00:11

标签: java css javafx treeview treecell

我为TreeView<Playlist>建模,使得有子节点的节点有一个Folder图标作为图形,而其他节点有一个播放列表图标。几乎的样式,但在展开/折叠节点时,图形图标会被破坏。

单击某个节点时,它始终显示正确的样式,但在展开/折叠时,或添加新节点时,TreeView的行为如下:

treeview-strange-behaviour

以下是代码的一部分,其中TreeCell

中定义了TreeView
public class PlaylistTreeView extends TreeView<Playlist> {

    public PlaylistTreeView() {
    ...

    setShowRoot(false);
    setCellFactory(treeView -> new PlaylistTreeCell());

    ...
    }

    private class PlaylistTreeCell extends TreeCell<Playlist> {

        @Override
        public void updateItem(Playlist p, boolean empty) {
            super.updateItem(p, empty);
            if(p == null || empty) {
                textProperty().unbind();
                setText("");
                setGraphic(null);
            }
            else {
                if(p.isFolder())
                    setId("playlist-folder-tree-cell");
                else
                    setId("playlist-tree-cell");
                textProperty().bind(p.nameProperty());
            }
        }
    }
}

这些是css影响这些组件的唯一样式:

.tree-cell, .list-cell {
    -fx-background-color: rgb(243, 243, 243);
}

#playlist-tree-cell {
    -fx-graphic: url('../icons/playlist-black-icon.png');
}

#playlist-tree-cell:selected {
    -fx-graphic: url('../icons/playlist-white-icon.png');
}

#playlist-folder-tree-cell {
    -fx-graphic: url('../icons/playlist-folder-black-icon.png');    
}

#playlist-folder-tree-cell:selected {
    -fx-graphic: url('../icons/playlist-folder-white-icon.png');
}

.list-cell:selected, .tree-cell:selected {  
    -fx-background-color: black;
    -fx-text-fill: white;
}

我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

我使用css伪类解决了它。

public class PlaylistTreeView extends TreeView<Playlist> {

    private PseudoClass playlist = PseudoClass.getPseudoClass("playlist");
    private PseudoClass playlistSelected = PseudoClass.getPseudoClass("playlist-selected");
    private PseudoClass folder = PseudoClass.getPseudoClass("folder");
    private PseudoClass folderSelected = PseudoClass.getPseudoClass("folder-selected");

    public PlaylistTreeView() {
        setShowRoot(false);
        setCellFactory(treeView -> new PlaylistTreeCell());
    }

    private class PlaylistTreeCell extends TreeCell<Playlist> {

        public PlaylistTreeCell() {
            super();

            ChangeListener<Boolean> isFolderListener = (obs, oldPlaylist, newPlaylist) -> {
                boolean isFolder = newPlaylist.booleanValue();
                boolean isSelected = selectedProperty().get();
                updatePseudoClassesStates(isFolder, isSelected);
            };

            ChangeListener<Boolean> isSelectedListener = (obs, oldValue, newValue) -> {
                boolean isFolder = itemProperty().getValue().isFolder();
                boolean isSelected = newValue.booleanValue();
                updatePseudoClassesStates(isFolder, isSelected);
            };

            itemProperty().addListener((obs, oldPlaylist, newPlaylist) -> {

                if(oldPlaylist != null) {
                    textProperty().unbind();
                    setText("");
                    oldPlaylist.folderProperty().removeListener(isFolderListener);
                    selectedProperty().removeListener(isSelectedListener);
                }

                if(newPlaylist != null) {
                    textProperty().bind(newPlaylist.nameProperty());
                    newPlaylist.folderProperty().addListener(isFolderListener);
                    selectedProperty().addListener(isSelectedListener);

                    updatePseudoClassesStates(newPlaylist.isFolder(), selectedProperty().get());
                }
                else {
                    pseudoClassStateChanged(folder, false);
                    pseudoClassStateChanged(folderSelected, false);
                    pseudoClassStateChanged(playlist, false);
                    pseudoClassStateChanged(playlistSelected, false);
                }
            });
        }

        private void updatePseudoClassesStates(boolean isFolder, boolean isSelected) {
            pseudoClassStateChanged(folder, isFolder && !isSelected);
            pseudoClassStateChanged(folderSelected, isFolder && isSelected);
            pseudoClassStateChanged(playlist, !isFolder && !isSelected);
            pseudoClassStateChanged(playlistSelected, !isFolder && isSelected);
        }
}

style.css档案:

.tree-cell, .list-cell {
    -fx-background-color: rgb(243, 243, 243);
}

.tree-cell:empty {
    -fx-graphic: none;
}

.tree-cell:playlist {
    -fx-graphic: url('../playlist-black-icon.png');
}

.tree-cell:playlist-selected {
    -fx-graphic: url('../playlist-white-icon.png');
}

.tree-cell:folder {
    -fx-graphic: url('../playlist-folder-black-icon.png');
}

.tree-cell:folder-selected {
    -fx-graphic: url('../playlist-folder-white-icon.png');
}

.tree-cell:playlist-selected, .tree-cell:folder-selected {
    -fx-background-color: black;
    -fx-text-fill: white;
}

enter image description here