1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public class Ouvrir extends JFrame implements ActionListener{
JButton open = new JButton("Selectionner un fichier"); //nouveau bouton open
public Ouvrir()
{
super("Explorateur de fichier"); //titre
setSize(450,100); //taille
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//fermeture
open.addActionListener(this);//ajout d'un actionlistener
JPanel pane = new JPanel();
BorderLayout bord = new BorderLayout();
pane.setLayout(bord);
pane.add("Center", open);
setContentPane(pane);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent evt)
{
JFileChooser chooser = new JFileChooser();//création dun nouveau filechosser
chooser.setApproveButtonText("Choix du fichier..."); //intitulé du bouton
chooser.showOpenDialog(null); //affiche la boite de dialogue
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
jTextArea1.append("Vous avez choisis : "+chooser.getSelectedFile().getAbsolutePath()+"\n"); //si un fichier est selectionné, récupérer le fichier puis sont path et l'afficher dans le champs de texte
String Firm = chooser.getSelectedFile().getAbsolutePath();
this.setVisible(false);
System.out.println(Firm);
try {
LF.LireFichier(Firm);
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
}
}
} |