Java automatically adjusting to the Windows 7 font size adjustment -


in windows 7, if change font size via control panel->appearance , personalization -> display "make text , other items larger or smaller", adjusts not menu sizes, text content size of apps notepad, wordpad, firefox.

is there way java automatically scale font without having manually scale it?

there 2 parts this:

  1. getting components, fonts, etc scale
  2. getting layouts scale

for swing, first part easy - starts 1 call.

uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); 

on windows, cause respect small/large fonts (dpi) setting.

here 2 screenshots quick test app threw together, demonstrating how looks on machine in windows 7 @ 96dpi (normal font size) , @ 144dpi (150%)

first default font size sample:

swing app windows using normal font sizes

now larger (150%) font size set:

swing app windows using larger font sizes

there no code change between runs, logging out & in new dpi settings. set fixed frame size on purpose demonstrate container not scaling in size, caused label pushed down in order fit.

here source code - cut & paste , run yourself:

import java.awt.flowlayout;  import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.swingutilities; import javax.swing.uimanager;  public class swingfonttest {     private static void creategui()     {         jbutton button = new jbutton("my button text");         jlabel label = new jlabel("and label");          jpanel panel = new jpanel(new flowlayout());         panel.add(button);         panel.add(label);          jframe frame = new jframe("title!");         frame.setcontentpane(panel);         frame.setsize(300,125);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setvisible(true);     }      public static void main(string[] args)     {         try         {             uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());         }         catch (exception e)         {             e.printstacktrace();         }          swingutilities.invokelater(new runnable() {             @override             public void run()             {                 creategui();             }         });     }  } 

the & feel supplies default sizing, gui author use scalable units in layouts. take effort (scalable layouts pain on webpages too!), it's attainable.

i recommend using layout formlayout let define layouts in dialog units (dlu), these scale dpi. enable make containers scale in size , should limit behaviors label moving next line due sizing. if size of frame determined using dialog units made same, larger.

it late - that's now.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -