use*_*443 5 javafx contextmenu scrollable
我遇到的情况是,上下文菜单可能会添加数百个菜单项。默认情况下,上下文菜单将在弹出窗口的顶部/底部显示滚动按钮,但是它将占据整个屏幕的高度。我尝试设置maxHeight和prefHeight值,但这没有效果。
理想情况下,我想在顶部和底部显示一个滚动条,而不是滚动按钮(即,将其放在滚动窗格中)。
这是我当前拥有的代码片段:
ContextMenu menu = new ContextMenu();
menu.setMaxHeight(500);
menu.setPrefHeight(500);
for(TabFX<T> tab : overflowTabs) {
MenuItem item = new MenuItem(tab.getTabName());
if (tab.getGraphic() != null) item.setGraphic(tab.getGraphic());
item.setOnAction(e -> {
select(tab);
layoutTabs();
});
menu.getItems().add(item);
}
Point2D p = overflowBtn.localToScreen(0, overflowBtn.getHeight());
menu.show(overflowBtn, p.getX(), p.getY());
Run Code Online (Sandbox Code Playgroud)
有没有解决方法?
小智 3
我遇到了同样的问题,我通过使用javafx.stage.Popup带有 a 的类来解决它ScrollPane,其中包含 aVBox并保存菜单项。
Popup popup = new Popup();
VBox vBox = new VBox();
for (int i = 0; i < 4; i++)
{
Button item = new Button("item " + i);
item.setOnAction(...);
vBox.getChildren().add(item);
}
ScrollPane scrollPane = new ScrollPane(vBox);
scrollPane.setMaxHeight(500);//Adjust max height of the popup here
scrollPane.setMaxWidth(200);//Adjust max width of the popup here
popup.getContent().add(scrollPane);
popup.show(rootWindow);
Run Code Online (Sandbox Code Playgroud)
注意:由于不能将 aMenuItem放入 a 中,因此我在示例中VBox使用了 a ,因为它有方法。但你可以使用任何你喜欢的东西:)ButtonsetOnAction(...)