How can I remove component (Files of Type) from JFileChooser; both label and its combobox?
I have the following code:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle("Select Folder");
fileChooser.setApproveButtonText("Select Folder");
fileChooser.setAcceptAllFileFilterUsed(false);
hideComponents(fileChooser.getComponents());
private void hideComponents(Component[] components) {
for (int i= 0; i < components.length; i++) {
if (components[i] instanceof JPanel)
hideComponents(((JPanel)components[i]).getComponents());
else if (//component to remove)//what do I check for in here?
components[i].setVisible(false);
}
I respectfully disagree. There is a facility for it, and I use it successfully all the time, particularly with the JFileChooser and particularly to make the cursed beast work for both DOS and Mac. There are numerous examples on the web; here is another, culled from my working applet. (This snippet also sets the background color on all components).
In short: The original poster was on the right track – iterate over JFileChooser.getComponents(). They don’t make it easy to identify a component, so what I do is look for a text label and then get its desired ancestor. You can then remove that from the layout using Container.getLayout().remove(component), or, you can setVisible(false), or you can sometimes setPreferredSize(new Dimension(0,0)) to make it go away.
Caveat: This leaves a bit of a gap where the removed components once resided. I have not been able to identify its source; if anybody has a clue, please post.
Result is like this (note that I make other modifications not shown in code snippet);
