Java – disable the open button in Jfilechooser?
I extended a Jfilechooser and overridden the approveselection method so that when the user selects an invalid directory and clicks the open button, an error message in joptionpane will be displayed But I want to make my Jfilechooser more user-friendly, and when the user clicks the invalid directory and then re enables it when the user clicks the valid directory, the button will open Can I further customize my Jfilechooser and access the open button so that I can change the status of the button accordingly (possibly the listener selected by listening directory)?
public class MyFileChooser extends JFileChooser { private final JFrame mainFrame; public MyFileChooser(JFrame mainFrame) { this.mainFrame = mainFrame; setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); } @Override public void approveSelection() { if (/* directory is valid */) { super.approveSelection(); return; } JOptionPane.showMessageDialog(mainFrame,"Invalid directory","Error",JOptionPane.ERROR_MESSAGE); } }
Solution
You can call chooser. When any selection change on the file / directory is detected Setcontrolbuttonsareshown (false) to hide the accept / Cancel button:
myChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); myChooser.addPropertychangelistener(new Propertychangelistener() { public void propertyChange(PropertyChangeEvent evt) { if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())) { File file = (File) evt.getNewValue(); if (file != null && file.isFile()) { // your condition myChooser.setControlButtonsAreShown(false); } else if ( file != null ) { System.out.println(file.getName()); myChooser.setControlButtonsAreShown(true); } } myChooser.repaint(); } });
However, it may confuse users, better make a custom filefilter and display only the files / directories you need:
public static class MyDirectoryFilter extends javax.swing.filechooser.FileFilter { @Override public boolean accept( File file ) { return file.isDirectory() && customeCondition(file) ; } @Override public String getDescription() { return "this only my custom dir"; } } myChooser.setFileFilter( new MyDirectoryFilter () );
Edit: I found a way to disable the button by iterating over the component and getting the handle to open the button: http://www.coderanch.com/t/468663/GUI/java/Disabling-Enabling-level-button-folder
Example:
myChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); myChooser.addPropertychangelistener(new Propertychangelistener() { public void propertyChange(PropertyChangeEvent evt) { if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())) { File file = (File) evt.getNewValue(); if (file != null && file.isFile()) { setOpenButtonState(myChooser,false); } else if ( file != null ) { setOpenButtonState(myChooser,true); System.out.println(file.getName()); } } myChooser.repaint(); } }); public static void setOpenButtonState(Container c,boolean flag) { int len = c.getComponentCount(); for (int i = 0; i < len; i++) { Component comp = c.getComponent(i); if (comp instanceof JButton) { JButton b = (JButton) comp; if ( "Open".equals(b.getText()) ) { b.setEnabled(flag); } } else if (comp instanceof Container) { setOpenButtonState((Container) comp,flag); } } }