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
| public void initComponent() {
//...........
for (JMenuItem b : liste) {
jMenu3.add(b);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
open();
}
});
// ...
}
//..........
}
private void open() {
JFileChooser chooser = new JFileChooser("./");
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String nomFichier = chooser.getSelectedFile().getPath();
//... ton code
}
}
// si tu veux rajouter un filtre tu peux rajouter ça après JFileChooser chooser = new JFileChooser();
// Il faudra définir quel type de fichier tu voudra (dans cette exemple c'est fichiers .txt uniquement)
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".txt") || f.isDirectory();
}
public String getDescription() {
return "Fichier du calculateur (.txt)";
}
}); |