Raveline
Raveline

Reputation: 2678

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 ?

Upvotes: 5

Views: 10754

Answers (8)

Alec Hewitt
Alec Hewitt

Reputation: 824

I just had the same problem and it turns out the solution was fairly simple.

  1. As others have said, make a backup of your file.
  2. Open your JFrame class and edit it to extend JPanel rather than JFrame.
  3. Cut your main method method and put it into another class.
  4. Close your netbeans project and netbeans IDE
  5. 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"
  1. After youve saved that. Open the netbeans project again.(it will open with an error)
  2. Modify one of the properties of the JPanel.
  3. 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!

Upvotes: 0

André Snede
André Snede

Reputation: 10045

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!

Upvotes: 2

Mohamed Saligh
Mohamed Saligh

Reputation: 12339

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. :)

Upvotes: 0

Andreas L.
Andreas L.

Reputation: 142

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):

  1. Open YourPanel.java in some editor
  2. Delete the line with setDefaultCloseOperation....
  3. Replace all getContentPane() with this
  4. Delete pack(); at the last line.
  5. Open YourPanel.form in some editor
  6. Delete the node <Properties> (and everything within)
  7. Delete the node <SyntheticProperties>.
  8. 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.

Upvotes: 0

jzd
jzd

Reputation: 23629

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.

Upvotes: 0

barti_ddu
barti_ddu

Reputation: 10299

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).

Upvotes: 3

trashgod
trashgod

Reputation: 205775

You might look in Team > Local History to see if you can revert.

Upvotes: 1

Merky
Merky

Reputation: 494

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...

Upvotes: 5

Related Questions