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

REGRESSION: JFileChooser: ListSelectionModel displays wrong selection

XMLWordPrintable

    • x86
    • linux, windows_2000

        Name: jk109818 Date: 05/01/2003


        FULL PRODUCT VERSION :
        java version "1.4.0"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
        Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)


        FULL OPERATING SYSTEM VERSION :
        glibc-2.2.4-31
        Linux 2.4.2-2 i686 unknown
        Red Hat Linux release 7.1

        A DESCRIPTION OF THE PROBLEM :
        JFileChooser is often reused over multiple requests for the
        user to select a file, but this can result in
        inconsistencies between what is displayed in the file
        choosers JTextField and the JList (which should display the
        currently selected file). This inconsistency can cause
        getSelectedFile to return <null> when the user double-clicks
        a file in the list. This bug appears have been introduced in
        jdk1.4.x, as I cannot reproduce it in jdk1.3.1_02

        The problem appears to be with the handling of the
        ListSelectionModel for the JList, which become inconsistent
        with the JFileChoosers notion of what it's 'selectedFile'
        is. This can happen in all file selection modes, and even
        if the file chooser has not yet been used.

        A particularly bad situation:

          MultipleFileSelection enabled, user selects single file,
        later dialog is redisplayed after clearing the selection,
        and the JTextField is empty, but the previously selected
        file is still highlighted. User double clicks on same file
        as before, dialog dissappears, but selected file is reported
        as null.


        REGRESSION. Last worked in version 1.3.1

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Here are some standalone examples that reproduce the bugs
        with the code below

        Click the "Select a File..." button
          Select a file and accept by any means (doubleclick, click
        ok, press return)
          Notice on stderr your selection was confirmed
          click select button again
            * the JTextField correctly shows your previous selection
        as it has
            * not been modified since the selection, but the file is not
            * highlighted in the JList, as the JListModel was wrongfully
            * cleared.
            *** This problem does not appear in jdk1.3.1_02 ***

        Select 'Enable multiple files'
          click select button
          select any number of files and accept. stderr confirms
          click select button again
            * both the JTextField and the JList show the previous
        selection
            * which is consistent as programmed/used, but
        inconsistent with above.

        Select 'Enable multiple files' and 'Clear selection...'
          click select button
          select a single file and accept. stderr confirms
          click select button
            * The JTextField is correctly empty, but the previously
        selected file
            * is still highlighed in the JList.
          single click the highlighted file
            * The JTextField does not update to display your current
        selection
          Press return or click the open button
            * The JFileChooser follows the programmers intent to
        have cleared
            * the selected file, even though the user has explicitly
        selected
            * one
          double click highlighted file
            * note that JFileChooser dissappears, but stdout shows
        that it
            * never saw the selection
            *** This problem does not appear in jdk1.3.1_02 ***

        Create a file named "A.txt" in the file choosers default
        directory
        Enable 'Select multiple files' and 'Pre-Select...'
          click select button
          select a single file _other_ than 'A.txt' and accept.
        stderr confirms
          click select button
            * JTextField shows 'A.txt' as selected, but JList highlights
            * previous selection
          single click the highlighted file
            * as above example JTextField does not update
          double click highlighted file
            * as above JFileChooser dissappears, but stdout shows
        that 'A.txt'
            * was selected, despite user explicitly double-clicking
        a different
            * file. The problem of a programatically selected text
        not being
            * highlighted existed in jdk1.3.1_02, though it would at
        least
            * de-highlight the previous selection


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.awt.*;
        import java.io.*;
        import javax.swing.*;
        import java.awt.event.*;

        public class FileChooserBug extends JFrame {
          
          public FileChooserBug() {
            super("File Chooser Bug");
            
            final JFileChooser fc = new JFileChooser();
            // this setting doesn't matter
            fc.setFileSelectionMode (JFileChooser.FILES_ONLY);

            final JCheckBox multiCheck = new JCheckBox("Enable multiple file selection");
            multiCheck.setSelected (fc.isMultiSelectionEnabled());
            multiCheck.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                  fc.setMultiSelectionEnabled(e.getStateChange()==ItemEvent.SELECTED);
                }
              });
            final JCheckBox clearCheck = new JCheckBox("Clear selection before re-display");
            final JCheckBox preSelectCheck = new JCheckBox("Pre-Select \"A.txt\" before
        re-display");
            JButton openButton = new JButton("Select a File...");
            openButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                  
                  if (clearCheck.isSelected())
                    fc.setSelectedFile(null);
                  // Workaround
                  // fc.setMultiSelectionEnabled(false);
                  // fc.setMultiSelectionEnabled(multiCheck.isSelected());
                  if (preSelectCheck.isSelected())
                    fc.setSelectedFile (new File("A.txt"));

                  int returnVal = fc.showOpenDialog(FileChooserBug.this);
                  
                  if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    System.err.println("File Selected: "+file);
                  } else {
                    System.err.println("Selection cancelled by user.");
                  }
                }
              });
            Box box = Box.createVerticalBox();
            box.add(multiCheck);
            box.add(clearCheck);
            box.add(preSelectCheck);
            box.add(openButton);
            getContentPane().add(box);
          }

          public static void main(String[] args) {
            JFrame frame = new FileChooserBug();
            
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                  System.exit(0);
                }
              });
            
            frame.pack();
            frame.setVisible(true);
          }
        }

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

        CUSTOMER WORKAROUND :
        1) To clear previously selected files, call
        setFileSelected(null), and if it is to remain or switch to
        multi-file-selection-mode then cycle to
        single-file-selection, then back to multi-file-selection
        before re-displaying the file chooser. That will to deselect
        previously selected files in the JList. The JTextField will
        be consistent, and while the list is inconsistent if you
        wish to pre-select files, it doesn't report false positives,
        confusing the user, effectively eliminating the problem of
        doubleclicking a preselected file while a different has been
        programmatically selected.

        2) You can do the same thing as above by changing
        directories instead of cycling single-file-selection

        3) you can also just use a new JFileChooser each time... but
        that get's the same results and takes time/memory at each
        instantiation.

        Release Regression From : 1.3.1_02
        The above release value was the last known release where this
        bug was known to work. Since then there has been a regression.

        (Review ID: 178837)
        ======================================================================

              rupashka Pavel Porvatov (Inactive)
              jkimsunw Jeffrey Kim (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: