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

getSelectedFile()in JFileChooser(Motif look and feel) returns wrong file path

XMLWordPrintable

    • beta
    • generic
    • generic



      Name: krT82822 Date: 10/11/99


      10/11/99 eval1127@eng -- figured this was a dupe, but couldn't find one. (see also 4199911, a problem only in Metal)

      Compile and run the program below.
      Browse the file system (up and down). Then, double click on a file.
      getSelectedFile() will return a path that contains all browsed
      directories (e.g., X/Y/../X/) instead of the absolute path to
      the file. This path value is returned only in Motif look and feel,
      but not in Java look and feel.
      The problem with this returned value is that if a ChangeToParentDirectory
      function is called while in X directory in the above example then the value
      returned is ".." which might not exist and is clearly wrong.

      The program:

      import javax.swing.*;
      import java.awt.event.*;
      import java.awt.*;

      public class JFileChooserTest extends JDialog
      implements ActionListener{
         
         JFileChooser myFileChooser;
         JPanel contentPane;

         public JFileChooserTest() {
            super();

             contentPane = (JPanel) getContentPane();
             contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.X_AXIS));
             myFileChooser = new JFileChooser("/");
             contentPane.add(myFileChooser);

             
             myFileChooser.addActionListener(this);
            
             setSize(new Dimension(500,500));
             setVisible(true);

         }

         public void actionPerformed(ActionEvent ae){
            
            Object source = ae.getSource();

            if (source == myFileChooser) {
               System.out.println(myFileChooser.getSelectedFile());
            }
         }


         public static void main(String args[]){

            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
             }
       
             catch (Exception e){
              System.out.println("Error setting the look and feel");
             }
          
          JFileChooserTest x = new JFileChooserTest();

         }
      }
      (Review ID: 96344)
      ======================================================================

            leifs Leif Samuelsson (Inactive)
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: