Cloud with rain
.:G
G:.
0 and 1 serie, black on white
pulled card
myjsp.feelinglinux.com
ver. 1.1.9-4
Hallo, welcome to my world.
Here you can find some stuff about computer science.
<<< Enjoy your visit! >>>
0 and 1 serie, white on black

Java - Swing: implementazione Directory chooser

        Scritto: Giansante Gabriele, 08/12/2003     

JDk 1.4/1.5
Oltre ai file puo' essere necessario poter selezionare solo directory.
Ecco una customizazione della classe javax.swing.JFileChooser, con un filtro creato appositamente per le directory.
Si tratta di due classi inserite nel progetto FAS.

Primo passo, creazione del filtro:
/*
 * Feeling Application System: "a quick way to develop a simple Java GUI"
 * Copyright (C) 2002-2003  Gabriele Giansante
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * --
 *
 * Giansante Gabriele
 *
 * WEB:      (IT) "http://www.feelinglinux.com"
 *
 */

package com.feelinglinux.application.swing;

import javax.swing.filechooser.FileFilter;
import java.io.File;

/**
 * <p><b>DirectoryFileFilter</b></p>
 * <p>Filtro per directory</p>
 * <p>Copyright (c) 2002</p>
 * <p>http://www.feelinglinux.com</p>
 * @author Gabriele Giansante
 * @version 1.0
 */
public class DirectoryFileFilter extends FileFilter {

    private String desc = "Directory";

    public DirectoryFileFilter(String desc){
        if (desc != null) this.desc = desc;
    }

    public boolean accept(File f){
        boolean ris = false;
        if (f!=null){
            String name = f.getName();
            //Accetto solo directory
            if (f.isDirectory()) ris = true;
        }
        return ris;
    }

    public String getDescription(){
        return desc;
    }
}
Secondo passo, estensione del JFileChooser:
/*
 * Feeling Application System: "a quick way to develop a simple Java GUI"
 * Copyright (C) 2002-2003  Gabriele Giansante
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * --
 *
 * Giansante Gabriele
 *
 * WEB:      (IT) "http://www.feelinglinux.com"
 *
 */

package com.feelinglinux.application.swing;

import javax.swing.JFileChooser;
import com.feelinglinux.application.swing.DirectoryFileFilter;

/**
 * <p><b>DirectoryChooser</b></p>
 * <p>JFileChooser specializzato per la selezione di directory</p>
 * <p>Copyright (c) 2002</p>
 * <p>http://www.feelinglinux.com</p>
 * @author Gabriele Giansante
 * @version 1.0
 */
public class DirectoryChooser extends JFileChooser{
    /**
     * Inizializza il JFileChooser per la selezione di sole directory.
     * @param desc e' il testo da associare al filtro delle directory.
     */
    public DirectoryChooser(String desc){
        this.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        this.addChoosableFileFilter(new DirectoryFileFilter(desc));
        this.removeChoosableFileFilter(this.getAcceptAllFileFilter());
        this.setDialogTitle(desc);
    }
}

Hai trovato utile questo articolo?
Aiutami a condividerlo o metti un "mi piace".
Grazie mille!


Gli strumenti di condivisione (Google+, Facebook) sono visibili in alto a destra solo dopo aver accettato la policy di utilizzo dei cookie per questo sito.
FAQ - Come faccio a cambiare la mia scelta?

 

Strumenti (myjsp.feelinglinux.com)
Gioco: allenamento con la tastiera Strumenti di codifica/decodifica URI (%-encoding) e Base64 Strumenti di calcolo online per IP e Reti
QUIZ GAME
Quiz game

Cerca @myjsp.feelinglinux.com

Pubblicita'