RFR: 8139228: JFileChooser renders file names as HTML document
Tejesh R
tr at openjdk.org
Thu May 8 04:10:52 UTC 2025
On Wed, 7 May 2025 21:08:53 GMT, Phil Race <prr at openjdk.org> wrote:
>> Because the problem is still there, and there are virtual folders on Windows. If a custom `FileSystemView` can return file names with `"<html>"` in their name, `JFileChooser` in Windows Look and Feel will render the file name as HTML.
>>
>> Isn't it the problem that you're trying to fix?
>
> @aivanov-jdk @TejeshR13
> This seems stalled because there are conflicting statements
> - Alexei : you can create a custom FIleSystemView on windows with <html> in path names.
> - Tejesh : no you can't, or at least it doesn't work when you try to use it.
>
> Can we please get that reconciled ?
> Tejesh pls show the code you used, and Alexei please show how you would do it.
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 Arrays.asList(roots).contains(file);
}
@Override
public Boolean isTraversable(File f) {
return true;
}
}
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/24439#discussion_r2078849056
More information about the client-libs-dev
mailing list