专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java教程 » netbeans:NetBeans Property Editor Tutorial »正文

netbeans:NetBeans Property Editor Tutorial

来源: 发布时间:星期四, 2009年1月8日 浏览:11次 评论:0
  This tutorial shows techniques for using property editors in NetBeans, including providing custom editors and custom inplace editors. Specically, the following will be covered:

  Providing your own property editor for an individual Node

  Creating a custom editor

  Creating a custom inplace editor

  Registering a custom property editor globally

  To follow this tutorial, you need the software and resources listed in the following table.

Software or Resource Version Required
NetBeans IDE version version 6.1 or

  version 6.0
Java Developer Kit (JDK) version 6 or

  version 5



  Optionally, for troubleshooting purposes, you can download the completed sample.

  Introduction to Custom Property Editors  Often you may have a property for which either the standard property editor is not sufficient, or the property type is a for which there is no standard property editor. NetBeans IDE contains es for many common Swing types, but every possible need cannot be covered by a of generic property editors.

  This tutorial is ended as a follow-on to these preceding tutorials, and its code is based _disibledevent="NetBeans Property Editor Tutorial" />

  Registering DatePropertyEditor Globally  Often it is useful to register a property editor to be used for all properties of a given type. Indeed, your DatePropertyEditor is generally useful for any property of the type java.util.Date. While usefulness is not the primary determinant of whether such a property editor should be registered, your application or module will regularly deal with Date properties, it might be useful to do so.

  Here is how to register DatePropertyEditor so that any property of the type java.util.Date will use DatePropertyEditor in the property sheet:

  Right click the My Editor project, and choose Properties from the popup menu.

  On the Libraries page of the project properties dialog, click Add Dependency—you need to add a dependency on the Module API so you can sub ModuleInstall to run some code on startup.Type ModuleInstall. The dialog should auto-select "Module API". Press Enter or click OK to add the dependency on the Modules API from the My Editor module.

  Right click the org.myorg.myeditor package in the My Editor project and choose New > Other. Under the NetBeans Module Development category, select Module Installer. Click Finish. A sub of org.openide.modules.ModuleInstall will be created for you—this contains code that will run during startup.

  Implement the restored method, which is run during startup, as follows:public void restored {
  PropertyEditorManager.registerEditor(Date., DatePropertyEditor.);
}
This code will register DatePropertyEditor as the default editor for all properties of the type java.util.Date throughout the system.

  Press Ctrl-Sht-I to Fix Imports.

  Remember, you should only do this you really need to—ModuleInstall es slow down application startup, because they mean more code has to run during startup. So where possible they should be avoided. If you do need to register a lot of property editors, though, it may make sense to aggregate them in a single module that registers them during startup.

  If the type you want to provide a property editor for is in your module, it may be preferable to place the registration code in a block that will be invoked when that is loaded, e.g.

public Foo {
   {
     PropertyEditorManager.registerEditor(Foo., FooEditor.);
  }
  //...
  Caveat: If you are not sure your property editor will be used during a typical session, a better technique may be to use PropertyEditorManager.EditorSearchPath, adding your package to the .gif' /> of packages ed by PropertyEditorManager.getEditorSearchPath. The above code will cause FooEditor. to be loaded o memory—this is paying a price of about 1K of memory for something that will not be used. For one or two property editors, this is probably acceptable; for more, it is preferable to aggregate all of your property editors o one package, name the es appropriately and register that package is being on the search path. For more information on registering property editors, see the javadoc for PropertyEditorManager.

  Using PropertyPanel  While you won't cover it in great detail, it is worth mentioning that the property sheet is not the only place that Node.Property objects are useful; there is also a convenient UI in the org.openide.explorer.PropertySheet called PropertyPanel. It's function is to display one property, much as it is displayed in the property sheet, providing an editor field and a custom editor button, or you have called somePropertyPanel.Preferences(PropertyPanel.PREF_CUSTOM_EDITOR), it will display the custom editor for a Property. It is useful as a convenient way to get an appropriate UI component for editing any getter/ter pair for which there is a property editor.

0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: