RFR: 8139228: JFileChooser renders file names as HTML document
Sergey Bylokhov
serb at openjdk.org
Tue May 13 03:08:52 UTC 2025
On Tue, 13 May 2025 03:01:46 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:
>> I tried with this code snippet.
>>
>> import javax.swing.JFileChooser;
>> import javax.swing.SwingUtilities;
>> import javax.swing.UIManager;
>> import javax.swing.UnsupportedLookAndFeelException;
>> import javax.swing.filechooser.FileSystemView;
>> import java.io.File;
>> import java.util.Arrays;
>>
>> public class VirtualFileChooser {
>>
>> public static void main(String[] args) throws UnsupportedLookAndFeelException, ClassNotFoundException, InstantiationException, IllegalAccessException {
>> UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
>> //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
>>
>>
>> SwingUtilities.invokeLater(() -> {
>> JFileChooser chooser = new JFileChooser(new VirtualFileSystemView());
>>
>> int result = chooser.showOpenDialog(null);
>> if (result == JFileChooser.APPROVE_OPTION) {
>> File selectedFile = chooser.getSelectedFile();
>> System.out.println("Selected: " + selectedFile.getAbsolutePath());
>> }
>> });
>> }
>>
>> static class VirtualFileSystemView extends FileSystemView {
>>
>> private final File[] roots;
>>
>> public VirtualFileSystemView() {
>> // Create a dummy root folder
>> String name = "<html><h1 color=#ff00ff><font face="Comic Sans MS">SWING ROCKS!!!111";
>> roots = new File[]{new File(name)};
>> }
>>
>> @Override
>> public File createNewFolder(File containingDir) {
>>
>> File folder = new File(containingDir, "New Folder");
>> folder.mkdir();
>> return folder;
>> }
>>
>> @Override
>> public File[] getRoots() {
>> return roots;
>> }
>>
>> @Override
>> public File getHomeDirectory() {
>> return roots[0];
>> }
>>
>> @Override
>> public File getDefaultDirectory() {
>> return roots[0];
>> }
>>
>> @Override
>> public File[] getFiles(File dir, boolean useFileHiding) {
>> // Simulate a virtual folder structure
>> return new File[]{
>> new File(dir, "virtualFile1.txt"),
>> new File(dir, "virtualFile2.txt"),
>> new File(dir, "virtualFolder")
>> };
>> }
>>
>> @Override
>> public boolean isRoot(File file) {
>> return Arr...
>
>> Exception in thread "AWT-EventQueue-0" java.nio.file.InvalidPathException: Illegal char <<> at index 0: <html><h1 color=#ff00ff><font face="Comic Sans MS">SWING ROCKS!!!111
>> at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:191)
>> at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:142)
>> at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:46)
>> at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
>> at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:203)
>> at java.base/java.nio.file.Path.of(Path.java:148)
>> at java.base/java.nio.file.Paths.get(Paths.java:69)
>> at java.desktop/sun.awt.shell.ShellFolder.getShellFolder(ShellFolder.java:260)
>> at java.desktop/javax.swing.filechooser.FileSystemView.getShellFolder(FileSystemView.java:724)
>> at java.desktop/javax.swing.filechooser.FileSystemView.getSystemIcon(FileSystemView.java:242)
>> at java.desktop/com.sun.java.swing.plaf.windows.WindowsFileChooserUI$WindowsFileView.getIcon(WindowsFileChooserUI.java:1398)
>> at java.desktop/javax.swing.JFileChooser.getIcon(JFileChooser.java:1614)
>
>
> This exception looks like a bug? I think if the file was not found then null is an expected result.
> For now it can be bypassed by this:
>
>
> import java.io.File;
> import java.lang.Override;
>
> import javax.swing.Icon;
> import javax.swing.JFileChooser;
> import javax.swing.SwingUtilities;
> import javax.swing.UIManager;
> import javax.swing.UnsupportedLookAndFeelException;
> import javax.swing.filechooser.FileSystemView;
>
> public class VirtualFileChooser {
>
> public static void main(String[] args) throws UnsupportedLookAndFeelException, ClassNotFoundException, InstantiationException, IllegalAccessException {
> UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
> //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
>
>
> SwingUtilities.invokeLater(() -> {
> JFileChooser chooser = new JFileChooser(new VirtualFileSystemView());
>
> int result = chooser.showOpenDialog(null);
> if (result == JFileChooser.APPROVE_OPTION) {
> File selectedFile = chooser.getSelectedFile();
> System.out.println("Selected: " + selectedFile.getAbsolutePath());
> }
> });
> }
>
> static class VirtualFileSystemView extends FileSystemV...
Note that the bug occurs both in the list of roots (in the dropdown) and in the list of files.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/24439#discussion_r2085842106
More information about the client-libs-dev
mailing list