简体   繁体   中英

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

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); 在此输入图像描述

JFileChooser is not designed to have its components hidden. There is no facility in the API to do this. Because the components are private you don't have access to them and can't write code to hide them yourself.

You probably shouldn't be doing this. You can disable the control by setting the "All Files" filter and no others, in which case the component becomes irrelevant.

Technically you may be able to do this by using Reflection and violating the class protections, but unless its absolutely critical to you app, don't do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM