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

Changing File Selection Mode Erases Selected File Name

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Microsoft Windows 11
      x64
      java 17.0.12 2024-07-16 LTS
      Java(TM) SE Runtime Environment (build 17.0.12+8-LTS-286)
      Java HotSpot(TM) 64-Bit Server VM (build 17.0.12+8-LTS-286, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      When using javax/swing/plaf/metal/MetalFileChooserUI.java, if you call setFileSelectionMode to change your file selection mode (to either 0 or 2) from mode 1 while attempting to save a file, setFileName(null) is called, meaning the selected filename is overwritten and will no longer be displayed.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Have a new file you want to save
      2. Be in DIRECTORIES_ONLY fileSelectionMode
      3. Switch to FILES_ONLY selection mode
      4. Attempt to save the file
      5. Observe that you are in the correct directory but the file save dialogue box is empty

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect to see "fakeFile.txt" in the save file dialogue box
      ACTUAL -
      The save file dialogue box is empty and ((MetalFileChooserUI)fc.getUI()).getFileName() is null

      ---------- BEGIN SOURCE ----------
      // File: MetalFileChooserBugDemo.java

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.ActionEvent;
      import java.io.File;
      import javax.swing.plaf.metal.MetalFileChooserUI;

      public class MetalFileChooserBugDemo {

          public static void main(String[] args) {
              SwingUtilities.invokeLater(() -> {
                  try {
                      // 1) Force Metal Look & Feel
                      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                      // 2) Tell Swing to use MetalFileChooserUI for JFileChooser
                      UIManager.put("FileChooserUI", "javax.swing.plaf.metal.MetalFileChooserUI");
                  } catch (Exception ex) {
                      ex.printStackTrace();
                  }

                  JFrame frame = new JFrame("MetalFileChooserUI Save‑Mode Bug Demo");
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setLayout(new FlowLayout(FlowLayout.CENTER));
                  frame.add(new JButton(new AbstractAction("Replicate Save Bug") {
                      @Override
                      public void actionPerformed(ActionEvent e) {
                          replicateBug(frame);
                      }
                  }));
                  frame.pack();
                  frame.setLocationRelativeTo(null);
                  frame.setVisible(true);
              });
          }

          private static void replicateBug(Component parent) {
              // Now when we construct it, JFileChooser will pick up your override
              JFileChooser fc = new JFileChooser();
              // Re-install the UI to pick up our UIManager override
              fc.updateUI();

              // Create a new file to save
              fc.setSelectedFile(new File(fc.getCurrentDirectory().getAbsolutePath(),"fakeFile.txt"));

              // 1) Start in DIRECTORIES_ONLY
              fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
              // 2) Then switch to FILES_ONLY
              fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

              // 3) Show Save dialog
              File sel = fc.getSelectedFile();
              JOptionPane.showMessageDialog(parent,
                      "You chose:\n" + sel.getAbsolutePath() + " \nbut the dialog sees\n" + ((MetalFileChooserUI)fc.getUI()).getFileName(),
                      "Selection", JOptionPane.INFORMATION_MESSAGE);
              fc.showSaveDialog(parent);
          }
      }
      ---------- END SOURCE ----------

            tr Tejesh R
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: