[Schmitzm-commits] r1858 - trunk/schmitzm-core/src/main/java/de/schmitzm/swing

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Feb 8 11:18:43 CET 2012


Author: mojays
Date: 2012-02-08 11:18:43 +0100 (Wed, 08 Feb 2012)
New Revision: 1858

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
Log:
SwingUtil: new method to increase all L&F font sizes (by factor)

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-02-07 15:56:11 UTC (rev 1857)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-02-08 10:18:43 UTC (rev 1858)
@@ -102,7 +102,9 @@
 import javax.swing.JTree;
 import javax.swing.JViewport;
 import javax.swing.SwingUtilities;
+import javax.swing.UIDefaults;
 import javax.swing.UIManager;
+import javax.swing.plaf.FontUIResource;
 import javax.swing.table.TableCellEditor;
 import javax.swing.table.TableCellRenderer;
 import javax.swing.text.JTextComponent;
@@ -172,7 +174,7 @@
 	}
 
 	/** A default font. */
-	public static final Font DEFAULT_FONT = new Font(Font.DIALOG, Font.PLAIN,
+	public static Font DEFAULT_FONT = new Font(Font.DIALOG, Font.PLAIN,
 			12);
 
     // ****************************************************************************
@@ -1165,6 +1167,42 @@
 			return "ITALIC";
 		return "PLAIN";
 	}
+	
+	/**
+	 * Increases the font size for <b>ALL</b> {@link Font} properties in
+	 * the L&F. Should be called before any swing component is initialized
+	 * in application!<br>
+	 * <br>
+	 * Sample: <a href="http://stackoverflow.com/questions/1043872/are-there-any-built-in-methods-in-java-to-increase-font-size">
+	 * http://stackoverflow.com/questions/1043872/are-there-any-built-in-methods-in-java-to-increase-font-size</a>
+	 * @param factor factor to increase the font size with (e.g. 1.5 increases
+	 *               the font size by 50%; 2.0 doubles the font size)
+	 */
+	public static void increaseUIFontSize(double factor) {
+	  // TODO: also increase:
+	  // - table row height
+	  // - list row height
+	  // - check box size
+	  // - JDateChooser width
+	  UIDefaults defaults = UIManager.getDefaults();
+	  int i = 0;
+	  for (Enumeration e = defaults.keys(); e.hasMoreElements(); i++) {
+	    Object key = e.nextElement();
+	    Object value = defaults.get(key);
+	    if (value instanceof Font) {
+	      Font font = (Font) value;
+	      int newSize = (int)Math.round(font.getSize() * factor);
+	      if (value instanceof FontUIResource) {
+	        defaults.put(key, new FontUIResource(font.getName(), font.getStyle(), newSize));
+	      } else {
+	        defaults.put(key, new Font(font.getName(), font.getStyle(), newSize));
+	      }
+	    }
+	  }
+	  
+	  // Increase default font
+	  DEFAULT_FONT = DEFAULT_FONT.deriveFont( (float)Math.round(DEFAULT_FONT.getSize() * factor) );
+	}
 
 	/**
 	 * Prueft, ob eine Datei existiert und fordert gegenfalls zum Bestaetigen



More information about the Schmitzm-commits mailing list