3

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.

Butaca
  • 416
  • 4
  • 14
  • 2
    *"Is this possible?"* A better question is "What is the point of doing it?" If the user needs the components of your app. to be twice the size, they can use the OS inbuilt accessibility functionality to do that. That ability comes free & independent of your app. – Andrew Thompson May 30 '12 at 03:04
  • There is no global scale factor. In its present form, this question cannot be answered. PLease edit your question to include details of what you want to accomplish. – trashgod May 30 '12 at 03:19
  • @AndrewThompson Please, check the edit. There is your point. – Butaca May 30 '12 at 14:06
  • I'm sure that this is most important reason why LayoutManagers exist here, by defauft there no easiest way – mKorbel May 30 '12 at 15:28
  • @Butaca: What Look & Feel implementations are available for this target platform? – trashgod May 30 '12 at 16:34
  • There are these L&F available Metal, Nimbus, CDE/Motif and GTK+ – Butaca Jun 01 '12 at 00:20

4 Answers4

7

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

Wiwi
  • 71
  • 1
  • 1
  • That sun.java2d.uiScale property appeared a bit late I guess :) Thanks for your answer anyway, I didn't know about it :) – Butaca Aug 21 '19 at 21:04
  • That property is not available in Java 8 and is available in Java 11, so I guess it was introduced pretty lately. – toolforger Aug 18 '20 at 13:53
4

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).

Mikle Garin
  • 10,083
  • 37
  • 59
  • Is it possible to change the default font size then? Thanks for your answer. – Butaca May 30 '12 at 00:27
  • It is certainly possible to modify [default](http://java.sun.com/products/jfc/tsc/articles/architecture/) Look & Feel properties using the `UIManger`. – trashgod May 30 '12 at 03:15
  • @trashgod You can modify Look & Feel properties through UIManager, but some of them won't be properly updated (even after you do a full components tree update since some of them are getting cached after first call). Plus there is only a few properties that affect components sizes and they do not depend on the component itself (for e.g. button/textfield margins), so you cannot assign them the way they will double component size. – Mikle Garin May 30 '12 at 07:32
  • 1
    @Butaca yes, you can modify the default components font size through default Look & Feel properties. Check my answer again for code example. – Mikle Garin May 30 '12 at 08:28
  • @MikleGarin: Good points. As the OP has since adduced a readability requirement, it may be possible to focus on font size, as you have shown. The component should be allowed to adopt the corresponding [preferred size](http://stackoverflow.com/q/7229226/230513). +1 – trashgod May 30 '12 at 16:33
4
  • is possible to scalling the Swing JComponents by using Nimbus Look and Feel

  • is very complicated to modify Nimbus L&F, its Color, Insets, Bounds e.i., but in plain form without any issues

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

You can use J(X)Layer for this (see www.pbjar.org/blogs/jxlayer/ but site seems down at the moment). An example which builds on it can be found here: http://patrickwebster.blogspot.com/2009/01/scalable-jfreechart-applet.html

Walter Laan
  • 2,956
  • 17
  • 15