Blog

Netbeans Platform. How to Invoke (Call) an Action Programmatically

[{"Id":"201901292039130485","ParentId":"20190129203729976739","Type":"Text","Sequence":1,"Attributes":{"Text":"The Netbeans Platform (NBP) is an advanced tool for rapid application development. One of its most important features is the action. As most of the features with the NBP creating an action is straight forward using the graphical user interface. Right click a module select \"Add Action\", enter the required details and off you go."}},{"Id":"201901292044030742","ParentId":"20190129203729976739","Type":"Text","Sequence":2,"Attributes":{"Text":"In the same time it is not so clear what needs to be done to call the action from another point of your application. Lets say you have created a MenuItem action, and you want to invoke (call) it from a Toolbar action. Or from a button in your main component."}},{"Id":"201901292046580572","ParentId":"20190129203729976739","Type":"Text","Sequence":3,"Attributes":{"Text":"Fortunately for us this is already thought of and to be completely frank is actualy quite easy. Every action when created is required to have an identification (ID). The ID usually defines to which category the action belongs, as well as its name."}},{"Id":"201901292049380143","ParentId":"20190129203729976739","Type":"Code","Sequence":4,"Attributes":{"Language":"java","Code":"@ActionID(\r\n category = \"File\",\r\n id = \"com.example.package.core.MyAction\"\r\n)"}},{"Id":"201901292051290448","ParentId":"20190129203729976739","Type":"Text","Sequence":5,"Attributes":{"Text":"Thanks to the ID the action can be easily invoked from anywhere like this:"}},{"Id":"201901292051310586","ParentId":"20190129203729976739","Type":"Code","Sequence":6,"Attributes":{"Language":"java","Code":"Action action = org.openide.awt.Actions.forID(\"File\", \"com.example.package.core.MyAction\");\r\naction.actionPerformed(e);"}},{"Id":"201901292051320943","ParentId":"20190129203729976739","Type":"Text","Sequence":7,"Attributes":{"Text":"The above code will call the required action and will execute the statements defined in the actionPerformed method."}}]