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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Ouvrir extends JFrame implements ActionListener{
private JButton open = new JButton("Selectionner un fichier"); //nouveau bouton open
private JTextField status = new JTextField("Pas de fichier chargé!"); //nouveau champs de texte
private JPanel container = new JPanel();
public Ouvrir() {
this.setTitle("Test d'ouverture d'un JFileChooser");
this.setSize(500, 100);
this.setLocationRelativeTo(null);
status.setEditable(false);
open.addActionListener(this);//ajout d'un actionlistener
container.setBackground(Color.CYAN);
JPanel pane = new JPanel();
pane.add(status);
pane.add(open);
container.add(pane);
this.setContentPane(container);
this.setVisible(true);
}
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)
{
status.setText(chooser.getSelectedFile().getAbsolutePath()); //si un fichier est selectionné, récupérer le fichier puis sont path et l'afficher dans le champs de texte
}
}
}
et voici le code qui appel cette class à partir de mon applet
if (evt.getActionCommand().equals("Fichier"))
{
Open = new Ouvrir();
Open.show();
} |
Partager