Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing: System-wide components scale?

Tags:

java

swing

Is there a way to scale all the Swing components in an app by a single factor? I have an existing Swing app, and I want to display all the components at double size. Is this possible?

EDIT:

I'm porting a Java Swing app to Sugar (OLPC XO Graphics Environment). I don't have accessibility functionalities. The machine has only one resolution of 1200x900 in a 7.5” display. So the components are really small and unreadable.

like image 734
Butaca Avatar asked Dec 05 '22 16:12

Butaca


2 Answers

If you place

System.setProperty("sun.java2d.uiScale","2")

at the very beginning of your main method, this will scale the entire application by a factor of 2. Other scaling factors are possible. If have tried this successfully with openJDK 11 on Windows 7 and Linux with KDE, both with System Look and Feel. Windows does accept fractional scaling factors, KDE does not.

If you do not want to modify the source code, a command-line option to the jvm will have the same effect:

java -Dsun.java2d.uiScale=2 -jar myApp.jar

like image 50
Wiwi Avatar answered Dec 22 '22 01:12

Wiwi


Unluckily there is no such standart feature in Swing.

Every component size in application is determined by layout of the container where that component is added and component's preferred/minimum sizes, provided by their UIs.

So the best way (as i see it) is to modify standart UIs, so they provide additional preferred size (doubled in your case). But you will have to do that separately for each component of a certain type (buttons/checkboxes/tables/trees/scrolls e.t.c.). Plus you cannot change the system UIs - you could only extend some cross-platform Look and Feel like Metal LaF and that won't be useful at all in case you are using native Look and Feel.

You can change some default L&F properties though, like font:

UIManager.put ( "Button.font", new FontUIResource ( new Font ( "Arial", Font.BOLD, 20 ) ) );

This specific case changes only buttons font. There are also a lot of other components font properties that you can find in any LookAndFeel class (for e.g. BasicLookAndFeel).

like image 22
Mikle Garin Avatar answered Dec 21 '22 23:12

Mikle Garin