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

UIManager.setLookAndFeel resets DefaultCaret's update policy in JTextArea

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 6
    • client-libs

      FULL PRODUCT VERSION :
      java version "1.6.0-beta2"
      Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)
      Java HotSpot(TM) Client VM (build 1.6.0-beta2-b86, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      My application uses a JTextArea to present logged information to the user. This information is written to the JTextArea by the AWT event thread as well as other threads. In order to have the embedding JScrollPane always scroll down to the position of the DefaultCaret, the DefaultCaret's update policy has to be changed to DefaultCaret.ALWAYS_UPDATE.

      Changing the applications's look-and-feel via UIManager.setLookAndFeel (to Metal or System) resets the DefaultCaret's update policy to its default policy UPDATE_WHEN_ON_EDT. This results in a failure of the JScrollPane to scroll down to the caret's position when a none-EDT-thread writes to the JTextArea.

      I can't see any reason why a change to the look-and-feel should have an effect on a caret's update policy and I cannot find any part of the documentation that indicates that behaviour. Therefore I consider it a bug.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please run the attached source code and check the output to System.out.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The output to System.out should look like this:

      update policy = 0
      update policy = 2
      update policy = 2
      update policy = 2
      update policy = 2
      ACTUAL -
      The actual output looks like this:

      update policy = 0
      update policy = 2
      update policy = 0
      update policy = 2
      update policy = 0

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * Bugdemo.java
       */

      import javax.swing.JFrame;
      import javax.swing.JTextArea;
      import javax.swing.UIManager;
      import javax.swing.SwingUtilities;
      import javax.swing.text.DefaultCaret;

      public class Bugdemo extends JFrame {
          
          static JTextArea jTextArea;
          static Bugdemo bugdemo;
          
          public Bugdemo() {
              
              bugdemo = this;
              JFrame.setDefaultLookAndFeelDecorated(true);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              
              jTextArea = new JTextArea();
              getContentPane().add(jTextArea);
              
              pack();
              setVisible(true);
          }
          
          public static void main(String[] args) {
              
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      new Bugdemo();
                      
                      System.out.println("update policy = " + ((DefaultCaret)jTextArea.getCaret()).getUpdatePolicy());
                      ((DefaultCaret)jTextArea.getCaret()).setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
                      System.out.println("update policy = " + ((DefaultCaret)jTextArea.getCaret()).getUpdatePolicy());
                      try {
                          UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                      } catch (Exception e) {
                      }
                      SwingUtilities.updateComponentTreeUI(Bugdemo.bugdemo);
                      System.out.println("update policy = " + ((DefaultCaret)jTextArea.getCaret()).getUpdatePolicy());
                      ((DefaultCaret)jTextArea.getCaret()).setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
                      System.out.println("update policy = " + ((DefaultCaret)jTextArea.getCaret()).getUpdatePolicy());
                      try {
                          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                      } catch (Exception e) {
                      }
                      SwingUtilities.updateComponentTreeUI(Bugdemo.bugdemo);
                      System.out.println("update policy = " + ((DefaultCaret)jTextArea.getCaret()).getUpdatePolicy());
                  }
              });
          }
      }
      ---------- END SOURCE ----------

            peterz Peter Zhelezniakov
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: