开发者

Generate anonymous listener in NetBeans

Is there any way to automatically generate appropriate listener in NetBeans?

For example, when I have JButton, and I type button.addActionListener, I'd like NetBeans to generate following code:

new开发者_JAVA技巧 ActionListener()
{

    public void actionPerformed(ActionEvent e)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}


NetBeans is capable of pregenerating listeners, without using Code Templates.

It is possible by typing new SomeListener and pressing CTRL+Space.

Generate anonymous listener in NetBeans

And viola!

Generate anonymous listener in NetBeans


To create event-listeners with NetBeans, open the file with your button, get to the "Design"-tab and select your button. In the sidebar on the right you should see the properties of the button. If not, be sure the properties-panel is activated under Window -> Properties. If you got the properties, select the "Events"-tab, and the first event you should see is the actionPerformed-event. In the dropdown-list you can either use the default event-name, or give it your own. If you selected it, NetBeans automatically creates the code you posted above for you.


You need a code template for this. Tools>Options>Editor>Code Templates

Generate anonymous listener in NetBeans

You can then click New and add your own abreviation say 2act. Then when you type:

 button.2act

and hit tab, Netbeans will replace 2act with your code.


Go to Tools -> Options -> Miscelleneous -> GUI Builder.

You''ll find some options as to how your listeners are generated.

Generate anonymous listener in NetBeans


You want to create your own new Template for this, so go into the Tools menu then the Templates submenu to reach the Template Manager. Then click on the Java folder and you can see a listing of the pre-existing Templates. To see what's in them, click on edit. To copy one, highlight the template and click duplicate. For more on how to create and modify templates search on Template Manager in the NetBeans help files.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