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.
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
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).
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
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
Related
How to get the font properties of titlebar in JOptionPane. I am using linux OS system. We can get font properties of other java component using getFont API but as titlebar is OS dependent (native), what is the way to get font properties of titlebar ?
You can't really control titlebar decorations on Linux before Wayland, which is not generalized yet, and even in a Wayland world that will depend on the desktop environment, its toolkit, and how much leeway it gives to individual apps.
Probably a lost cause if you add the Java/native toolkit barrier. Java is not even using GTK3 yet, I doubt GTK2 will ever get more than minimal Wayland love and exploiting Wayland from GTK3 will probably require deeper support than just a minimal GTK2 glue replacement.
You may have more luck with SWT.
OpenJDK GTK3 support
I have written a Java UI using the SWT UI library and the MigLayout layout library.
This page shows screenshots of the UI on Windows, Linux and OSX: http://mchr3k.github.com/org.intrace/screenshots.html
On Linux and OSX my UI has a lot more padding and spacing which I feel wastes a lot of screen space. Is this normal for these platforms? If not, what is the best way to work around this?
EDIT: the linked screenshots have now been updated and no longer show an extra gap on OSX as I have explicitly set the margins on some of my UI elements to 0. The OSX UI is still quite spaced out but I assume that this is correct for the platform.
This is Mikael Grev, the creator of MigLayout.
As someone else mentioned this is a feature of MigLayout. Instead of using x number of pixels (you can of course use that too) by default it is using gaps like related, unrelated and paragraph. These correspond to different sizes in different UI toolkits. For instance on OS X the recommended white space between components is larger than on Windows.
So, yes, this is how it's supposed to look and it will look correct for a native OS X user.
You do seem to have too much white space at the bottom of the Output pane though, in both OS X and Windows.
You can use the PlatformDefaults class in MigLayout to either force a platform or to change the default gaps. Check the source code if you want to know what's happening behind the scenes.
And remember, this is a feature so that every developer won't have to keep track of how to spacing should look like on different platforms, which is kind of hard.
Cheers,
Mikael
That's actually a feature of MigLayout, if I recall correctly. If you don't want this I am sure you could so configure MigLayout. Otherwise, you could use a different layout manager (but I still highly recommend a table-based one) which uses constant spacing for all platforms (such as my MatrixLayout).
I have been developing a program on a windows machine.
All dimensions for the application are based on the default windows font and size.
The default mac font seems to be larger in size, therefore not all text is visible in labels, comboboxes, ...
How can i make sure that the proper font size is used on mac machines?
The correct way to do it is not to hardcode control sizes and use layout managers instead.
Michael is right. Font sizes are not the only thing changing from one L&F to the other. Components like buttons can have different sizes too. So forget about hardcoded sizes. With Apple's control styles, developers now have a chance to choose the size of some components, but there is no guarantee this will work in the future if your layout is not flexible.
Is there a way to write a Java Swing application with a custom chrome? Please take a look* at the frame for Microsoft's Zune 4.0 software.
I realize that colors, the shape of scroll bars, etc. are controlled by skins or looks and feels. Right now I'm trying to tackle the native window which houses the java components--the title bar mainly.
Thanks
(*) http://www.winsupersite.com/zune/zune4_shots.asp
By default the frame of a JFrame is native. This can be removed by calling Frame.setUndecorated. The Sun Window PL&F does not provide a title bar. You could hack aJInternalFrame so that it draws the frame, although that probably isn't going to be as easy as it may seem. Of course, if you are going the full custom route, you can draw whatever you want. From 6u10, Sun's JRE also provides APIs to make windows transparent and non-rectangular.
No part of a Swing component's look and feel is "native" in any way. Swing components are "lightweight", which means they are entirely drawn on the Java side, and not at all on the windowing system side.
To create custom "chrome" you create the UI delegates for one or more components. In yor case, you'd want to muck around with the delegates for JRootPane and JInternalFrame.
The Look and Feel of Swing apps are pluggable..that is it can change on the fly. You can create your own look and feel but its not a simple undertaking. To get started this tutorial explains. This article does a little more.
This project demonstrates what could be done. So its up to your imagination.
Running a Java Swing program on Ubuntu Linux, I'm getting an odd bug where the font height is too large for the actual font size, that is, every piece of text (in menus, buttons, text areas etc.) has excess blank space underneath the text itself.
This does not depend on which font I select.
It also does not depend on the font size chosen - setting a larger size in an attempt to use up the extra space, just causes the excess space to scale with the font size.
It does depend on the operating system -- it works fine on Windows.
It also depends on the look and feel chosen -- it works with the GTK look and feel (which is otherwise not as attractive as the Metal look and feel, so I would prefer not to use that as a workaround if I can avoid it).
I can't find any mention of this on Google. Any ideas how to fix it?
Are you using SunJDK or OpenJDK? I think OpenJDK uses some other fonts and in general has problems with fonts and layouts. If you're using default install of eclipse, you're with gcj which is even more unpredictable. Try SunJDK.