0

I am using Java Swing to show a FileDialog on Mac OS X when user clicks button 1, to select a PDF file as shown below:

enter image description here

After clicking "Open" the PDF is loaded fine. Then i click button 2 to show another FileDialog to select and load a text file. Strangely the FileDialog loads the last loaded directory path + automatically selects the old selected PDF file, while it appears disabled as i'm using a *.txt filter, as shown below:

enter image description here

If i click "Open" it selects the PDF file, which is a wrong behavior. How can i prevent this behavior for the FileDialog for auto-selecting the old selected file?

------------------------------------- Edit: Add code sample:

private void button1ActionPerformed() {
    FileDialog fileDialog = new FileDialog( this, "Select PDF file...", FileDialog.LOAD );
    
    FilenameFilter allFiltersAllowed = new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            if( name.toLowerCase().endsWith( ".pdf" ) ) return true;
            return false;
        }
    };
    
    fileDialog.setFilenameFilter( allFiltersAllowed );      
    fileDialog.setVisible( true );

    if( fileDialog.getFile() != null ) System.out.println( fileDialog.getFile() );  // Prints: Family Budget.pdf
}

private void button2ActionPerformed() {
    FileDialog fileDialog = new FileDialog( this, "Select Text file...", FileDialog.LOAD );
    
    FilenameFilter allFiltersAllowed = new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            if( name.toLowerCase().endsWith( ".txt" ) ) return true;
            return false;
        }
    };
    
    fileDialog.setDirectory( "" );
    fileDialog.setFile( "" );
    fileDialog.setFilenameFilter( allFiltersAllowed );      
    fileDialog.setVisible( true );

    if( fileDialog.getFile() != null ) System.out.println( fileDialog.getFile() );  // Prints: Family Budget.pdf
}

For the code above, i just click "button1" then "button2" and i get the 2 screenshots mentioned above.

Brad
  • 4,457
  • 10
  • 56
  • 93

0 Answers0