Thursday, March 23, 2017

Keep Aspect Ratio of a JFXPanel embedded in a NetBeans TopComponent when resizing

Following the strategies outlined in the book "JavaFX Rich Client Programming on the NetBeans Platform" by Gail and Paul Anderson (Amazon reference) I still had trouble keeping a circle a circle after users decide to resize the TopComponent.

The FX Scene is using a StackPanel and on top of that Panels for different drawings. The ScatterChart sits on top of the StackPanel and causing a circle to become oval when the user reduces the width or height of the TopComponent.

Challenge is show below :



And what really should happen when like above the user fx reduces the width, is:

Like with the Egg of Columbus the solution is quite simple. Though you have to remember that the controller is executing in the FX Thread and the TopComponent on the Swing Event Thread.

In the TopComponent containing the JFXPanel, add the following to the componentOpened() method:


The "controller" object is the class (as described in the book mentioned above) and is executing on the FX Thread. The method with the long name is doing nothing else than setting the Pref size of the StackPane and the ScatterChart in the controller class like:



If any of you have a better way - drop a comment. The above at least made it work for me.