Wednesday, March 12, 2008
GroupLayout converting Java 6 to Java 5
OK, so I'm writing Swing code again (which I'm not very good at), and have been using an odd combination of Netbeans 6 to do form design via Matisse (since the Eclipse version is MyEclipse only?) and the rest of the code in Eclipse (since I can't get comfortable in NB yet). Unfortunately, and perhaps because I'm running on Java 6, Matisse defaults to using the Java 6 javax.swing.GroupLayout, but my client is running Java 5 (late discovery). While I could install a new version of Java for them, the lack of Java 6 on my G5 Mac at home tipped me back to 5.
While there is an option in NB to generate using the org.jdesktop packages (enabled now), it won't retrospectively rejig an existing form. Now the APIs while essentially the same code, and different versions so a bunch of things make the process more than changing the imports.
The script below did 95% of the changes my forms needed (in fact just one line in two forms needed a parameter removing -- addComponent had an extra param on a vertical group). Just thought I'd put it out there since I saw a few forum posts asking about this. It would be nice if NB could do this for you, but I suspect it's a low priority item..
Update:
Annoyingly, when you do this, the form reverts to the previous layout type next time you edit in NetBeans. By the power of diff, you can fix this too by changing the MyForm.form file thus:
While there is an option in NB to generate using the org.jdesktop packages (enabled now), it won't retrospectively rejig an existing form. Now the APIs while essentially the same code, and different versions so a bunch of things make the process more than changing the imports.
The script below did 95% of the changes my forms needed (in fact just one line in two forms needed a parameter removing -- addComponent had an extra param on a vertical group). Just thought I'd put it out there since I saw a few forum posts asking about this. It would be nice if NB could do this for you, but I suspect it's a low priority item..
$ cat swing6tolayout.sedIt does make me smile that despite the size / complexity / ability of Eclipse & Netbeans, sed was the easiest way to do this. Wasn't Eclipse supposed to be providing refactoring batches? Or James Gosling's project to manipulate code? Dunno. Sed was already in the toolkit (cygwin), so it won.
s/javax.swing.GroupLayout/org.jdesktop.layout.GroupLayout/g
s/GroupLayout.Alignment/GroupLayout/g
s/javax.swing.LayoutStyle.ComponentPlacement./org.jdesktop.layout.LayoutStyle./g
s/\.addGroup/\.add/g
s/\.addComponent/\.add/g
s/\.addGap/\.add/g
$ sed -f swing6tolayout.sed MyForm.java > tmp.java
$ mv tmp.java MyForm.java
Update:
Annoyingly, when you do this, the form reverts to the previous layout type next time you edit in NetBeans. By the power of diff, you can fix this too by changing the MyForm.form file thus:
$ diff JDesk.form Java6.formie '2' is for jdesktop.org and '1' is for Java 6.
9c9
< <auxvalue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="2" />
---
> <auxvalue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
Labels: GroupLayout, java, swing
Comments:
<< Home
Actually, I am in a similar situation. I am writing an app that is supposed to be as cross-platform compatible as possible (hence the use of Java), but since there is only a developer preview of Java 6 for the Mac, I am reverting to Java 5. NetBeans will allow you to do exactly this.
If you're looking at a form in design view, go to your Inspector panel. The very first thing in there will be something like Form FormName. Right click that and go to properties. Change the Layout Generation Style from Standard Java 6 Code to Swing Layout Extensions Library. I haven't tried this on several forms at a time, so it could be quite bothersome if you've got more than 5 forms in your project. It might be worth looking into more though.
If you're looking at a form in design view, go to your Inspector panel. The very first thing in there will be something like Form FormName. Right click that and go to properties. Change the Layout Generation Style from Standard Java 6 Code to Swing Layout Extensions Library. I haven't tried this on several forms at a time, so it could be quite bothersome if you've got more than 5 forms in your project. It might be worth looking into more though.
I just changed the .form file to use the "Swing Layout Extensions Library" and then made a trivial change to the gui and everything was regenerated correctly. I tried what the above comments said and that worked perfectly and was easier than modifying the form file (note to self, read the comments before trying to fix the problem).
I did this for two forms in a medium sized project. Had to do each form, but This tutorial was quick and fast!!! Thanks so much!
Design Mode --> Inspector Panel --> Right Click Form --> Properties --> Layout Generation! Woohoo!
Keywords for hits:
grouplayout java 5 netbeans
convert java 6.0 java 5.0 netbeans
ant build deploy osx java se
Design Mode --> Inspector Panel --> Right Click Form --> Properties --> Layout Generation! Woohoo!
Keywords for hits:
grouplayout java 5 netbeans
convert java 6.0 java 5.0 netbeans
ant build deploy osx java se
Awesome. I read some thing very similar but not as concise. I had been screaming at this problem for quite some time and now - yippee!
Wow, thanks for that, just saved me too! I'm modifying a Java 6 app that some-one else wrote, and I'm not much of a java programmer so that would have been a real time waster.
Post a Comment
<< Home