Choisir un fichier avec JFileChooser

Author:

 fichier, list, list, set, swing, awt, JPanel, JFrame
{filelink=168}

import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;


public class ExJFileChooser extends JPanel {



    public ExJFileChooser(JFrame f) {

        final JFrame frame = f;

        final JFileChooser chooser = new JFileChooser( );

        JButton btn = new JButton("Parcourir...");

        add(btn);

        btn.addActionListener(new ActionListener( ) {

            public void actionPerformed(ActionEvent e) {

            int returnVal = chooser.showOpenDialog(frame);

            if (returnVal == JFileChooser.APPROVE_OPTION) {

                JOptionPane.showMessageDialog(null,"Vous avez sélectionné: " + 

                    chooser.getSelectedFile( ).getPath( ));

            } else {

                JOptionPane.showMessageDialog(null,"Vous n'avez rien sélectionné.");

            }

            }

        });

    }



    public static void main(String[] args) {

        JFrame f = new JFrame("JFileChooser Demo");

        f.getContentPane( ).add(new ExJFileChooser(f));

        f.pack( );

        f.setVisible(true);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *