1 <!doctype html>
   2 <html lang="en">
   3 <head>
   4   <meta charset="utf-8"/>
   5   <title>AWT Desktop Properties</title>
   6 </head>
   7 <!--
   8  Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
   9  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  10 
  11  This code is free software; you can redistribute it and/or modify it
  12  under the terms of the GNU General Public License version 2 only, as
  13  published by the Free Software Foundation.  Oracle designates this
  14  particular file as subject to the "Classpath" exception as provided
  15  by Oracle in the LICENSE file that accompanied this code.
  16 
  17  This code is distributed in the hope that it will be useful, but WITHOUT
  18  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  20  version 2 for more details (a copy is included in the LICENSE file that
  21  accompanied this code).
  22 
  23  You should have received a copy of the GNU General Public License version
  24  2 along with this work; if not, write to the Free Software Foundation,
  25  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  26 
  27  Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  28  or visit www.oracle.com if you need additional information or have any
  29  questions.
  30 -->
  31 
  32 <body>
  33 <main role="main">
  34 <h1>AWT Desktop Properties</h1>
  35 
  36 The following refers to standard AWT desktop properties that
  37 may be obtained via the
  38 <a href="../Toolkit.html#getDesktopProperty(java.lang.String)">
  39 <code>Toolkit.getDesktopProperty</code></a> method.
  40 <p>
  41 Each desktop property is named by a unique string, which
  42 is the "name" of that property.
  43 <p>
  44 Desktop properties supported by the AWT but not documented
  45 elsewhere - typically because there is no suitable
  46 method or class - are documented here.
  47 <p>
  48 Desktop properties documented elsewhere are those which are
  49 tightly coupled with a method or class which documents them.
  50 <p>
  51 Since desktop properties abstract an underlying platform
  52 setting, they may not be available in environments that do
  53 not support them. In the event that a desktop property is
  54 unavailable for any reason, the implementation will return
  55 <code>null</code>.
  56 <p>
  57 The following table summarizes the desktop properties documented
  58 here, and their value types.
  59 <br><br>
  60 <table>
  61 <tr bgcolor="#ccccff">
  62 <th>Property Name</th>
  63 <th>Value Type</th>
  64 <th>Summary Description</th>
  65 </tr>
  66 <tr>
  67 <td><A href="#awt.font.desktophints">awt.font.desktophints</A></td>
  68 <td><a href="../../../../java.base/java/util/Map.html">java.util.Map</a></td>
  69 <td>Font smoothing (text antialiasing) settings.</td>
  70 </tr>
  71 <tr>
  72 <td><A href="#sun.awt.enableExtraMouseButtons">sun.awt.enableExtraMouseButtons</A></td>
  73 <td><a href="../../../../java.base/java/lang/Boolean.html">java.lang.Boolean</a></td>
  74 <td>Controls if mouse events from extra buttons are to be generated or not</td>
  75 </tr>
  76 </table>
  77 
  78 <h2>Desktop Font Rendering Hints</h2>
  79 <b>Desktop Property: <a id="awt.font.desktophints">"awt.font.desktophints"</A></b>
  80 <p>
  81 Modern desktops support various forms of text antialiasing (font smoothing).
  82 <p>
  83 These are applied by platform-specific heavyweight components.
  84 However an application may want to render text using the same text
  85 antialiasing on a drawing surface or lightweight (non-platform) component using
  86 <a href="../Graphics2D.html"> <code>Graphics2D</code></a> methods.
  87 This is particularly important when creating
  88 <a href="../../../javax/swing/JComponent.html"> Swing components</a> which
  89 are required to appear consistent with native desktop components or other
  90 Swing components.
  91 
  92 <h3>Basic Usage</h3>
  93 The standard desktop property named
  94 <b>"awt.font.desktophints"</b>
  95 can be used to obtain the rendering hints that best match the desktop settings.
  96 
  97 The return value is a
  98 <a href="../../util/Map.html"> Map</a> of
  99 <a href="../RenderingHints.html"> <code>RenderingHints</code></a> which
 100 can be directly applied to a <code>Graphics2D</code>.
 101 <p>
 102 It is a <code>Map</code> as more than one hint may be needed.
 103 If non-null this can be directly applied to the <code>Graphics2D</code>.
 104 <pre><code>
 105 Toolkit tk = Toolkit.getDefaultToolkit();
 106 Map map = (Map)(tk.getDesktopProperty("awt.font.desktophints"));
 107 if (map != null) {
 108     graphics2D.addRenderingHints(map);
 109 }
 110 </code></pre>
 111 <h3>Advanced Usage Tips</h3>
 112 
 113 <h4>Listening for changes</h4>
 114 <p>
 115 An application can listen for changes in the property
 116 using a <a href="../../beans/PropertyChangeListener.html">
 117 <code>PropertyChangeListener</code></a> :
 118 <pre><code>
 119 tk.addPropertyChangeListener("awt.font.desktophints", pcl);
 120 </code></pre>
 121 Listening for changes is recommended as users can, on rare occasions,
 122 reconfigure a desktop environment whilst applications are running
 123 in a way that may affect the selection of these hints, and furthermore
 124 many desktop environments support dynamic reconfiguration of these
 125 running applications to conform to the new settings.
 126 <p>
 127 There is no direct way to discover if dynamic reconfiguration
 128 is expected of running applications but the default assumption
 129 should be that it is expected, since most modern desktop environments
 130 do provide this capability.
 131 <h4>Text Measurement</h4>
 132 <p>
 133 Text always needs to be measured using the same
 134 <a href="../font/FontRenderContext.html"> <code>FontRenderContext</code></a>
 135 as used for rendering. The text anti-aliasing hint is a component of
 136 the <code>FontRenderContext</code>.
 137 A <a href="../FontMetrics.html"> <code>FontMetrics</code></a>
 138 obtained from the <code>Graphics</code> object on which the hint
 139 has been set will measure text appropriately.
 140 This is not a unique requirement for clients that specify this hint
 141 directly, since the value of the <code>FontRenderContext</code> should
 142 never be assumed, so is discussed here principally as a reminder.
 143 <h4>Saving and restoring Graphics State</h4>
 144 <p>
 145 Sometimes an application may need to apply these hints on a shared
 146 Graphics only temporarily, restoring the previous values after they
 147 have been applied to text rendering operations.
 148 The following sample code shows one way to do this.
 149 <pre><code>
 150 /**
 151   * Get rendering hints from a Graphics instance.
 152   * "hintsToSave" is a Map of RenderingHint key-values.
 153   * For each hint key present in that map, the value of that
 154   * hint is obtained from the Graphics and stored as the value
 155   * for the key in savedHints.
 156   */
 157 RenderingHints getRenderingHints(Graphics2D g2d,
 158                                   RenderingHints hintsToSave,
 159                                   RenderingHints savedHints) {
 160      if (savedHints == null) {
 161          savedHints = new RenderingHints(null);
 162      } else {
 163          savedHints.clear();
 164      }
 165      if (hintsToSave.size() == 0) {
 166          return savedHints;
 167      }
 168      /* RenderingHints.keySet() returns Set&lt;Object&gt; */
 169      for (Object o : hintsToSave.keySet()) {
 170          RenderingHints.Key key = (RenderingHints.Key)o;
 171          Object value = g2d.getRenderingHint(key);
 172          savedHints.put(key, value);
 173      }
 174      return savedHints;
 175 }
 176 
 177 
 178 Toolkit tk = Toolkit.getDefaultToolkit();
 179 Map map = (Map)(tk.getDesktopProperty("awt.font.desktophints"));
 180 Map oldHints;
 181 if (map != null) {
 182      oldHints = getRenderingHints(graphic2D, map, null);
 183      graphics2D.addRenderingHints(map);
 184      ..
 185      graphics2D.addRenderingHints(oldHints);
 186 }
 187 </code></pre>
 188 
 189 <h3>Details</h3>
 190 <ul>
 191 <li>The return value will always be null or a <code>Map</code>
 192 <br><br>
 193 <li>If the return value is null, then no desktop properties are available,
 194 and dynamic updates will not be available. This is a typical behaviour if
 195 the JDK does not recognise the desktop environment, or it is one which
 196 has no such settings. The <b>Headless</b> toolkit is one such example.
 197 Therefore it is important to test against null before using the map.
 198 <br><br>
 199 <li>If non-null the value will be a <code>Map</code> of
 200 <code>RenderingHints</code> such that every key is an instance of
 201 <code>RenderingHints.Key</code> and the value is a legal value for that key.
 202 <br><br>
 203 <li>The map may contain the default value for a hint. This is
 204 needed in the event there is a previously a non-default value for the hint
 205 set on the <code>Graphics2D</code>. If the map did not contain
 206 the default value, then <code>addRenderingHints(Map)</code> would leave
 207 the previous hint which may not correspond to the desktop setting.
 208 <p>
 209 An application can use <code>setRenderingHints(Map)</code> to reinitialise
 210 all hints, but this would affect unrelated hints too.
 211 <br><br>
 212 <li>A multi-screen desktop may support per-screen device settings in which
 213 case the returned value is for the default screen of the desktop.
 214 An application may want to use the settings for the screen on
 215 which they will be applied.
 216 The per-screen device hints may be obtained by per-device property names
 217 which are constructed as the String concatenation
 218 <pre><code>
 219 "awt.font.desktophints" + "." + GraphicsDevice.getIDstring();
 220 </code></pre>
 221 <p>
 222 An application can also listen for changes on these properties.
 223 <p>
 224 However this is an extremely unlikely configuration, so to help
 225 ease of development, if only a single, desktop-wide setting is supported,
 226 then querying each of these per-device settings will return null.
 227 So to determine if there are per-device settings it is sufficient to
 228 determine that there is a non-null return for any screen device using
 229 the per-device property name.
 230 </ul>
 231 <h2>Mouse Functionality</h2>
 232 <b>Desktop Property: <a id="sun.awt.enableExtraMouseButtons">"sun.awt.enableExtraMouseButtons"</A></b>
 233 <p>
 234 This property determines if events from extra mouse buttons (if they are exist and are
 235 enabled by the underlying operating system) are allowed to be processed and posted into
 236 {@code EventQueue}.
 237 <br>
 238 The value could be changed by passing "sun.awt.enableExtraMouseButtons"
 239 property value into java before application starts. This could be done with the following command:
 240 <pre>
 241 java -Dsun.awt.enableExtraMouseButtons=false Application
 242 </pre>
 243 Once set on application startup, it is impossible to change this value after.
 244 <br>
 245 Current value could also be queried using getDesktopProperty("sun.awt.enableExtraMouseButtons")
 246 method.
 247 <br>
 248 If the property is set to {@code true} then
 249 <ul>
 250 <li> it is still legal to create {@code MouseEvent} objects with
 251 standard buttons and, if the mouse has more
 252 then three buttons, it is also legal to use buttons from the range started
 253 from 0 up to {@link java.awt.MouseInfo#getNumberOfButtons() getNumberOfButtons()}.
 254 
 255 <li> it is legal to use standard button masks when using {@code Robot.mousePress()}
 256 and {@code Robot.mouseRelease()} methods and, if the mouse has more then three buttons,
 257 it is also legal to use masks for existing extended mouse buttons.
 258 That way, if there are more then three buttons on the mouse then it is allowed to
 259 use button masks corresponding to the buttons
 260 in the range from 1 up to {@link java.awt.MouseInfo#getNumberOfButtons() getNumberOfButtons()}
 261 </ul>
 262 <br>
 263 If the property is set to {@code false} then
 264 <ul>
 265 <li> it is legal to create {@code MouseEvent} objects with standard buttons
 266 only: {@code NOBUTTON}, {@code BUTTON1}, {@code BUTTON2} and
 267 {@code BUTTON3}
 268 <li> it is legal to use standard button masks only:
 269 {@code InputEvent.BUTTON1_DOWN_MASK}, {@code InputEvent.BUTTON2_DOWN_MASK},
 270 {@code InputEvent.BUTTON3_DOWN_MASK}
 271 </ul>
 272 
 273 This property should be used when there is no need in listening mouse events fired as a result of
 274 activity with extra mouse button.
 275 By default this property is set to {@code true}.
 276 </main>
 277 </body>
 278 </html>