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 b开发者_运维问答e 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()
withthis
- 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!
精彩评论