Java GUI Resolution Independent Scaling

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?

Asked by: Dainton765 | Posted: 21-01-2022

Answer 1

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.

Answered by: Brad722 | Posted: 22-02-2022

Answer 2

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.

Answered by: Connie592 | Posted: 22-02-2022

Similar questions

java - Resolution independent SWT Dialog

Can somebody tell me how to achieve resolution independence for size and appearance of swt dialogs. my dialog works correct in 1680x1050, but when resolution is changed to 1280x800, part of dialog is clipped away!

java - Android resolution independent Bitmap from ImageView

I have a Bitmap which is 1200 pixels in both height and width. And I put it in ImageView. My app requires that later on I get the Bitmap from the ImageView so that I get the same quality of the Bitmap just when put in the ImageView. If I do it with getDrawingCache for the Layout I get bad image since I have to scale to the original size of the Bitmap and there is quality loss. So is there any resolution independe...

java - Export composite to image independent of the screen resolution

Is there a way in SWT to export a composite to an image which always has the same size/resolution? The problem is that we have a Dashboard which looks always different when opening on screens with different display size/resolution. The question is now can I export the Dashboard to an image wich has a fixed size and always looks the same no matter on which screen size/resolution it has been created? For the time bei...

java - hashCode() method when equals() is based on multiple independent fields

i have a class whose equality is based on 2 fields such that if either one is equal then the objects of this type are considered equal. how can i write a hashCode() function for such an equals() so that the general contract of hashCode being equal when equals returns true is preserved? public class MyClass { int id; String name; public boolean equals(Object o) { if (!(o instanceof MyClass)) ...

java - How to split a path platform independent?

I'm using the following code to get an array with all sub directories from a given path. String[] subDirs = path.split(File.separator); I need the array to check if certain folders are at the right place in this path. This looked like a good solution until findBugs complains that File.separator is used as a regular expression. It seems that passing the windows file separator to a functio...

java - Is there a way to make the Maven build independent of the network connection?

I'm working in a Continuous Integration environment and part of the automated build process includes the compilation of Maven managed projects. What I want to know is if there is a way to make the Maven build independent of the network connection. Currently we have the all the jar's that we need installed in the repository but the problem is that Maven tries to check for plugins updates and that makes the ...

operating system - Platform Independent Way of Detecting the Number of Processors in Java

Is there a platform independent way to detect the number of physical and/or virtual processors in Java? One potential solution is to detect the operating system and use the Windows environment variable "NUMBER_OF_PROCESSORS". Do Linux and Mac OS X have a similar offering? This however may not be the best solution.

Java Algorithm for finding the largest set of independent nodes in a binary tree

By independent nodes, I mean that the returned set can not contain nodes that are in immediate relations, parent and child cannot both be included. I tried to use Google, with no success. I don't think I have the right search words. A link, any help would be very much appreciated. Just started on this now. I need to return the actual set of independent nodes, not just the amount.

Is it correct to say that "Java is platform independent but version dependent ?"

Our project application uses Java 5 and now when I update Java 6, there are some kind of inconsistency with functionality and seeing this effects our manager passed comment that "Java is platform independent but version dependent", Is it really true ?

windows - Find user independent TEMP directory with Java

when running a Java application as service with the user 'LocalService', the temp directory ("") points to 'c:/windows/temp' (for example). Running a Java application normally gives 'c:/documents and settings/user/local settings/temp' instead. How can I determine the user independent temp folder 'c:/windows/temp' when my application runs normally? Thanks and greetings, GHad

java - Is Eclipse platform independent?

Is eclipse platform independent? As per my knowledge, eclipse is written in java. If it is so eclipse should be platform independent. But there are different eclipse for different OS???

Why is the JVM not platform independent, given that Java (the language) is platform independent?

Just curious to know when java is made platform independent then are there any specific reasons JVM is made platform dependent..

java - How do i propagate security to my independent web service enterprise application?

I created a simple web application which contains web pages and one enterprise application which contains web services and EJBs for my web application. I managed to configure security for my web application. But now how do i propagate this security to my enterprise application on my EJB method methods? so that i can use annotations like @RolesAllowed("") etc?

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)