How to change the appearance and behavior of the "Open" and "Cancel" Jfilechooser buttons?

Asked

Viewed 770 times

3

Good people, 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;
}

o que é mostrado a partir desde código é.

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

  • 2

    I don’t know if it’s gonna work the way you want it to, but you’ve tried it setMultiSelectionEnabled(true)? This will allow the selection of more than one file (I just don’t know if it is possible to select them in different folders). Customizing button text is easy, there is the setApproveButtonText and think which should have similar option to cancel button. If this is not enough, I suggest in your own code a loop that selects a path, opens the dialog again, etc, until the user chooses everything.

1 answer

1


Good people I managed to get where I wanted, or better already gave to begin...

The code was as shown below, what I am doing is captaining all kinds of selection and what I did with the button open up was to change the text on it no more, but when I got 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;

        }

    }

}

The Jfilechooser looked like this:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.