简体   繁体   中英

Java: In JFileChooser, how to stop showing absolute file path when double-clicking on file/folder?

在此处输入图片说明

The red box is showing the issue where the full file path appears when the user double clicks on a folder directory.

There are times when the text field contains full file path that is too long (as in, the folder opened is nested deeply among folders), thus blocking out the file name. Other times, when the user is saving a file, it is possible to save a file with the file name as long as what is in the text field, in this case the file path as the file name.

How to get rid of this behavior?

Here's how to change the behavior:

First, you need this method to scour all Components in JFileChooser:

@SuppressWarnings("unchecked")
private JList<Class<?>> findFileList(Component comp){
    if (comp instanceof JList){
        return (JList<Class<?>>) comp;
    }
    if (comp instanceof Container){
        for (Component c : ((Container) comp).getComponents()){
            JList<Class<?>> list = findFileList(c);
            if (list != null){
                return list;
            }
        }
    }
    return null;
}

Next, we need to find all MouseListeners available from the JList of all components, and remove all of them. It is possible that some MouseListeners may register double clicks and will affect how the text field displays its text. This will help to remove all of those affecting MouseListeners.

But removing MouseListeners will also affect how the JFileChooser is to handle opening files. We need to handle this action ourselves. Here's a simple way to do this.

final JFileChooser opener = new JFileChooser();
JList<Class<?>> list = findFileList(opener);
LOOP_TEMP1: for (MouseListener l : list.getMouseListeners()){
    if (l.getClass().getName().indexOf("FilePane") >= 0){
        list.removeMouseListener(l);
        list.addMouseListener(new MouseListener(){
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1){
                    File file = opener.getSelectedFile();
                    if (file != null){
                        BasicFileChooserUI ui = (BasicFileChooserUI) opener.getUI();
                        ui.setFileName(file.getName());
                    }
                }
                else if (e.getClickCount() == 2){
                    File file = opener.getSelectedFile();
                    if (file != null){
                        if (file.isDirectory()){
                            opener.setCurrentDirectory(file);
                        }
                        else if (file.isFile()){
                            opener.setSelectedFile(file);
                        }
                        BasicFileChooserUI ui = (BasicFileChooserUI) opener.getUI();
                        ui.setFileName(file.getName()); 
                    }
                }
            }
            @Override
            public void mouseEntered(MouseEvent e) {
            }
            @Override
            public void mouseExited(MouseEvent e) {
            }
            @Override
            public void mousePressed(MouseEvent e) {
            }
            @Override
            public void mouseReleased(MouseEvent e) {
            }
        });
        break LOOP_TEMP1;
    }
}

Now, whenever the user double clicks on a folder/directory, JFileChooser will no longer display long file paths in the text field. And it now looks better. You may have to handle opening files after double clicking on your own.

Hope you find this somewhat helpful!

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