开发者

Swing Application Framework @Action annotation ? what for?

开发者 https://www.devze.com 2022-12-16 12:08 出处:网络
I\'m actually using the Swing Application Framework on a desktop application and make good use of the session state persistence and resource manager (great for globalization)

I'm actually using the Swing Application Framework on a desktop application and make good use of the session state persistence and resource manager (great for globalization)

Now I have been testing the @Action annotation feature, and really I don't see any advantage over NOT using it. The official doc lists mainly these advantages:

you can conveniently put all the relevant visual cues and the event-processing logic for a component in one place.

Yes this place storing the visual cues is the resource bundle. (can be used with Resource Manager) Yes the event-processing logic in one place is within a common method. but no need to annotate a method in order to make it common to several events.

Another convenient benefit of using Action interfaces is that you can reuse the same action across multiple UI components. GUIs often provide multiple ways to accomplish a task

Yes, obviously event-processing can be common to different events, but again using the @Action annotation isn't required to call a common method from different action listeners methods.

I don't see much of an advantage writing this :

 openButton.setAction(actionMap.get("open"));
 // NOI18N

 @Action public void open() {
     // processing logic 
 }

instead 开发者_开发问答of:

 openButton.addActionListener(new
 java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 openButtonActionPerformed(evt);
             } });

 private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {
     // processing logic 
 }

To mention that using the latter, I keep my method private, which is neater, and I also keep control over specific components getting added the listener (as each of them will have their own resources, instead of sharing the same visual cue!)

Anyone found an real interest in using @Action annotations ?


The documentation provides the answer to your question.

https://docs.oracle.com/javaee/6/api/javax/xml/ws/Action.html

I think the advantage to using @Action rather than the anonymous ActionListener is that @Action is a very nice and convenient hook into the framework which makes it really easy to localize things for the action such as icons, text, or mnemonics. Additionally, if your @Action annotated method returns a Task object, the framework appropriately handles executing code that should not run on the EDT.

Without using the frameworks @Action you'd have to be very careful to localize your ActionListeners and be even more careful about how you manage code that should not run on the EDT (if you want a responsive UI).

Using @Action on a method that returns a Task is a very powerful convenience this framework provides.


When I use NetBeans every method marked with @Action will be automatically available in the NetBeans GUI for setting actions on components.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号