Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8277993

Java loses UI settings after changing system font anti-aliasing

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Windows Enterprise 10 Version 20H2 (Build 19042.1348)
      Java version: 11.0.12

      A DESCRIPTION OF THE PROBLEM :
      When running provided example app and changing font anti-aliasing (WIN+R "sysdm.cpl" -> System Properties -> Advanced -> Performance Settings -> Smooth edges of screen fonts) one can see that the JTextArea gets the wrong font (and stays with it also if toggle anti-aliasing again).

      The change causes https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/JComponent.html#updateUI()
      to be called. The JDoc there states:

      Resets the UI property to a value from the current look and feel. JComponent subclasses must override this method like this:
         public void updateUI() {
            setUI((SliderUI)UIManager.getUI(this);
         }
      So maybe it is not a bug but in my opinion it is since we run a huge swing application for years without any issues. Since either newer Java Version or 64 bit switch the issue occurs sporadically. And to do like in the JDoc proposed we would have to refactor hundreds of places in the code.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the example app and change font anti-aliasing (WIN+R "sysdm.cpl" -> System Properties -> Advanced -> Performance Settings -> Smooth edges of screen fonts).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Independent of font anti-aliasing change events, the UI should keep it set values, e.g. a bold font should stay bold.
      ACTUAL -
      When font anti-aliasing listeners are fired then the UI loses previously set settings, e.g. a font set to bold is set to normal.

      ---------- BEGIN SOURCE ----------
      import java.awt.Font;
      import java.awt.Toolkit;
      import java.beans.PropertyChangeListener;

      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JTextArea;
      import javax.swing.SwingUtilities;
      import javax.swing.UIManager;

      public class Main
      {
          public static void main(String[] args)
          {
           
              SwingUtilities.invokeLater(() -> {
                  
      // Toolkit tk = Toolkit.getDefaultToolkit();
      // PropertyChangeListener[] listeners = tk.getPropertyChangeListeners("awt.font.desktophints"/*SunToolkit.DESKTOPFONTHINTS*/);
      // for (PropertyChangeListener listener : listeners)
      // {
      // tk.removePropertyChangeListener("awt.font.desktophints"/*SunToolkit.DESKTOPFONTHINTS*/, listener);
      // }
                  
                  JFrame frame = new JFrame();
                  JPanel mainPanel = new JPanel();
                  JLabel label = new JLabel("labelMessage:");
                  mainPanel.add(label);
                  
                  final JTextArea textArea = new JTextArea("textAreaMessage")
                  {
      // @Override
      // public void setFont(Font f)
      // {
      // super.setFont(label.getFont());
      // }
                  };
                  textArea.setEditable(false);
                  textArea.setOpaque(false);
                  textArea.setFont(label.getFont());
                  mainPanel.add(textArea);
                  
                  frame.getContentPane().add(mainPanel);
                  frame.pack();
                  frame.setVisible(true);
              });
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      1. Override all methods like shown in the code example done for setFont
      2. Set via UIManager the defaults
      3. Use -Dswing.useSystemFontSettings=false (or -Dswing.aatext or -Dawt.useSystemAAFontSettings) BUT then the font looks ugly so not acceptable without further enhancements
      4. Remove the listener (like commented lines in the example)

      FREQUENCY : always


            prr Philip Race
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: