Converting PowerBuilder Applications to Java – Tip #1
I had to convert a PowerBuilder application to Java for a client recently and documented some information to help out any other PB developers who might need to do the same. The task is not a fun one but it forced me to learn Java.
Java in my opinion is a 3GL (3rd Generation Language) so in many situations it will take a lot more code to accomplish the same things as in a 4GL such as PowerBuilder. The datawindow is the most challenging thing to convert and at first I was tempted to write my own Java datawindow replacement, but the task was far too difficult for a Java beginner. One of the most difficult and overlooked part of the conversion was printing. Java is terrible at printing reports and something that we take for granted as PowerBuilder developers is a nightmare in Java.
I’ll post the PowerBuilder to Java tutorials in many separate posts titles similar to this one. You can click on the PB to Java category to show all posts for that category and find all the tips I’ve posted to date.
Top Issues when converting from PowerBuilder to Java Client Server
- UI controls don’t map easily especially datawindow controls and edit mask
- Converting DataWindows
- Mapping of PowerBuilder events to Java Swing
- Datawindow Reports
- Since the Swing library is platform independent to achieve the same look and feel the Layout Manager needs to be set to Windows look and feel. There is no native Java for Edit Mask but there are third party components available.
- Develop helper classes that implement the base DataWindow functionality.
- Some UI events in PowerBuilder do not have equivalent in Java Swing. Experimentation and use of multiple events may be needed. e.g. DataWindow ItemChanged Event -> FocusListener.focusLost
- Use BIRT (open source) for reporting.
Mapping of PB Objects and Variables to Java
Converting PowerBuilder Datatypes to Java Datatypes
The most obvious challenge in a conversion is to determine mapping between PowerBuilder object types and variables and Java. There is no hard set rules when it comes to the mappings but I’ve posted suggestions based on my limited experiences. The most challenging objects to convert are datawindows. There was another area that caused a lot of confusion for me at first, and that is passing values by reference in function calls, or events. You can’t pass variables by reference in Java so you need to work around that by creating classes for your variable types and using getter and setter methods. I’ll explain in more detail in a future post.
So for now, here is a list of potential mappings of PowerBuilder to Java objects/variables.
PowerBuilder to Java Conversion
Object Mapping Suggestions
|Datawindow (Free Form)||javax.swing (various components)|
|Datawindow (Grid, Tabular)||javax.swing.JTable|
Variable Mapping Suggestions
|real||float or double|
Datawindows – Converting Tips
Datawindows are by far the most complicated to convert to Java. There are lots of different datawindow object types but in general there are List types and Free Form types. Also, to be considered are drop down datawindows. In PowerBuilder drop down datawindows are easy and they use a code value and display value. By default they do not use a code value in Java, the JComboBox only uses a display value. If this is a problem then you could extend the JComboBox and write code to support the data value. There is no support for edit mask type controls, the only suggestion is to use third-party tools such as JSuite.
List Style Datawindows
The JTable is a good option for converting tabular and grid view datawindows. There are some aftermarket controls you might want to consider that have extended the JTable, we did not purchase any but we suffered with a pretty difficult learning curve. The JTable is very primitive compared to the datawindow so you’ll be doing a lot more coding than you are used to.
Free Form Datawindows
Typically, we would create a Jpanel for Free Form datawindows. Then depending on which controls you need, place them onto the Jpanel. Use the mapping table above for determining which components to use on the Jpanel.
There is no support for printing JTables in a report format. We purchased a third-party tool for reporting in the new Java application. Printing in Java appears to be an afterthought, if your client requires a lot of printing support be sure to allow a LOT of time to support this.
Using Layout Managers
When designing GUI’s in Java it is important to understand Layout Managers. I have had some exposure to Layout Managers when learning Microsoft .NET in Visual Studio 2010. Using layout managers is pretty cool because your GUI is much more flexible when it comes to resizing and different screen sizes. You can hard code component X and Y coordinates by setting layout managers to null. The gridbaglayout is one of the best layout managers in Java, and it is the most common one that IDE’s generate when layout out GUI’s.
Layout Managers help applications adapt to different operating systems. Also another great advantage of Layout Managers is that you will not have to code for resize events which can be tedious in PowerBuilder if you have lots of controls.
Convert PowerBuilder Units to Pixels
If you are converting some PB objects to Java and want to keep some of the same look and feel then you’ll have to convert PowerBuilder X and Y coordinates to Pixels. PowerBuilder provides functions called XUnitsToPixels and YUnitsToPixels.
Java doesn’t support structure objects so you will need to create objects and then make instance variables for each of the attributes of the structure. You’ll also need to make getter and setter methods for each of the instance variables. The instance variables should be declared as private and access be limited to using getter and setter functions.