Swing on Ubuntu font height - java

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.

Related

What are font properties of titlebar in JOptionPane?

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

inconsolata and netbeans

When I installed inconsolata everything looked fine, however when I tried viewing it on netbeans it look like this:
I tried using both the ttf and otf versions and they both looked messed up.
Is this a java issue? Win 7 64 bit issue? Is there any way to fix this?
You could try disabling font antialiasing in NetBeans.
Open up your netbeans.conf
As part of the netbeans_default_options section add -J-Dswing.aatext=false -J-Dawt.useSystemAAFontSettings=off.
Edit: fvu also provided a helpful link to a relevant section in the NetBeans font rendering FAQ.
If you want to play around with NetBeans font rendering settings, take a look at http://wiki.netbeans.org/FaqFontRendering
Unfortunately, what this article concludes is that default font rendering settings provide the best possible experience. At least under Windows. I have tried all the possibilities with Inconsolata and defaults provide the the best look indeed.
As a workaround, you can try two things:
Decrease contrast of your color scheme, i.e. by decreasing lightness of a background and/or increasing lightness of your font. It makes font rendering issues less visible.
Use NegativeScreen: http://arcanesanctum.net/negativescreen/
You may find your experience improved, but this is up to your personal preference.
It's not just NetBeans, although it is probably not simply the Inconsolata font's fault either. There seems to be a problem at the level of the underlying Java system that NetBeans is built on. I say this because the exact same problem shows up in the IDEs created by Jetbrains -- IntelliJ IDEA, WebStorm, PyCharm, and possibly others.

Java Swing: System-wide components scale?

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

Cross platform UI spacing/padding

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

How can i set the proper font for java application on windows and mac?

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.

Categories

Resources