Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Windows Design differs on Linux

Tags:

java

javafx

I build the following JavaFX-Application on Windows. On Linux the button size and font size does not match. Label, Textfield und Button descriptons do not fit to its dimension.

Picture 1 enter image description here

How can I set the font size of my program so that it looks like under windows?

Changing the font size and font style manually does not works very well as it does not fit the dimension. Or maybe I did not find the right combination.

Thanks in Advance

Windows and Linux using JDK 8.
- Tool for JavaFX: SceneBuilder by Gluon

The following does not help:
Default font size of JavaFX under Linux is larger as on Windows
Modena – new theme for JavaFX 8

like image 856
User8461 Avatar asked Jan 24 '26 23:01

User8461


1 Answers

[EDIT: It occurs to me that I came up with this "solution" before having much awareness of themes. Next time I'm dealing with this, I will check if the explicitly making the theme on both Windows and Mac/Linux identical (rather than allowing their respective defaults) eliminates this problem.]

I was pretty disappointed to find out that the famous "write once--run everywhere" program had this rather significant shortfall. I'm holding out the hope that there is a more elegant way to deal with it, or something I'm missing or misunderstanding, but for now am going in and specifying sizings for the given OS. From what I've seen, there are only two operating system scaling sizes that need to be accommodated: Windows on one hand and Linux/Mac on the other.

If there's only one or two sizes to tweak, something like the following can work:

    String os = System.getProperty("os.name","generic").toLowerCase(Locale.US);
    if (os.indexOf("mac") > 0) {
        root.setStyle("-fx-font-size: 14pt");           
    }

In this case, the Windows default font had a size of 10pt and my Mac user wanted something larger, and the program was not otherwise being widely distributed. Once the style is set for the group at the base of the scene graph (I usually name it 'root'), it will pertain to the entire graph unless over ridden.

But for more elaborate situations, I've used something like the following static class. When I distribute the application, which is usually as a self-contained program (using JLINK), the OS is known and can be set at the top of the file. I might as well just post the whole class. You will probably have different specifics, and more or fewer things that need tweaking.

If there are better ways to handle this, and I am all ears.

import java.awt.Font;
import java.awt.Insets;

/**
 * This file contains values needed that pertain to the
 * difference in the size relationships of fonts:pixels in
 * Mac OS and Windows. 
 * 
 * @author Phil Freihofner
 */
public class OSConstants {

    public enum OS {WIN, MAC_LINUX};
    public static OS os;
    {
//      os = OS.WIN;
        os = OS.MAC_LINUX;
    }
    
    public static Font tinySizeFont;
    public static Font smallSizeFont;
    public static Font normalSizeFont;
    public static Font largeSizeFont;
    public static Font bigLabelFont;
    public static Font cboChoicesFont;
    public static Font buttonFont;
    public static int preferenceButtonSpacer;
        
    public static Insets gridButtonInsets;
    public static Insets gridCBOInsets;
    public static Insets gridLabelInsets;
    public static Insets gridRadioInsets;
    public static Insets gridSliderInsets;
    
    private final int MAC_TINY_FONT_SIZE = 7;
    private final int MAC_SMALL_FONT_SIZE = 8;
    private final int MAC_CBO_FONT_SIZE = 10;
    private final int MAC_NORMAL_FONT_SIZE = 11;
    private final int MAC_LARGE_FONT_SIZE = 15;
    private final int MAC_BIG_LABEL_FONT_SIZE = 20;
    private final int MAC_PREFERENCE_BUTTON_SPACER = -16;

    private final Insets MAC_GRID_BUTTON_INSETS = new Insets(-1, -1, -1, -1);
    private final Insets MAC_GRID_CBO_INSETS = new Insets(-1, -1, -1, -2);
    private final Insets MAC_GRID_LABEL_INSETS = new Insets(-1, -1, -1, -1);
    private final Insets MAC_GRID_RADIO_INSETS = new Insets(-1, -1, -1, -1);
    private final Insets MAC_GRID_SLIDER_INSETS = new Insets(-1, -1, -1, -1);
    
    private final int WIN_TINY_FONT_SIZE = 8;
    private final int WIN_SMALL_FONT_SIZE = 9;
    private final int WIN_CBO_FONT_SIZE = 11;
    private final int WIN_NORMAL_FONT_SIZE = 12;
    private final int WIN_LARGE_FONT_SIZE = 16;
    private final int WIN_BIG_LABEL_FONT_SIZE = 24;
    private final int WIN_PREFERENCE_BUTTON_SPACER = -22;
    
    private final Insets WIN_GRID_BUTTON_INSETS = new Insets(2, 2, 2, 2);
    private final Insets WIN_GRID_CBO_INSETS = new Insets(2, 2, 2, 2);
    private final Insets WIN_GRID_LABEL_INSETS = new Insets(2, 2, 2, 2);
    private final Insets WIN_GRID_RADIO_INSETS = new Insets(2, 10, 2, 2);
    private final Insets WIN_GRID_SLIDER_INSETS = new Insets(2, 2, 2, 2);
    
    OSConstants()
    {
        if (os == OS.WIN)
        {
            tinySizeFont = new Font(Font.SANS_SERIF, Font.PLAIN, WIN_TINY_FONT_SIZE);
            smallSizeFont = new Font(Font.SANS_SERIF, Font.PLAIN, WIN_SMALL_FONT_SIZE);
            normalSizeFont = new Font(Font.SANS_SERIF, Font.BOLD, WIN_NORMAL_FONT_SIZE);
            largeSizeFont = new Font(Font.SANS_SERIF, Font.BOLD, WIN_LARGE_FONT_SIZE);
            bigLabelFont = new Font(Font.SERIF, Font.BOLD, WIN_BIG_LABEL_FONT_SIZE);
            cboChoicesFont = new Font(Font.SANS_SERIF, Font.BOLD, WIN_CBO_FONT_SIZE);
            buttonFont = new Font(Font.SANS_SERIF, Font.BOLD, WIN_NORMAL_FONT_SIZE);
            preferenceButtonSpacer = WIN_PREFERENCE_BUTTON_SPACER;
            
            gridButtonInsets = WIN_GRID_BUTTON_INSETS;          
            gridCBOInsets = WIN_GRID_CBO_INSETS;
            gridLabelInsets = WIN_GRID_LABEL_INSETS;
            gridRadioInsets = WIN_GRID_RADIO_INSETS;
            gridSliderInsets = WIN_GRID_SLIDER_INSETS;
        }
        else if (os == OS.MAC_LINUX)
        {
            tinySizeFont = new Font(Font.SANS_SERIF, Font.PLAIN, MAC_TINY_FONT_SIZE);
            smallSizeFont = new Font(Font.SANS_SERIF, Font.PLAIN, MAC_SMALL_FONT_SIZE);
            normalSizeFont = new Font(Font.SANS_SERIF, Font.BOLD, MAC_NORMAL_FONT_SIZE);
            largeSizeFont = new Font(Font.SANS_SERIF, Font.BOLD, MAC_LARGE_FONT_SIZE);
            bigLabelFont = new Font(Font.SERIF, Font.BOLD, MAC_BIG_LABEL_FONT_SIZE);
            cboChoicesFont = new Font(Font.SANS_SERIF, Font.BOLD, MAC_CBO_FONT_SIZE);
            buttonFont = new Font(Font.SANS_SERIF, Font.BOLD, MAC_NORMAL_FONT_SIZE);
            preferenceButtonSpacer = MAC_PREFERENCE_BUTTON_SPACER;
            
            gridButtonInsets = MAC_GRID_BUTTON_INSETS;          
            gridCBOInsets = MAC_GRID_CBO_INSETS;
            gridLabelInsets = MAC_GRID_LABEL_INSETS;
            gridRadioInsets = MAC_GRID_RADIO_INSETS;
            gridSliderInsets = MAC_GRID_SLIDER_INSETS;
        }    
    }
}
like image 96
Phil Freihofner Avatar answered Jan 26 '26 13:01

Phil Freihofner