Created
October 21, 2013 06:17
-
-
Save bjpeterdelacruz/7079402 to your computer and use it in GitHub Desktop.
A custom file chooser that will change the state of the Open button when the user clicks on a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyFileChooser extends JFileChooser { | |
public MyFileChooser() { | |
setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); | |
addPropertyChangeListener(new MyListener()); | |
} | |
private class MyListener implements PropertyChangeListener { | |
public void propertyChange(PropertyChangeEvent event) { | |
String property = FileChooser.SELECTED_FILE_CHANGED_PROPERTY; | |
if (!property.equals(event.getPropertyName())) { | |
return; | |
} | |
if (!(event.getNewValue() instanceof File)) { | |
return; | |
} | |
File file = (File) event.getNewValue(); | |
boolean flag = true; // insert your logic here | |
setOpenButtonState(MyFileChooser.this, flag); | |
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 (b.getText().equals("Open") ) { | |
b.setEnabled(flag); | |
} | |
} | |
else if (comp instanceof Container) { | |
setOpenButtonState((Container) comp, flag); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment