views:

811

answers:

2

I am working on a Java application using Swing and I want to set up the GUI using the "proper" fonts and font sizes. With proper I mean the fonts (+size) as defined by the user, so that my application will fit right in to the user's screen setup (resolution / font size combination) without having to provide my own custom settings. My app will be used on Windows, MacOS X and Linux.

I've been unable to find how to do that, or whether this is actually possible in the first place. I found some references to getSystemFont but could not find any documentation.

Can anybody help?

EDIT: Eugene suggested that using setLookAndfeel would already take care of that. While this may be correct, this still leaves me with the issue on how to add e.g. a JTextField to the screen that has the proper height, as I need to pass something on to .setBounds

+1  A: 

Actually system look and feel suppose to do it for each platform. You just have to use system look and feel:

UIManager.setLookAndfeel( UIManager. getSystemLookAndFeelClassName())

And do it before your GUI is created.

eugener
Yes, but I still have to set the font I want to use - don't I? Also I need to retrieve the font size in order to be able to calculate the height of e.g. an input field (JTextField)
btw, I already have this line in my code
No.. All of that is set in UI defaults of system look and feel.
eugener
The only other way is to replace all the font settings in UI defaults to the ones you need
eugener
Well, I don't want to define what I need, but rather want the system settings. I am a bit confused about how I would now create e.g. a JTextField and position it on the screen. Wouldn't I need to know the font characteristics in order to know how big to make it?
You can define everything of course, but you don't have to. It has preferred size and height there is based on font UI defaults. The width is up to you. If you use graphical editor - just drop the field and you'll see it taking on preferred size
eugener
BTW... Best thing you can do it to use good layout (such as MIG layout) to ensure that you UI behaves correctly on different platforms
eugener
A: 

this still leaves me with the issue on how to add e.g. a JTextField to the screen that has the proper height, as I need to pass something on to .setBounds

No, you should be using LayoutManagers. They do all the work for you. There is no need to use a null layout and therefore you don't need to set the bounds of the component.

Read the section from the Swing tutorial on Using Layout Managers

camickr