如何通过程序触发按钮上的Action事件?

本文介绍如何在ADF中通过CommandLink触发CommandButton的事件,包括直接触发、通过客户端监听器触发等方式,并提供代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发环境:JDevloper 11.1.2.1.0+ Oracle Database 10g Express Edition 10.2.0.1。

这个问题来自于https://cn.forums.oracle.com/forums/thread.jspa?threadID=2373947。
我觉得这个问题比较典型,实际使用中可能会用到,因此做了个实验总结了一下。

我在《 传递参数给TaskFlow 》项目的基础上,修改了department.jsf页面代码,增加了一个按钮以及相应的Managed Bean。
其中,CommandLink的代码如下:
<af:commandLink text="#{row.DepartmentId}" id="cl1" action="#{myBackingBean.selectLink_action}">
<af:setPropertyListener from="#{row.DepartmentId}" to="#{requestScope.deptId}" type="action"/>
<af:setPropertyListener from="#{row.DepartmentId}" to="#{pageFlowScope.pf_deptId}" type="action"/>
</af:commandLink>

CommandButton的代码如下:
<af:commandButton text="Select Department" id="cb1" actionListener="#{myBackingBean.selectButton_actionListener}"/>

场景1:点击CommandButton,然后触发CommandLink的Action事件,即相当于点击了CommandLink。
(1)CommandLink上的Action代码如下:
public String selectLink_action() {
FacesContext fctx = FacesContext.getCurrentInstance();
UIViewRoot root = fctx.getViewRoot();
RichCommandButton rcb = (RichCommandButton)root.findComponent("cb1");
System.out.println("#################### " + rcb);

ADFContext ac = ADFContext.getCurrent();
Map requestScope = ac.getRequestScope();
Map pageFlowScope = ac.getPageFlowScope();
requestScope.put("deptId", JSFUtils.getManagedBeanValue("bindings.DepartmentId.inputValue"));
pageFlowScope.put("pf_deptId", JSFUtils.getManagedBeanValue("bindings.DepartmentId.inputValue"));

return "toEmployees";
}
注意,之所以在这里要给RequestScope和PageFlowScope变量重新赋值,是因为通过程序无法触发setPropertyListener。

(2)CommandButton上的Action代码如下:
public void selectButton_actionListener(ActionEvent evt) {
RichCommandButton rcb = (RichCommandButton)evt.getSource();
RichCommandLink rcl = (RichCommandLink)rcb.findComponent("t1:cl1");
ActionEvent actionEvent = new ActionEvent(rcl);
actionEvent.queue();
}
其原理是:找到CommandLink对象,为其创建一个ActionEvent事件,并压入事件队列。
这样就相当于触发了CommandLink的Action事件。

场景2:点击CommandLink,然后触发CommandButton的Action事件,即相当于点击了CommandButton。
(1)CommandLink上的Action代码如下:
public String selectLink_action() {
FacesContext fctx = FacesContext.getCurrentInstance();
UIViewRoot root = fctx.getViewRoot();
RichCommandButton rcb = (RichCommandButton)root.findComponent("cb1");
ActionEvent actionEvent = new ActionEvent(rcb);
actionEvent.queue();
return "toEmployees";
}
其原理是:找到CommandButton对象,为其创建一个ActionEvent事件,并压入事件队列。
这样就相当于触发了CommandButton的Action事件。

(2)CommandButton上的Action代码如下: 
public void selectButton_actionListener(ActionEvent evt) {
ADFContext ac = ADFContext.getCurrent();
Map requestScope = ac.getRequestScope();
System.out.println("#################### " + requestScope.get("deptId"));
Map pageFlowScope = ac.getPageFlowScope();
System.out.println("#################### " + pageFlowScope.get("pf_deptId"));
}
因为在CommandLink中已经使用SetPropertyListener为RequestScope和PageFlowScope变量赋值了,这里只是获取这些变量,并打印出来。

