Customizing Generated Code


NetBeans IDE dynamically generates the code for GUI construction. You can view this code in the Source view of the Form Editor (click the Source button in the Form Editor's toolbar). In addition to the code generated within the class in its class-name. java file, the IDE maintains an XML file called class-name.from that details the structure of the form. Note that the source-code control systems (such as CVS) supported by NetBeans ensure by default that the .form file is maintained in the repository in addition to the .java file.

The generated code within the .java source file is delimited by special comments (for example, //GEN-BEGIN:initComponents ...//GEN-END:initComponents). The editor does not allow this code to be modified and indicates the unmodifiable code with a pale blue background. Although you could modify this code outside the IDE, it is not recommended, because those modifications would be lost if you reopened the form in the IDE. (The IDE regenerates the .java file of a form created in the IDE from the .form files each time you open the file in the IDE.)

The use of delimited generated code prompted vigorous discussion in the NetBeans IDE team, but the advantages are significant: It is extremely difficult to reliably "reverse-engineer" arbitrary Swing code without requiring restrictive coding discipline on the developer's part.

Instead, NetBeans IDE provides "hooks" where you can add (almost) any arbitrary code to be part of the code to be generated. This code is added via a codeaware window accessed from the Code tab of the Properties window for the component. The properties used are:

  • Custom Creation Code. Code to be inserted instead of the default newComponentClassName(); statement.

  • Pre-Creation Code. One or more lines of code to precede the statement that instantiates the component.

  • Post-Creation Code. One or more lines of code to follow the statement that instantiates the component.

  • Pre-Init Code. One or more lines of code to precede the first statement that initializes the properties of the component.

  • Post-Init Code. One or more lines of code to follow the last statement that initializes the properties of the component.

In addition, the initial values of the various properties of components can be specified in various ways:

  • A static value.

  • A property from a component written to the JavaBeans architecture.

  • A property of another component on the form.

  • A call to a method of the form or one of its components. You can choose from a list of methods that return the appropriate data type.

  • Code you define, which will be included in the generated code.



NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net