SelectDirectoryListener.java

package edu.jiangxin.apktoolbox.swing.extend.listener;

import edu.jiangxin.apktoolbox.utils.FileUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class SelectDirectoryListener implements ActionListener {
    private final String dialogTitle;
    private final JTextField pathTextField;

    public SelectDirectoryListener(String dialogTitle, JTextField pathTextField) {
        super();
        this.dialogTitle = dialogTitle;
        this.pathTextField = pathTextField;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jfc.setDialogTitle(dialogTitle);
        int ret = jfc.showDialog(new JLabel(), null);
        if (ret == JFileChooser.APPROVE_OPTION) {
            File file = jfc.getSelectedFile();
            String path = FileUtils.getCanonicalPathQuiet(file);
            pathTextField.setText(path);
        }
    }
}