场景3:点击CommandLink,然后通过ClientListener触发CommandButton的Action事件,即相当于点击了CommandButton。 
场景3与场景2是一样的,不过实现方式不同,场景3是通过ClientListener调用JavaScript函数实现的。
(1)修改department.jsf页面,为CommandLink增加ClientListener及相应的JavaScript函数。
JavaScript 函数:
<af:resource type="javascript">
function clickOnCommandLink(evt) {
var cmdButton = AdfPage.PAGE.findComponentByAbsoluteId("cb1");
AdfActionEvent.queue(cmdButton, cmdButton.getPartialSubmit());

</af:resource>
其原理是:找到CommandButton对象,然后为其压入一个ActionEvent到事件队列中。

CommandLink代码:
<af:commandLink text="#{row.DepartmentId}" id="cl1" action="#{myBackingBean.selectLink_action}">
<af:setPropertyListener from="#{row.DepartmentId}" to="#{requestScope.deptId}" type="action"/>
<af:setPropertyListener from="#{row.DepartmentId}" to="#{pageFlowScope.pf_deptId}"
type="action"/>
<af:clientListener type="click" method="clickOnCommandLink"/>
</af:commandLink>

(2)Managed Bean代码修改如下:
CommandLink的Action事件,因为已经通过JavaScript函数触发CommandButton上的事件了,这里只要返回导航String就可以了。
public String selectLink_action() {
return "toEmployees";
}
CommandButton的Action Listener的代码不变:
public void selectButton_actionListener(ActionEvent evt) {
ADFContext ac = ADFContext.getCurrent();
Map requestScope = ac.getRequestScope();
System.out.println("#################### " + requestScope.get("deptId"));
Map pageFlowScope = ac.getPageFlowScope();
System.out.println("#################### " + pageFlowScope.get("pf_deptId"));
}

Project 下载: ADF_TaskFlow_InputParam(Trigger ActionEvent).7z

参考文献:

1. http://jjzheng.blogspot.com/2011/06/invoke-actions-on-non-command-component.html

http://maping930883.blogspot.com/2012/05/adf119action.html

<think>好的,我现在需要帮助用户找到在JavaFX中实现按钮点击事件的代码示例。用户已经提供了一些参考引用,其中引用[1]是一个简单的JavaFX程序,创建了一个按钮,但还没有事件处理。引用[2]提到事件处理的相关信息。我的任务是根据这些资料生成正确的示例代码,并确保符合用户的要求。 首先,我应该回忆一下JavaFX中事件处理的基本方法。通常,按钮的点击事件是通过设置setOnAction方法,并传入一个EventHandler<ActionEvent>来实现的。用户可能需要一个完整的示例,包括按钮创建、事件处理以及程序的启动。 接下来,我需要检查用户提供的引用内容。引用[1]中的代码创建了一个按钮,并将其放入场景中,但缺少事件处理部分。引用[2]指出事件处理的通用方法,但没有具体代码。因此,我需要结合这两者,补充事件处理的代码。 然后,我应该考虑如何将事件处理集成到示例代码中。例如,在按钮实例化后,调用setOnAction方法,并在其中定义事件发生时的操作,比如弹出一个对话框或在控制台输出信息。这样用户就能清楚地看到点击后的反馈。 另外,需要注意JavaFX的应用程序结构,确保start方法正确设置场景和舞台,并且包含main方法调用launch。用户提供的示例代码已经包含了这些基本结构,所以只需要在按钮部分添加事件处理即可。 还需要验证代码的正确性,比如导入正确的包,例如javafx.event.EventHandler和javafx.event.ActionEvent。如果用户提供的引用中没有这些导入,可能需要补充,或者在示例代码中明确写出,避免用户遗漏必要的引用。 最后,要确保代码格式正确,使用代码块包裹,并正确标注语言类型为java。同时,生成相关问题,帮助用户进一步学习,比如事件处理的原理、其他类型的事件,以及如何传递参数。这些问题应该与用户的问题相关,并且基于提供的引用内容。 总结步骤:1. 创建按钮实例。2. 设置按钮的onAction事件处理器。3. 在处理器中定义点击后的行为,比如弹出对话框。4. 确保代码结构完整,包括舞台和场景的设置。5. 检查导入语句是否正确。6. 生成相关问题,扩展用户的学习路径。</think>[系统级响应] 在JavaFX中实现按钮点击事件需要三个步骤:创建按钮控件、注册事件处理器、定义事件响应逻辑。以下是完整的代码示例: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Alert; import javafx.stage.Stage; import javafx.event.ActionEvent; import javafx.event.EventHandler; public class ButtonClickDemo extends Application { @Override public void start(Stage primaryStage) { // 1. 创建按钮控件 Button btn = new Button("Click Me!"); // 2. 注册事件处理器 btn.setOnAction(new EventHandler<ActionEvent>() { // 3. 定义事件响应 @Override public void handle(ActionEvent event) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("点击事件"); alert.setHeaderText(null); alert.setContentText("按钮已被点击!"); alert.showAndWait(); } }); Scene scene = new Scene(btn, 400, 200); primaryStage.setTitle("JavaFX按钮事件示例"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 该示例实现了: 1. 创建带文字标签的按钮控件 2. 使用匿名内部类实现$EventHandler<ActionEvent>$接口 3. 通过$Alert$对话框显示点击反馈 4. 符合JavaFX应用程序的基本结构要求[^1][^2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值