tags:

views:

489

answers:

3

Hi,

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?

A: 

Usually when a component that I make with Swing needs to be resizable I just resize the frame to the desired size and then allow the layout manager to do the resizing of the individual components for me.

For really good flexibility I use GridBagLayout and embed panels with custom layout within other panels.

However, I have no solution to your specific problem.

jjnguy
+2  A: 

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.

Jay R.
sorry about not accepting this earlier :), you are right and the team is now working on reimplementing much of the interface using better layout management. Thanks
luke
A: 

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.

luke
That would suck....sorry
jjnguy
tell me about it.
luke