Bonjour j'ai un code qui me premet de récupérer le chemin d'un fichier avec un JFileChooser
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; public class FileChooser { public static String getfilePath() { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File(".")); chooser.setDialogTitle("Selection du fichier"); chooser.setAcceptAllFileFilterUsed(false); FileNameExtensionFilter filter = new FileNameExtensionFilter( "Fichiers xlsx.", "xlsx"); chooser.addChoosableFileFilter(filter); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory()); return String.valueOf(chooser.getSelectedFile()); } else { return ("No Selection"); } } }
Et dans l'autre classe je récupère le chemin comme ça :
Mais maintenant j'aimerais faire exactement la même chose avec un comboBox pour qu'après avoir récupéré le chemin du fichier je récupère une valeur dans un comboBox
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 final static String CHEMIN_FICHIER = FileChooser.getfilePath();
quelque chose comme ça :
Avec ça :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 if (CHEMIN_FICHIER.contains(".xlsx")) { Lectures test = new Lectures(CHEMIN_FICHIER); List<Cours> listeCP = new ArrayList<>(); // retourne la liste des cours pour l'enseignant choisis dans le comboBox listeCP = test.getListeCoursProf(ComboBox.getProf());
malheureusement ça ne marche pas puisque contrairement au JFileChooser la comboBox ne s'affiche pas à l’exécution du programme
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 public class ComboBox { public static String getProf() { JComboBox cb = new JComboBox(); List l1 = Lectures.getListeProf(); for (int i = 0; i < l1.size(); i++) { cb.addItem(l1.get(i)); } String nomProf = (String)cb.getSelectedItem(); return nomProf; } }
Partager