1

I have a Java application that is using Swing GUI, when I execute application in MAC pro or surface, UI size is been very small but the font size is normal, if I change the font size it can be suit, but the font and UI objects will become very small it is hard to read.

Can I let the layout looks like full HD panel in the high resolution panels?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ives
  • 505
  • 1
  • 4
  • 14

1 Answers1

2

You need to calculate resolution factor. This code blocks calculate the factor for all OS

public static Float getRetinaScaleFactor(){
  Object obj = Toolkit.getDefaultToolkit().getDesktopProperty("apple.awt.contentScaleFactor");
  if(obj != null){
   if(obj instanceof Float)
    return (Float) obj;
  }
  return null;
}


public static boolean hasRetinaDisplay(){
  Float fRetinaFactor = getRetinaScaleFactor();
  if(fRetinaFactor != null){
     if(fRetinaFactor > 0){
        int nScale = fRetinaFactor.intValue();
        return (nScale == 2); // 1 indicates a regular mac display, 2 is for retina
     }
   }
   return false;
}


 private static float getResulationFactor(){
   float fResolutionFactor = ((float) Toolkit.getDefaultToolkit().getScreenResolution() / 96f);
   if(hasRetinaDisplay()){
      fResolutionFactor = fResolutionFactor * getRetinaScaleFactor().floatValue();
   }
 return fResolutionFactor;
}

Now we have a resolution factor value. Lets use it. You set this value one for each one like this.

JLabel jTestLabel = new JLabel("hello world");

Font jNewFont = jTestLabel.getFont().deriveFont(Font.Plain, jTestLabel.getFont().getSize() * getResulationFactor()); 

jTestLabel.setFont(jNewFont);

Or you can use this value just one time by overriding defaultFont value of your lookandFeel.

ziLk
  • 3,120
  • 21
  • 45
  • thank you, if I use this setting , UI object's size will be change simultaneously or only font size. – Ives Apr 15 '16 at 01:26
  • Font size leads to resize UI objects because of the object's layout. However if your UI object has an specific comfigurations you need to set this value for them too. For example jLabel.setBorder(new EmptyBorder(2, 2, 2, 2); you should change like this to be able to change all UI configuration in terms of user's screen resolution jLabel.setBorder(new EmptyBorder(2 * factor, 2* factor, 2* factor, 2* factor); – ziLk Apr 15 '16 at 07:41