FileChooser: fixed occasional NPE in FlatShortcutsPanel on Windows (issue #718)

This commit is contained in:
Karl Tauber
2023-08-23 19:40:59 +02:00
parent c953ff84d0
commit 581c64b601
2 changed files with 29 additions and 11 deletions

View File

@@ -1,6 +1,14 @@
FlatLaf Change Log FlatLaf Change Log
================== ==================
## 3.3-SNAPSHOT
#### Fixed bugs
- FileChooser: Fixed occasional NPE in `FlatShortcutsPanel` on Windows. (issue
#718)
## 3.2 ## 3.2
#### New features and improvements #### New features and improvements

View File

@@ -29,6 +29,7 @@ import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.io.File; import java.io.File;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.function.Function; import java.util.function.Function;
import javax.swing.AbstractButton; import javax.swing.AbstractButton;
import javax.swing.Box; import javax.swing.Box;
@@ -408,7 +409,7 @@ public class FlatFileChooserUI
protected final File[] files; protected final File[] files;
protected final JToggleButton[] buttons; protected final JToggleButton[] buttons;
protected final ButtonGroup buttonGroup; protected final ButtonGroup buttonGroup = new ButtonGroup();
@SuppressWarnings( "unchecked" ) @SuppressWarnings( "unchecked" )
public FlatShortcutsPanel( JFileChooser fc ) { public FlatShortcutsPanel( JFileChooser fc ) {
@@ -427,19 +428,22 @@ public class FlatFileChooserUI
File[] files = getChooserShortcutPanelFiles( fsv ); File[] files = getChooserShortcutPanelFiles( fsv );
if( filesFunction != null ) if( filesFunction != null )
files = filesFunction.apply( files ); files = filesFunction.apply( files );
this.files = files;
// create toolbar buttons // create toolbar buttons
buttons = new JToggleButton[files.length]; ArrayList<File> filesList = new ArrayList<>();
buttonGroup = new ButtonGroup(); ArrayList<JToggleButton> buttonsList = new ArrayList<>();
for( int i = 0; i < files.length; i++ ) { for( File file : files ) {
// wrap drive path if( file == null )
if( fsv.isFileSystemRoot( files[i] ) ) continue;
files[i] = fsv.createFileObject( files[i].getAbsolutePath() );
// wrap drive path
if( fsv.isFileSystemRoot( file ) )
file = fsv.createFileObject( file.getAbsolutePath() );
File file = files[i];
String name = getDisplayName( fsv, file ); String name = getDisplayName( fsv, file );
Icon icon = getIcon( fsv, file ); Icon icon = getIcon( fsv, file );
if( name == null )
continue;
// remove path from name // remove path from name
int lastSepIndex = name.lastIndexOf( File.separatorChar ); int lastSepIndex = name.lastIndexOf( File.separatorChar );
@@ -454,15 +458,21 @@ public class FlatFileChooserUI
// create button // create button
JToggleButton button = createButton( name, icon ); JToggleButton button = createButton( name, icon );
File f = file;
button.addActionListener( e -> { button.addActionListener( e -> {
fc.setCurrentDirectory( file ); fc.setCurrentDirectory( f );
} ); } );
add( button ); add( button );
buttonGroup.add( button ); buttonGroup.add( button );
buttons[i] = button;
filesList.add( file );
buttonsList.add( button );
} }
this.files = filesList.toArray( new File[filesList.size()] );
this.buttons = buttonsList.toArray( new JToggleButton[buttonsList.size()] );
directoryChanged( fc.getCurrentDirectory() ); directoryChanged( fc.getCurrentDirectory() );
} }