开发者

How to modify/add code to the initComponents() method in Java using NetBeans?

开发者 https://www.devze.com 2022-12-22 16:26 出处:网络
How to modify/add code to the initComponents() method in Java on NetBeans? When I try to add any line of code this area seems to be like readonly and it\'s highlighted in gr开发者_开发技巧ay! It\'s fo

How to modify/add code to the initComponents() method in Java on NetBeans? When I try to add any line of code this area seems to be like readonly and it's highlighted in gr开发者_开发技巧ay! It's for security probably, but I suppose there is a way to disable that.


Yes the initComponents method is read only to keep full control for the IDE. You may add yours in the constructor right after initComponents.

public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
    initComponents();
    myInitComponents();
}

public void myInitComponents() {
}


The initComponents() method is regenerated by the IDE as you create your UI in the GUI editor. The method is 'guarded' to prevent this regeneration from overwriting user written code.

There are a couple of ways to add code to this method, indirectly:

  1. Drop a new component onto the design editor 'canvas' for the window.

  2. Enter code as part of one of the following code properties: Pre-Creation Code, Post-Creation Code, Pre-Init Code, Post-Init Code, Post-Listener Code, Pre-Population Code, Post-Population Code and After-All-Set Code.

    There are a couple other code properties that do not alter the initComponents() method... but can be very useful: Pre-Declaration Code and Post-Declaration Code. alt text http://blogs.sun.com/vkraemer/resource/code-properties.png Note: the editor for these properties is not 'rich', so I would recommend creating methods in the "regular editor" that you just call in the initComponents().

You can modify the code in the initComponents() method by positioning or changing the 'regular' properties of the 'base panel' or controls.


Presumably what you are doing is writing an application using the Matisse GUI tool.

Matisse generates non editable code blocks. This is required by Matisse so that the tool remains synchronized with the code base.

There are a number of options provided by Matisse to allow insertion of custom code before, after or within code blocks such as initComponents().

See the image below:

How to modify/add code to the initComponents() method in Java using NetBeans?

This shows the properties tab for a jPanel and some of the code insertion options.


If you right-click on the component in the Design View, and then click on "Customize Code" selection, you can modify the code in the InitComponent code. Several lines of explicit code can be customized.


To allow changes in both the source and the Matisse GUI editor, NetBeans prevents editing in what it calls "guarded blocks". While you could imagine the IDE being able to interpret almost any kind of GUI code you write, in practice, it is much easier for the IDE developers to encapsulate the automatically generated GUI code in a single method (initComponents()) and leave the rest for you to edit.

If you are certain you know what you're doing, you can easily edit the .java file from an external editor, but:

  1. be sure to save the current version somewhere
  2. check that your changes didn't break something by opening the class in NetBeans visual editor once your done


I discovered by trial and error, that initialization which needs to be done before the user sees the panel should be added as "Pre-Init Code". In my case, I needed to populate a drop down box (called "Choice" in AWT). There seems to be very little detailed documentation available on using Matisse. So, I hope this will help others.


select all of the code and copy in an external editor like gedit or notepad.

Then delete your jframe file.

Create a new java class in netbeans in the same package with the same name.

Copy all the contents from the editor file and paste it in your newly created java class.


  1. Close Netbeans
  2. Go to folder path where there is your form file
  3. Backup the 2 files with extensions ".form" and ".java"
  4. Edit the 2 files with extensions ".form" and ".java" in notepad editor. For example if your form name is "myForm" you must have the files "myForm.form" and "myForm.java" in the folder.
  5. The first file ".form" is xml file and the second ".java" is a code java file
  6. Edit carefully your code in both files save changes and open NETBEANS. Thats it!
0

精彩评论

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

关注公众号