Swing and GTK font - java

I have write a little GUI with java Swing. But i have some little problems with the default look and feel using com.sun.java.swing.plaf.gtk.GTKLookAndFeel
As you can see the menus and the buttons doesn't have the same font of the other application in my Desktop (In the picture Eclipse and Nautilus), I'm using GTK on Gnome2.
Meanwhile using the relative look and feel on windows there are not differences:
The font look identical.
How is it possible this? Which parameters can I modify to edit the font look on GTK?

You should let the System decide which L&F to use
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
In your above image you are using Ubuntu with Unity (I guess?), but Unity does not use GTK but Nux.
Another way to create a native Look and Feel is the using the SWT (Standard Widget Toolkit) that provides you with system dependent libraries for Windows, Linux and OSX, so that the graphical elements in your program use the actual native elements from those systems. But I have to warn you, it may not be that easy (and sometimes even weird and annoying :) ) to use, because you cannot use any Swing components. So maybe that's not worth the trouble – it's for you to decide.

Consider setting defaults for the fonts on components after setting your look and feel.
UIManager.put("Menu.font", new FontUIResource(your_desired_font));
UIManager.put("Button.font", new FontUIResource(your_desired_font));
Windows L&F is using by default new Font("Tahoma", Font.PLAIN, 11);

For GTK, set the font standard by UIManager.getLookAndFeelDefaults() does not work... then I found a hack via reflection that works, you can find it here in response:
https://stackoverflow.com/a/31345102/3757320

Related

Corrupted java swing component

I'm working on a Java Swing project,
I have an issue with some component while running projects vs running the interface here's some screen shoot :
This look when I run only the jFrame
This is when I run A Frame that lead to this
and this when i run the full project
I want to know why this look difference and how to resolve it.
I'm Using netbeans 12.2 with jdk 15.0.2 on a windows 64 bit machine
Java Swing by default uses native GUI components. The upside of this is that when done correctly, your Java application will have a Windows style on Windows and a Linux on Linux etc. To get a fixed style, you can set the Java Look And Feel to a LAF that is always available, such as the built-in METAL LAF. This page contains much more specifics on Java Look And Feels, how to set them and even how to create your own if you wish.

java L&F to obtain modern "wpf" look

