From JFrame to JPanel in Netbeans - java

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!

Related

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

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!

Positioning a new view within Eclipse Plugin

I have successfully created a new view, passing in the primary ID from my plugin.xml and a unique secondary ID. All is well. It pops up as one would want.
However ... it pops up in completely the wrong folder.
I want it to go along side the other one's that are of the same type. It is the main folder center panel I want it created in. Now I can manually move it (as a user) and put it where I want it.
Feel so close to this.
What am I missing?
I am not clear by "wrong folder"?
Two things here.
First, if it's showing up in the wrong place in the workbench, then you need to use org.eclipse.ui.perspectiveExtensions extension to place it in the right place in the perspective.
Second, if it's not grouping with set of views, then you need to add "category" in the view extension to show up in the right category in "Show View".
Hope these two points will help you out. Please let me know if I completely missed your point.
Create a perspective extension and use the "relative" attribute of the view definition there.
I found the solution. When laying out the folders in the perspectiveExtensions extended class, I need to tell it where additional views will end up. You do this by calling the IFolderLayout.addPlaceholder() passing in the necessary wildcards where necessary.
bodyleft.addPlaceholder( "MyViewId:*" );
This works a charm.

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

Java SwingX Setup and use of JXCollapsiblePanel

I am trying to create a panel which opens to the left of the rest of my GUI. I am working in Eclipse. I added swingx-core-1.6.2.jar to my build path. I can import org.jdesktop.swingx.JXCollapsiblePane and then make a new JXCollapsiblePane, but in order to set the orientation of the pane, I need to do something like:
JXCollapsiblePane myCollapsingPane = new JXCollapsiblePane();
mycollapsingPane.setOrientation(JXCollapsiblePane.Orientation.HORIZONTAL);
However, this fails saying that Orientation cannot be resolved. So, I tried importing
org.jdesktop.swingx.JXCollapsiblePane.Orientation, which also fails saying that it cannot be resolved.
I'm probably missing something silly here; how do I set the panel to open to the side rather than vertically?
JXCollapsiblePanel at 1.6.2 has no setOrientation method. Also, there is JXCollapsiblePane.Orientation enum any more. It was probably dropped.
However, there is JXCollapsiblePane.Direction and there JXCollapsiblePanel.setDirection(), it is probably a replacement.
Here is a thread about this.

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