How to add textbox change event in NetBeans' GUI builder [duplicate] - java

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 gray! 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:
Drop a new component onto the design
editor 'canvas' for the window.
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:
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:
be sure to save the current version somewhere
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.

Close Netbeans
Go to folder path where there is your form file
Backup the 2 files with extensions ".form" and ".java"
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.
The first file ".form" is xml file and the second ".java" is a code java file
Edit carefully your code in both files save changes and open NETBEANS.
Thats it!

Related

Is there any possibility to just open a piece of code in a separate tab/section in Intellij?

I know the that we can close the sections of the code as mentioned here
Imagine a jsp file and you want to change a part of code (e.g from lines 100 -150 in 700 jsp file)
It would be easy for any one to select the code and open in any of below mentioned scenarios
popup
permanently highlight the background
new tab
Do intellij/eclipse supports any of the above mentioned feature even with plugins ?
There is no way to open only the code fold in another editor.
What I do is I split the editor, for example right-click on the editor tab and select Split Vertically or Horizontally. Then you'll get another editor window showing the same file. Expanding/collapsing code folds in one window will not affect the other window.
There is a great, related feature that however only works in specialized cases, where IDEA detects (automatically or via annotation) that a region contains another language, for example:
//language=HTML
String foo ="<div>Tralala</div>";
Here you can press alt-enter on the string and select "Edit HTML Fragment" to see only the string content in a new editor window.
In many cases IDEA automatically detects that the language, in other cases it needs help (alt-enter, then "Inject language or reference").

Eclipse Java code formatting

I'm writting a GUI program.
It has several JPanels that conatin other components. It would be really nice if my code would look like this:
parent component code
child component code
grandchild component code
instead of this:
parent component code
child component code
grandchild component code
It would make the code far more readable. Is(Are) there some character(s) I can put in my code that Java will ignore, but Eclipse would detect it as a part of code that has to have additional indentation?
I know I can do all this manually, but I often set indentation automatically (ctrl+a, ctrl+i). That way I don't have to worry about indentation directly, I can make Eclipse manage it for me. However, that also nullifies any "custom" changes I made myself.
Adding {} does the trick, but it also influences on the way java reads the code, so that doesn't really work.
Maybe there are some plug-ins available for this?
Something that would detect lines like e.g.
//{
and
//}
and then indent code that is enclosed in them?
Select a block of text with the proper opening and closing braces, and then pressing Ctrl+Shift+F will format the selected block of text.
Morevoer, in eclipse you can go to Window -> Preferences -> Java -> Code Style -> Formatter. Here you can create a new formatter profile and assign a lot of your custom formatting rules.
Screenshot for easy reference

which WindowBuilder Swing widget should I use to make a System.out "console" display?

I do not want to give up visibility to all the data the program prints to System.out as I upgrade a linux console app to a Swing app. If I dump it to a file, I might never see something important.
Which WindowBuilder Swing widget should I use to make a System.out "console" display?
(I am using Java, Eclipse, ubuntu, Swing and WindowBuilder.)
Edit: I used jfpoilpret's suggestion and Mohaimin's suggestion. The result is a private static reference to a JTextArea (inside a scrollable pane inside a WindowBuilder structure). Then I use a getter method to expose the JTextArea and I invoke the getter as follows:
public static PrintWriter pw = new PrintWriter(new mySwingConsole(getter()))
where mySwingConsole is a Writer.
You might use a class such as the Message Console for the System.err/System.out side of it.
The answer is described in the "edit" to the question. I used jfpoilpret's suggestion and Mohaimin's suggestion. If jfpoilpret wants to repost his comment as an answer, I will select it.

From JFrame to JPanel in Netbeans

I'm rethinking the design of my GUI. I designed a few JFrame with Netbeans automated GUI (yes, I'm lazy, and this generated code is often awful, I know !), but now I want them to be JPanel (actually, to inherit from another class that inherits JPanel). But I had the "setDefaultCloseOperation" modified, so my code is broken : setDefaultCloseOperation is impossible for a JPanel. Since I can't modify the generated code, I was wondering : is there a way to make Netbeans understand I changed my mind, and regenerate the code ?
When I run into this (I've done the same thing before) I usually end up having to modify the generated code XML file (.form file) or just copying all of the controls I've added and paste into a new JPanel. Just my $0.02 but beware that this can break your code...
IMHO, nb won't manage automatic refactoring in this case; it's easier to add new panel and copy-paste all elements from Your old JFrame (their methods will be copied as well).
BEFORE DOING THIS, close the file in Netbeans AND BACK UP the .java and the .form file you are about to edit.
I just had this problem, and fixed it by changing the .form file, that follows with your .java file for the given type.
In the top of this XML file you will see a:
<Form version="1.3" maxVersion="1.8" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
Just change this to:
<Form version="1.3" maxVersion="1.8" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
Notice that i just changed this part:
type="org.netbeans.modules.form.forminfo.JPanelFormInfo"
After you have done this, reopen the file in netbeans, it will now tell you that there is an error, this is because you might have set some properties, that aren't available for a JPanel, but was for the JFrame. Just hit Edit, and then change one value in your GUI, this will force it to rebuild the generated code, and this way it will remove the properties that aren't applicable.
It should now be fixed for you..
I hope this helped a bit!
' Cheers!
You might look in Team > Local History to see if you can revert.
Can't you just remove the setDefaultCloseOperation() and any other calls to methods that are no longer in the super class? Making this call definitely doesn't make sense now that the class is not a JFrame.
I don't now what you broke, but I can tell you this: You can not tell Netbeans to change his own generated code the way you described it.
You can try the following (be sure to make a backup before):
Open YourPanel.java in some editor
Delete the line with setDefaultCloseOperation....
Replace all getContentPane() with this
Delete pack(); at the last line.
Open YourPanel.form in some editor
Delete the node <Properties> (and everything within)
Delete the node <SyntheticProperties>.
In root node change from <Form ... type="...JPanelFormInfo"> into <Form ... type="...JFrameFormInfo">
As far as I observed, everything within the forms file can be deleted apart from the stuff within <Layout>.
Good Luck.
Yes, you can simply copy and paste it to the JPanel. Make sure that, you JPanel size must greater or equal to the existing JFrame container size. :)
I just had the same problem and it turns out the solution was fairly simple.
As others have said, make a backup of your file.
Open your JFrame class and edit it to extend JPanel rather than JFrame.
Cut your main method method and put it into another class.
Close your netbeans project and netbeans IDE
Open your .form file in a text editer that is located in the src folder and change the first line from this:
Form="1.5" maxVersion="1.8" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"
to this:
Form="1.5" maxVersion="1.8" type="org.netbeans.modules.form.forminfo.JPanelFormInfo"
After youve saved that. Open the netbeans project again.(it will open with an error)
Modify one of the properties of the JPanel.
Run the programme. Netbeans will automatically get rid of all methods that are only associated with a JFrame. and it will run fine.
Hope that helps someone!

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

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 gray! 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:
Drop a new component onto the design
editor 'canvas' for the window.
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:
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:
be sure to save the current version somewhere
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.
Close Netbeans
Go to folder path where there is your form file
Backup the 2 files with extensions ".form" and ".java"
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.
The first file ".form" is xml file and the second ".java" is a code java file
Edit carefully your code in both files save changes and open NETBEANS.
Thats it!

Categories

Resources