Recently I found some absolutely beautiful applications made using WPF.
I'd really like to add this lovely look to my java applications.
Do you know if there are look and feels which make it possible?
First of all think what are the key concepts of the new metro-UI ?
Its flat and simple
1) use metro studio 2 to create icons for the project, use flat borders always
2) either create a full L&F or simply create small custom components ? or use Java FX (http://pixelduke.wordpress.com/2012/10/23/jmetro-windows-8-controls-on-java/ , http://pixelduke.wordpress.com/category/javafx/page/3/)
I was able to come little bit close to metro like text-buttons and background repeat panels, you might also want to see my repo here https://github.com/JaDogg/BhathiGUI
above image is for my near metro custom controls
NOTE : this will not create an actual metro app just one that looks like it
Update
Here is a screenshot of JavaFX application built using Undecorator and JMetro (JavaFX8)
Java was always stingy on new L&F. So new WPF style (developed for Windows 8 apps) is not available yet and i doubt it will be anytime soon.
The latest L&F released and provided together with Java SE was NimbusLookAndFeel. It is based on SynthLookAndFeel, which supposed to be a base for any custom L&F from now on.
There are also native L&Fs with:
Windows 2000 and Vista/7 styles (WindowsLookAndFeel)
GTK style (GTKLookAndFeel)
Mac OS style (AquaLookAndFeel)
There are also some good commercial L&Fs:
Java Look and Feel (L&F)
So if you really want to have such L&F - you will have to modify some existing L&F so it matches WPF styling or wait for a miracle (that someone will release such L&F).

Why in my Java application am I seeing "strange" scrollbars?

In my Java application I have a window which holds a JTextArea within a JScrollPane with scrollbars policies set to AS_NEEDED.
As I run my application I see that JTextArea this way:
Why am I seeing the scrollbars with that cutaway knob (which doesn't reflect a "standard" representation like this)?
The Layout for the frame is GridBagLayout, and I'm on Mac OS X 10.8.2, should that matter.
This is based on the Look and Feel your app is using, and the limitations of Java's integration with the native OS layout components. The one in your screenshot looks like Nimbus.
Swing applications always custom-render the look and feel, and don't do a very good job of using the native OS widgets everywhere. The result is that you get weird looks that might be consistent the OS only some of the time, or only with certain layout components.
Welcome to developing cross-platform desktop apps in Java. :(
To attempt to get the system look and feel when your application starts you can do this:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassname());
} catch (Exception e) {
// Handle exception
}
This will set the look and feel to that of the system regardless of what you run it on.
And as mentioned, the default look and feel for your application appears to be Nimbus and not OSX's Aqua, which again can be fixed with he above snippet and you could (should you care to) offer a UI option to the user to change the look and feel of the application to whatever they chose.
You are with Nimbus LookAndFeel
http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/nimbus.html

Java FileDialog selecting directories: Mac OSX only?

I read that via
System.setProperty("apple.awt.fileDialogForDirectories", "true");
users can select directories via a FileDialog, now the FileDialog evoces the native file chooser, so that is exactly what i want but in the line above it reads: apple.awt..., does this mean this option will only work on Mac OSX?
if(no) {
great
} else {
what can i do to implement this on other operating systems than?
}
Thanks for any help!
PS: I know a lot of people suggest the use of a JFileChooser, but in this case i'd very much prefer the FileDialog, except if that's impossible
It is exactly as you feared.
AWT used native libs underneath. OSX has the feature to look for directories only, windows does not.
So youre only change is to use a dialog not based on AWT, i.e. Swing or SWT.
You can define an interface with platform specifc implementations. This gives a good looking dialog on OSX and something that works on other platforms. That's what I do.
To my knowledge, FileDialog does not support (in-code) using a directory dialog on all platforms.
You're already mentioned Swing's JFileChooser, but you may want to consider SWT for your widgets instead.
SWT is Eclipse (originally IBM)'s project to create an updated Java GUI Toolkit that still uses native widgets when they are available.
The major downside of SWT is that it is not part of the standard Java distribution... and each platform has its own SWT jar file.
Incidentally, SWT has a DirectoryDialog widget.

Java Swing positioning elements differently in different operating systems

I have written a Java Desktop Application that runs in Windows, Mac OS X, Ubuntu and Open Suse. I am having issues with is positioning thing differently in Linux.
I developed the application with NetBeans using the designer, it looks as I would expect in Windows and Mac OS X, but in the Linux distros certain label controls have shifted into different positions.
Is there a common reason for this?
Perhaps Linux uses a different font. One where letters have a different width.
You could try to explicitely set a specific font for your Look&Feel. It should be a font which is installed on all of your target platforms.
The most probable thing is that you use different Look and Feel for every platform. If you're developing in Windows you do layouts to fit for Window style only. But Linux has different L&F with different margins and font for GUI components. Metal style (basic for Linux) and its descendants (Nimbus and GTK+) have larger default system font and heights for components. I think, that must be a reason why you have shiftings.
The way to fix that is to check program looking in both platforms. I'd suggest to develop in Metal style because Windows has smaller fonts, as result, everything which fits in Metal will fit in Windows.
I'm assuming you developed the thing on Windows or Mac, that's why it looks "as expected". Can you compiling/running the code in your IDE on Linux and see how it looks?
My guess is that one of the implementations of the containers is flawed. I saw this sometimes when I did cross-development and mostly did trial-and-error modifications to fix it (by changing to use other classes). It also helped to have one developer working in Windows and another in Linux, so that we would easily spot and fix problem areas.
I changed the Layout style from 'Free Design' to 'Absolute'

Categories