JDK-4525475 : Cannot prevent file-system modification in JFileChooser
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.2,1.3.1,1.4.0_01,1.4.2
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS:
    generic,linux,solaris_8,solaris_10,windows_2000 generic,linux,solaris_8,solaris_10,windows_2000
  • CPU: generic,x86,sparc
  • Submitted: 2001-11-09
  • Updated: 2022-03-24
  • Resolved: 2003-08-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 tigerFixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Description
JFileChooser allows modification to the file-system by way of the "New Folder"
button, and the ability to rename file/folder names.  There is no method on
JFileChooser which indicates that this type of modification should not be
allowed within the given JFileChooser.

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b15
24-07-2004

EVALUATION See workaround for a possible way of removing the "New Folder" button. We will consider adding an API for this in a future release. ###@###.### 2001-11-09 Adding check for new UI default property "FileChooser.readOnly". ###@###.### 2003-07-19 The readOnly property is broken in 1.5 beta1 but will be fixed in beta2. See bug 5003410. At that point it will be possible to control the behavior on a per-instance basis, although it is not straightforward. See workaround section for an example. ###@###.### 2004-02-25
25-02-2004

WORK AROUND This is a rather ugly workaround. Please use with caution. import java.awt.*; import javax.swing.*; import javax.swing.plaf.ComponentUI; public class MyFileChooser extends JFileChooser { protected void setUI(ComponentUI newUI) { super.setUI(newUI); removeNewFolderButton(this); } private void removeNewFolderButton(Container c) { int n = c.getComponentCount(); for (int i = 0; i < n; i++) { Component comp = c.getComponent(i); if (comp instanceof JButton) { JButton b = (JButton)comp; Action action = b.getAction(); if (action != null) { String name = (String)action.getValue(Action.NAME); if (name != null && name.equals("New Folder")) { c.remove(b); return; } } } else if (comp instanceof Container) { removeNewFolderButton((Container)comp); } } } public static void main(String[] args) { new MyFileChooser().showOpenDialog(null); System.exit(0); } } ###@###.### 2001-11-09 ======================================== Here is an example of how to control the UI defaults property "FileChooser.readOnly" on a per-instance basis. This will only work in 1.5.0 beta2 and later versions. Note that the property must be set in two places: before creating the instance and in the setUI() method. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.ComponentUI; public class ROFileChooser extends JFileChooser { private Boolean readOnly; public ROFileChooser(boolean readOnly) { this.readOnly = Boolean.valueOf(readOnly); } protected void setUI(ComponentUI newUI) { if (readOnly != null) { UIManager.put("FileChooser.readOnly", readOnly); } super.setUI(newUI); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { final JFrame frame = new JFrame(); final JButton normalButton = new JButton("Normal"); final JButton readOnlyButton = new JButton("Read Only"); ActionListener action = new ActionListener() { public void actionPerformed(ActionEvent ev) { boolean readOnly = (ev.getSource() == readOnlyButton); UIManager.put("FileChooser.readOnly", Boolean.valueOf(readOnly)); JFileChooser fc = new ROFileChooser(readOnly); fc.showOpenDialog(frame); } }; normalButton.addActionListener(action); readOnlyButton.addActionListener(action); frame.getContentPane().add(normalButton, BorderLayout.WEST); frame.getContentPane().add(readOnlyButton, BorderLayout.EAST); frame.pack(); frame.setLocation(200, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } ###@###.### 2004-02-25
25-02-2004