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

JFileChooser.setSelectedFiles() doesn't highlight when using Windows Look&Feels

    XMLWordPrintable

    Details

      Description

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7600]

      A DESCRIPTION OF THE PROBLEM :
      We are developing a backup application where users can browse increments with a JFileChooser. When switching between increments, we restore the file selection. Everything works as expected as long as we use Metal or Nimbus look&feel: the file selection is restored and the selected files are highlighted.
      But because Metal and Nimbus feel alien on Windows we use the system look&feel on Windows.

      Unfortunately, when using the Windows look&feel the selected files are not highlighted. This makes the feature "keep the file selection when switching between increments" useless on Windows because highlighting the selected files is the only useful visual feedback, the list of selected filenames in the textfield below is not.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please execute the included test case. It defaults to MetalLookAndFeel. Please notice that the programmatical selecting of the COPYRIGHT file highlights it, which gives a good visual feedback about the selected files.

      Now comment the MetalLookAndFeel line, uncomment the WindowsLookAndFeel line and execute the test case again. Please notice that the programmatical selecting of the COPYRIGHT file does no longer highlights it, which gives a bad visual feedback about the selected files.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expected that calling JFileChooser.setSelectedFiles() when using the WindowsLookAndFeel also highlights the selected files as it does when using MetalLookAndFeel or NimbusLookAndFeel.
      ACTUAL -
      The actual result was that calling JFileChooser.setSelectedFiles() when using the WindowsLookAndFeel does not highlight the selected files as it does when using MetalLookAndFeel or NimbusLookAndFeel.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.EventQueue;
      import java.beans.*;
      import java.io.File;
      import javax.swing.*;
      import javax.swing.plaf.FileChooserUI;
      import javax.swing.plaf.basic.BasicFileChooserUI;

      public class JFileChooserTest implements PropertyChangeListener {

          private JFileChooser fileChooser;
          private final File copyright;

          public JFileChooserTest() {
              try {
                  /*
                   * selected files are highlighted when using these LookAndFeels
                   */
                  UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                  //UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

                  /*
                   * selected files are NOT highlighted when using these LookAndFeels
                   */
                  //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
                  //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                  //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
                  //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
              } catch (Exception ex) {
                  ex.printStackTrace();
              }

              File javaHome = new File(System.getProperty("java.home"));
              copyright = new File(javaHome, "COPYRIGHT");
              fileChooser = new JFileChooser();
              fileChooser.setMultiSelectionEnabled(true);
              FileChooserUI fileChooserUI = fileChooser.getUI();
              if (fileChooserUI instanceof BasicFileChooserUI) {
                  BasicFileChooserUI basicFileChooserUI =
                          (BasicFileChooserUI) fileChooserUI;
                  basicFileChooserUI.getModel().addPropertyChangeListener(this);
              }
              fileChooser.setCurrentDirectory(javaHome);
              fileChooser.showOpenDialog(null);
          }

          public static void main(String[] args) {
              new JFileChooserTest();
          }

          public void propertyChange(PropertyChangeEvent evt) {
              if ("busy".equals(evt.getPropertyName())
                      && evt.getNewValue() == Boolean.FALSE) {
                  // The file loading thread is now no longer busy.
                  // But calling setSelectedFiles() right now sometimes triggers NPEs
                  // when using MetalLookAndFeel or sometimes does not select the file
                  // when using NimbusLookAndFeel.
                  // Therefore we must postpone the call to setSelectedFiles() again
                  // for a little while until the bugs above are fixed.
                  new Thread() {

                      @Override
                      public void run() {
                          try {
                              sleep(1000);
                          } catch (InterruptedException ex) {
                              ex.printStackTrace();
                          }
                          EventQueue.invokeLater(new Runnable() {

                              public void run() {
                                  fileChooser.setSelectedFiles(new File[]{copyright});
                              }
                          });
                      }
                  }.start();
              }
          }
      }

      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Imported:
              Indexed: