How can I change the appearance and behavior of the "Open" and "Cancel" buttons in JFileChooser?

3

Good folks, I'm trying to use JFileChooser to select directories and also files. What I'm using so far is

public void buscaFile() {
    File[] diretorio = null;
    diretorio = selectDir();
    if(diretorio != null) {
        for(File i : diretorio) {
            String[] add = { i.toString(), "aguarde"};
            System.out.println("DIR "  + i.toString());
          }
    }
    return;
}   
private static File[] selecionaDir() {
    File caminho = new File("C:\");
    JFileChooser arq = new JFileChooser(caminho);
    arq.setMultiSelectionEnabled(true);
    arq.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int showOpenDialog = arq.showOpenDialog(null);
    if (showOpenDialog != arq.APPROVE_OPTION) {
        return null;
    }
    File[] uploadDir = fileChooser.getSelectedFiles();
    return uploadDir;
}

But the idea is that instead of the Open and Cancel buttons is Add and OK. So that each time the person clicks add the selected path is saved, and when you finish selecting everything you need just click OK.

    
asked by anonymous 18.09.2015 / 01:28

1 answer

1

Good people I got to get where I wanted, or better already started ...

The code is as shown below, what I'm doing is capturing all sorts of selection and what I did with the open button was to change the text in it no more, but when I get the address of the selected directories I call the method again.

The code:

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author Cizo
 */
public class MyFileChooser extends JFrame {

    public void seleciona() {

        File diretorio = new File("C:\");

        JFileChooser fileChooser = new JFileChooser(diretorio);
        fileChooser.resetChoosableFileFilters();
        UIManager.put("FileChooser.openDialogTitleText", "Seleçao de aquivos");
        UIManager.put("FileChooser.lookInLabelText", "Local");
        UIManager.put("FileChooser.openButtonText", "Inserir");
        UIManager.put("FileChooser.cancelButtonText", "Sair");
        UIManager.put("FileChooser.fileNameLabelText", "Nome do Arquivo");
        UIManager.put("FileChooser.filesOfTypeLabelText", "Tipo de Arquivo");
        UIManager.put("FileChooser.openButtonToolTipText", "Abrir Selecionado");
        UIManager.put("FileChooser.cancelButtonToolTipText", "Sair");
        UIManager.put("FileChooser.fileNameHeaderText", "Nome do Arquivo");
        UIManager.put("FileChooser.upFolderToolTipText", "Subir Nivel Acima");
        UIManager.put("FileChooser.homeFolderToolTipText", "Desktop");
        UIManager.put("FileChooser.newFolderToolTipText", "Nova Pasta");
        UIManager.put("FileChooser.listViewButtonToolTipText", "Lista");
        UIManager.put("FileChooser.newFolderButtonText", "Criar Nova Pasta");
        UIManager.put("FileChooser.renameFileButtonText", "Renomear");
        UIManager.put("FileChooser.deleteFileButtonText", "Apagar");
        UIManager.put("FileChooser.filterLabelText", "Tipo de Arquivos");
        UIManager.put("FileChooser.detailsViewButtonToolTipText", "Detalhes");
        UIManager.put("FileChooser.fileSizeHeaderText", "Tamanho");
        UIManager.put("FileChooser.fileDateHeaderText", "Data de Modificação");

        SwingUtilities.updateComponentTreeUI(fileChooser);

        fileChooser.setMultiSelectionEnabled(true);
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        File[] dir = null;
        switch (fileChooser.showOpenDialog(this)) {

            case JFileChooser.APPROVE_OPTION:

                dir = fileChooser.getSelectedFiles();

                for (File dir1 : dir) {
                    System.out.println("AQUI > " + dir1);
                }
                seleciona();
                break;

            case JFileChooser.CANCEL_OPTION:
                JOptionPane.showMessageDialog(null, "Final");
                break;

        }

    }

}

JFileChooser looks like this:

    
20.09.2015 / 07:46