JavaFX 8自定义RowFactory ContextMenu背景图像不完全可见

时间:2014-04-17 13:13:44

标签: css javafx contextmenu menuitem

我已设置以下自定义行工厂:

treeTblViewFiles.setRowFactory(new Callback<TreeTableView<FileModel>, TreeTableRow<FileModel>>() {

            @Override
            public TreeTableRow<FileModel> call(TreeTableView<FileModel> treeTableView) {
                final TreeTableRow<FileModel> row = new TreeTableRow<>();
                final ContextMenu rowMenu = new ContextMenu();
                MenuItem removeItem = new MenuItem("Remove");
                removeItem.setOnAction(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent t) {
                        int currentPlaylistIndex = m_playlists.indexOf(row.getTreeItem().getParent().getValue());
                        boolean itemRemoved = false;
                        if (row.getItem().getClass().equals(Song.class)) {
                            itemRemoved = m_playlists.get(currentPlaylistIndex).getSongs().remove(row.getItem());
                            m_playlists.get(currentPlaylistIndex).updatePlaylist((Song) row.getItem());
                        } else if (row.getItem().getClass().equals(Playlist.class)) {
                            itemRemoved = m_playlists.remove(row.getTreeItem().getValue());
                        }
                        TreeItem<FileModel> treeItem = row.getTreeItem();
                        // may need to check treeItem.getParent() is not null:
                        treeItem.getParent().getChildren().remove(treeItem);
                        treeTblViewFiles.getSelectionModel().clearSelection();
                        if (MyApp.DEBUG) {
                            System.out.println(m_playlists.size());
                            if (currentPlaylistIndex > -1) {
                                System.out.println(m_playlists.get(currentPlaylistIndex).getSongs().size());
                            }
                        }
                    }
                });
                rowMenu.getItems().add(removeItem);
                row.contextMenuProperty().bind(Bindings.when(Bindings.isNotNull(row.itemProperty()))
                        .then(rowMenu)
                        .otherwise((ContextMenu) null));
                return row;
            }

        });

在我的CSS中我有:

/******************
 * ContextMenu
 ******************/
.context-menu{
        -fx-background-color: transparent;
}
.context-menu .menu-item{
    -fx-background-image: url("styles/cm_bg.png");
    -fx-background-repeat: no-repeat;
    -fx-background-position: center;
        -fx-background-color: transparent;
}
.context-menu .menu-item .label{
        -fx-text-fill: #ababab;
    -fx-font-weight: normal;
    -fx-font-size: 12pt;
}

问题在于它不显示整个图像(220x40),而只显示适合“删除”字符串的宽度。最右边的部分被削减了。

编辑: 简化版(对于Label上的contextMenu,结果相同):

ContextMenu cm = new ContextMenu();
MenuItem mi = new MenuItem("Test");
cm.getItems().addAll(mi);
lblUploader.setContextMenu(cm);

为什么会发生这种情况以及如何解决?

提前致谢。

1 个答案:

答案 0 :(得分:0)

使用以下.css解决问题:

.context-menu {
    -fx-background-color: transparent;  
    -fx-min-width: <image_width>;
    -fx-min-height: <image_height>; 
}

请记住设置首选(或最小)尺寸。