Ives
Ives

Reputation: 555

Swing GUI layout is strange in 4K panel

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?

Upvotes: 1

Views: 941

Answers (1)

ziLk
ziLk

Reputation: 3200

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.

Upvotes: 2

Related Questions