Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable rename of a file in JFileChooser?

When you click twice (not double click) on a file in JFileChooser, you can rename the selected file. How to disable this feature? I've tried with

UIManager.put("FileChooser.readOnly", Boolean.TRUE);

but it doesn't work.

like image 289
kofucii Avatar asked Nov 18 '11 20:11

kofucii


2 Answers

Surprisingly, you cannot disable renaming files/creating new directories from JFileChooser itself. As you correctly surmised, you need to disable this FileChooser "feature" from UIManager instead.

Here's a snippet that might help:

http://www.coderanch.com/t/555535/GUI/java/FileChooser-readOnly

  Boolean old = UIManager.getBoolean("FileChooser.readOnly");  
  UIManager.put("FileChooser.readOnly", Boolean.TRUE);  
  JFileChooser fc = new JFileChooser(".");  
  UIManager.put("FileChooser.readOnly", old);  

The key thing is to set "FileChooser.readOnly" BEFORE you create the file chooser.

like image 70
paulsm4 Avatar answered Sep 30 '22 16:09

paulsm4


Customizing a JFileChooser Look and Feel has some rename constants

Your static should go into the JFileChooser using class.

Alternatively do addMouseListener to throw click away.

like image 20
Joop Eggen Avatar answered Sep 30 '22 14:09

Joop Eggen