当前页面: 开发资料首页 → Netbeans 专题 → NetBeans Google Toolbar Module Tutorial
摘要: NetBeans Google Toolbar Module Tutorial This tutorial demonstrates how to create a module that adds a Google Search toolbar to the IDE. You use a wizard to create an action that extends CallableSystemAction. The wizard lets you specify that the action will be registered as a toolbar button. You then create a Swing panel, containing a JTextField and a JLabel, and you override the action's getToolbarPresenter() method to return the panel. When the user presses Enter in the JTextField, URLDisplayer is used to send a google search string to the IDE's default browser. The default browser opens and displays the results of the user's search string.
This tutorial demonstrates how to create a module that adds a Google Search toolbar to the IDE. You use a wizard to create an action that extends CallableSystemAction. The wizard lets you specify that the action will be registered as a toolbar button. You then create a Swing panel, containing a JTextField and a JLabel, and you override the action's getToolbarPresenter() method to return the panel. When the user presses Enter in the JTextField, URLDisplayer is used to send a google search string to the IDE's default browser. The default browser opens and displays the results of the user's search string.
The following topics are covered below:
Once the software is installed, this tutorial can be completed in 20 minutes.
For more information on working with modules, see the NetBeans Development Project home on the NetBeans website. If you have questions, visit the NetBeans Developer FAQ or use the feedback link at the top of this page.
Before you begin, you need to install the following software on your computer:
Take the following steps to install the sample:
Now that you know what the end result looks like, you will create the module from scratch and learn about each part while creating it.
Before you start writing the module, you have to make sure you that your project is set up correctly. NetBeans IDE 5.x provides a wizard that sets up all the basic files needed for a module.
The IDE creates the Show XML Structure project. The project contains all of your sources and project metadata, such as the project's Ant build script. The project opens in the IDE. You can view its logical structure in the Projects window (Ctrl-1) and its file structure in the Files window (Ctrl-2). For example, the Projects window should now look as follows:
For basic information on each of the files above, see the Introduction to NetBeans Module Development.
Click Next.
Click Next.
Then, delete the default text in the JTextField. Next, click the JLabel and change its text to Google:
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
int i = evt.getKeyChar();
if (i==10){//The ENTER KEY
// we display the google url.
try{
URLDisplayer.getDefault().showURL
(new URL("http://www.google.com/search?hl=en&q="+jTextField1.getText()+"&btnG=Google+Search"));//NOI18N
} catch (Exception eee){
return;//nothing much to do
}
}
}
Right-click in the Source Editor and choose Reformat Code (Ctrl-Shift-F). Notice that a line of code is underlined in red. This is because its package has not been imported yet. You will resolve this in the next step.
Select UI Utilities API and click OK. Then click OK to confirm your selection and to exit the Project Properties dialog box. Right-click in the Source Editor again, and choose Fix Imports (Alt-Shift-F). Now the red underline disappears and the package is imported.
public java.awt.Component getToolbarPresenter() {
return retValue;
}
At the top of the action class declare the following:
GooglePanel retValue = new GooglePanel();
The IDE uses an Ant build script to build and install your module. The build script is created for you when you create the module project.
The module is built and installed in the target platform. The target platform opens so that you can try out your new module. The default target platform is the installation used by the current instance of the development IDE.
The NBM file is created and you can view it in the Files window (Ctrl-2):
For more information about creating and developing module, see the following resources: