Class JFileChooser

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class JFileChooser extends JFileChooser
A wrapper around JFileChooser that works around security restrictions in environments with limited reflection permissions, such as Groovy scripts running under Freeplane's security manager.

Problem Description

When using JFileChooser in restricted environments (like Groovy scripts), FlatLaf's UI components may attempt to use reflection to apply styling, which can result in AccessControlExceptions with messages like:
 access denied ("java.lang.reflect.ReflectPermission" "suppressAccessChecks")
 
This occurs because FlatLaf's styling system tries to access private fields using reflection during UI component initialization, but the security manager denies these operations.

Solution

This class overrides the setUI(ComponentUI) method to wrap the UI setting operation in a AccessController.doPrivileged(PrivilegedAction) block. This allows the FlatLaf styling operations to complete successfully by temporarily elevating privileges for the necessary reflection operations.

Usage

Use this class as a drop-in replacement for JFileChooser in Groovy scripts:
 import org.freeplane.api.swing.JFileChooser
 
 def fileChooser = new JFileChooser()
 def result = fileChooser.showOpenDialog(null)
 if (result == JFileChooser.APPROVE_OPTION) {
     def selectedFile = fileChooser.selectedFile
     // Process the selected file
 }
 

Security Considerations

This class uses doPrivileged only for the specific operation of setting the UI component, which is necessary for proper FlatLaf functionality. No other privileged operations are performed, maintaining the overall security model of the scripting environment.
Since:
Freeplane 1.12.11
See Also:
  • Constructor Details

    • JFileChooser

      public JFileChooser()
      Constructs a JFileChooser pointing to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix.
    • JFileChooser

      public JFileChooser(String currentDirectoryPath)
      Constructs a JFileChooser using the given path. Passing in a null string causes the file chooser to point to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix.
      Parameters:
      currentDirectoryPath - a String giving the path to a file or directory
    • JFileChooser

      public JFileChooser(File currentDirectory)
      Constructs a JFileChooser using the given File as the path. Passing in a null file causes the file chooser to point to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix.
      Parameters:
      currentDirectory - a File object specifying the path to a file or directory
    • JFileChooser

      public JFileChooser(FileSystemView fsv)
      Constructs a JFileChooser using the given FileSystemView.
      Parameters:
      fsv - a FileSystemView
    • JFileChooser

      public JFileChooser(File currentDirectory, FileSystemView fsv)
      Constructs a JFileChooser using the given current directory and FileSystemView.
      Parameters:
      currentDirectory - a File object specifying the path to a file or directory
      fsv - a FileSystemView
    • JFileChooser

      public JFileChooser(String currentDirectoryPath, FileSystemView fsv)
      Constructs a JFileChooser using the given current directory path and FileSystemView.
      Parameters:
      currentDirectoryPath - a String giving the path to a file or directory
      fsv - a FileSystemView
  • Method Details

    • setUI

      protected void setUI(ComponentUI newUI)
      Sets the L&F object that renders this component.

      This method is overridden to work around security restrictions in environments where reflection permissions are limited. The UI setting operation is wrapped in a AccessController.doPrivileged(PrivilegedAction) block to allow FlatLaf's styling operations to complete successfully.

      This is necessary because FlatLaf attempts to use reflection to access private fields during component styling, which would otherwise fail with an AccessControlException in restricted environments.

      Overrides:
      setUI in class JComponent
      Parameters:
      newUI - the FileChooserUI L&F object
      See Also: