gpt4 book ai didi

JavaFX 拆分菜单按钮箭头触发事件

转载 作者:行者123 更新时间:2023-11-30 03:40:32 26 4
gpt4 key购买 nike

我有一个 SplitMenuButton,但我似乎找不到在用户单击按钮旁边的箭头时触发事件的方法。

我希望在单击下拉箭头时下拉列表中填充数据库中的项目。

我不确定哪个事件可以做到这一点,我也找不到任何相关信息。

最佳答案

简短回答:使用 showing property 注册一个监听器.

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitMenuButton;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class SplitMenuButtonTest extends Application {

@Override
public void start(Stage primaryStage) {

IntegerProperty count = new SimpleIntegerProperty();

SplitMenuButton splitMenuButton = new SplitMenuButton();
splitMenuButton.setText("Action");
splitMenuButton.showingProperty().addListener((obs, wasShowing, isNowShowing) -> {
if (isNowShowing) {
int c = count.get() + 1;
count.set(c);
splitMenuButton.getItems().clear();
for (int choice = 1; choice <= 3; choice++) {
MenuItem mi = new MenuItem("Choice "+choice+" (" + c + ")");
splitMenuButton.getItems().add(mi);
}
}
});

BorderPane root = new BorderPane(null, splitMenuButton, null, null, null);
primaryStage.setScene(new Scene(root, 350, 150));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

顺便说一句,我不确定这是否是一个真正的好主意。数据库连接通常是长时间运行的进程(即足够长以在 UI 环境中在视觉上引人注目)。如果您在 FX 应用程序线程上运行此代码,那么您将在检索数据时阻止 UI 执行任何操作,并且这也是在用户刚刚尝试执行某些操作时发生的。当然,如果您将其作为后台任务运行,那么菜单将弹出以前的数据,然后在数据下载后更新。我建议在用户请求之前找到一种方法来填充它。

关于JavaFX 拆分菜单按钮箭头触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26895534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com