Java GUI Resolution Independent Scaling

By : luke

I'm working on a large legacy java application for which an important feature was automatic scaling of GUI Components based on monitor resolution. We are upgrading the JVM on which it runs from 1.4.2 to 1.6 and now the scaling is broken due to a change in the implementation of java.awt.Container.getPreferredSize().

getPrefferedSize used to return the same object that you gave with setPreferredSize() (everything also goes for min/max size), so what we would do is call setPRefferedSize on every scaling component with a particular subclass of java.awt.Dimension, then we would walk the component hierarchy and update each scaling component whenever the resolution changed.

However in Java 1.6 getPreferredSize returns a copy of the Dimension object you passed it, so it is no longer the right type and nothing gets scaled.

I hacked together a solution pretty quick by overriding the Component class with my own implementation (thank you open source JRE) by placing it in front of the JRE on the class path. However this solution is clearly not maintainable.

Does anyone else know any other solution to this problem, how would you implement GUI scaling in java 1.5/1.6?

By : luke


Changing layout managers seems like good solutions, but would, in this case, necessitate a complete redesign of the UI (which includes thousands of components) so this isn't really a practical solution for this application.

By : luke

It sounds like they are protecting the internal state of the component better in 1.6 than before. Maybe they ran FindBugs on it and fix the bug.

When I first read this question, I thought of Java 1.6u10. It includes a new look and feel called Nimbus which supports High DPI displays by way of using vector graphics to draw all of the interface components.

However, after reading the question a little slower and trying to comprehend what you are saying, I'd suggest that you rewrite the user interface using a layout manager that will manage the sizes of the internal components for you. My feeling is that hand managing the component sizes as you suggest is really not a good idea. As jjnguy suggests, you could use GridBagLayout. There are a number of other choices for Layout Managers. There was a question posted that provides a survey of everyone's favorite Layout Managers, if you are looking for something different than GridBagLayout.

By : Jay R.

public key encryption? Use the server's public key to encrypt the password.

By : axel_c

This video can help you solving your question :)
By: admin