Java - Remove component (Files of Type) from JFileChooser

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);
}

Thank you very much all

Answers


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.

// in wrapper:
modifyJChooser(fileChooser.getComponents(), Color.white);

// in component:
private void modifyJChooser(Component[] jc, Color bg) {

    for (int i = 0; i < jc.length; i++) {
        Component c = jc[i];

        // hide file name selection
        if (c.getClass().getSimpleName().equals("MetalFileChooserUI$3")) {
            c.getParent().setVisible(false);
        }

        if (c instanceof JComboBox) {
            Object sel = ((JComboBox) c).getSelectedItem();
            if (sel.toString().contains("AcceptAllFileFilter")) {
                c.setVisible(false);
            }
        } else if (c instanceof JLabel) {
  // **** This is the part that the original poster is looking for ****
            String text = ((JLabel) c).getText();
            if (text.equals("Files of Type:") || text.equals("File Name:") || text.equals("Folder Name:")) {
                c.getParent().getParent().remove(c.getParent());
            }
        } else if (c instanceof JButton) {
            JButton j = (JButton) c;
            String txt = j.getText();
            if (txt != null) {
                if (JCHOOSER_NEW_FOLDER.equalsIgnoreCase(txt)) {
                    j.getParent().setVisible(false); // Disable New Folder on Mac OS
                } else if (JCHOOSER_BTN_CANCEL.equalsIgnoreCase(txt)) {
                    Component parent = c.getParent();
                    ((Container) parent).remove(c);
                }
            }
        }

        if (c instanceof Container)
            modifyJChooser(((Container) c).getComponents(), bg);

        c.setBackground(bg);
    }

}

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);


Need Your Help

Can we Enable Use Google Location Services on 4.0 programatically?

android

I wants to Enable Use Google Location Services on my 4.0 Device From within my Application.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.