Bonjour,
Je stock dans un répertoire, plusieurs dossiers qui contienne des fichier .json, le répertoire est créer dans mon src de mon projet.
J'ai créer une Class qui m'affiche une JCombox qui me permet de sélectionner un de c'est dossier et une autre Class me permet de choisir les fichiers .Json dans le dossier sélectionner.
J'ai créer une variable String qui récupéré le nom du dossier sélectionne. celle ci m'affiche bien le nom du dossier mais quand je veux utilise ce nom de dossier pour mon chemin d’accès, elle reste nul....
uen partie du code dans mon Main
Mon GameLoader
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
26
27
28
29
30
31
32
33 static String GameSelected; static final String CONFIG_DIR2 = "src/GameSelection/"; private static final String DEFAULT_CONFIG2 = CONFIG_DIR2; try { setupGameLoader(); setupGameLoading(); System.out.println("dosssier jeux:" + CONFIG_DIR); // ici ca m'affiche null } catch (NativeHookException ex) { System.exit(1); } private static void setupGameLoader() { try { File file = new File(DEFAULT_CONFIG2); if (!file.exists()) { FileWriter fileWriter = new FileWriter(file); gson.toJson(new Games(), fileWriter); fileWriter.close(); } GameLoader.getInstance3().readConfigs1(); } catch (IOException e) { e.printStackTrace(); } } private static void setupGameLoading() { Games gameschoice = GameLoader.getInstance3().getCurrentParametre(); GameSelected = gameschoice.JEUXENCOURS; }Mon code de selection de jeux
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 class GameLoader { private static ArrayList<Games> configss = new ArrayList<>(); private static GameLoader ourInstance3 = new GameLoader(); private GameLoader() { } static GameLoader getInstance3() { return ourInstance3; } void readConfigs1() { configss.clear(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); File dir = new File(Main.CONFIG_DIR2); File[] files = dir.listFiles(); if (files != null) { for (File file : files) { try { BufferedReader br1 = new BufferedReader(new FileReader(file)); Games config = gson.fromJson(br1,Games.class); config.name = file.getName().replace(".json", ""); configss.add(config); System.out.println("Loaded jeux:" + gson.toJson(config)); br1.close(); } catch (Exception e) { e.printStackTrace(); } } } } Games getCurrentParametre() { if (configss.size() <= 0) { return new Games(); } else { int currentConfig1 = 0; return configss.get(currentConfig1); } }et pour finir la Class Games qui stock ma variable par default :
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
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
55
56
57
58
59
60
61
62
63
64
65
66 class Gameselection extends JFrame { static String path = new File("Games/"+Main.GameSelected+"/").getAbsolutePath(); Gameselection() { JFrame f = new JFrame();//Remplacer JFrame par ta nouvelle fenetre f.setLocationRelativeTo(null); f.setSize(300, 150); f.setLocationRelativeTo(null); f.setAlwaysOnTop(true); f.setResizable(false); f.setVisible(true); f.setDefaultCloseOperation(f.DISPOSE_ON_CLOSE); JPanel pan = new JPanel(); pan.setBackground(Color.lightGray); pan.setLayout(null); f.setContentPane(pan); JComboBox jComboBox1 = new JComboBox(); JLabel label1 = new JLabel("Game Selection"); pan.add(jComboBox1); pan.add(label1); jComboBox1.setBounds(10, 35, 265, 30); label1.setBounds(10, 10, 101, 10); String[] directory = new File("src/Games").list(); for (int i = 0; i < directory.length; i++) { String aDirectory = directory[i]; jComboBox1.addItem(aDirectory); jComboBox1.setSelectedItem(Main.GameSelected); JSONObject obj = new JSONObject(); jComboBox1.addItemListener(e -> { if (e.getStateChange() == ItemEvent.SELECTED) { obj.put("GameSelected", Main.GameSelected = (String) jComboBox1.getSelectedItem()); System.out.println(path); ici aussi j'ai "null" try { FileWriter out = new FileWriter("src/GameSelection/GameSelection.json"); obj.writeJSONString(out); out.close(); } catch (IOException H) { H.printStackTrace(); } }); } } }Ensuite une fois le dossier sélectionné je charge les .json .
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 public class Games { String name = "Jeux"; String JEUXENCOURS = "Mon Jeux1/"; }
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 static final String CONFIG_DIR = "src/Games/"+ GameSelected; //ICI Mon game selected reste a null!!!!! private static final String DEFAULT_CONFIG = CONFIG_DIR; ///////////////////////////////////////// private static void setupConfigs() { try { File file = new File(CONFIG_DIR); if (!file.exists()) { FileWriter fileWriter = new FileWriter(file); gson.toJson(new Config(), fileWriter); fileWriter.close(); } ConfigManager1.getInstance1().readConfigs(); ConfigManager2.getInstance2().readConfigs(); } catch (IOException e) { e.printStackTrace(); } }
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
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 class ConfigManager1 { private static ArrayList<Config> configs = new ArrayList<>(); private static ConfigManager1 ourInstance1 = new ConfigManager1(); private int currentConfig = 0; private ConfigManager1() { } static ConfigManager1 getInstance1() { return ourInstance1; } void readConfigs() { configs.clear(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); File dir = new File(Main.CONFIG_DIR); File[] files = dir.listFiles(); if (files != null) { for (File file : files) { try { BufferedReader br = new BufferedReader(new FileReader(file)); Config config = gson.fromJson(br, Config.class); config.name = file.getName().replace(".json", ""); configs.add(config); System.out.println("Loaded Config-1:\n" + gson.toJson(config)); br.close(); } catch (Exception e) { e.printStackTrace(); } } } } Config getCurrentConfig() { if (configs.size() <= 0) { return new Config(); } else { return configs.get(currentConfig); } } void cycleConfigs() { if (configs != null && configs.size() > 0) { if (currentConfig == configs.size() - 1) { currentConfig = 0; } else { currentConfig++; } } }et ma class Config
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
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
55 class ConfigManager2 { private static ArrayList<Config> configs = new ArrayList<>(); private static ConfigManager2 ourInstance2 = new ConfigManager2(); private int currentConfig = 0; private ConfigManager2() { } static ConfigManager2 getInstance2() { return ourInstance2; } void readConfigs() { configs.clear(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); File dir = new File(Main.CONFIG_DIR); File[] files = dir.listFiles(); if (files != null) { for (File file : files) { try { BufferedReader br = new BufferedReader(new FileReader(file)); Config config = gson.fromJson(br, Config.class); config.name = file.getName().replace(".json", ""); configs.add(config); System.out.println("Loaded Config-2:\n" + gson.toJson(config)); br.close(); } catch (Exception e) { e.printStackTrace(); } } } } Config getCurrentConfig() { if (configs.size() <= 0) { return new Config(); } else { return configs.get(currentConfig); } } void cycleConfigs() { if (configs != null && configs.size() > 0) { if (currentConfig == configs.size() - 1) { currentConfig = 0; } else { currentConfig++; } } } }Mon application est fonctionnel depuis des Mois, c'est a dire , le chargement des fichiers et le reste. Avant je n'avais pas de sélection de jeux, un dossier "Config" etais a la racine de mon projet et pas de soucis.
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 class Config { // Ne jamais modifie cette class String name = "Default"; float rateoffire = 0; int bullets = 0; int weaponssetbackTablevertical[] = new int[bullets]; int weaponssetbackTablehorizontal[] = new int[bullets]; double Scopex1 = 0.0; double Scopex2 = 0.0; double Scopex3 = 0.0; double Scopex4 = 0.0; double Standing = 0.0; double Crouch = 0.0; double Prone = 0.0;
Je chargeais mais avec un chemin direct : [CODE
] static final String CONFIG_DIR = "Config";
private static final String DEFAULT_CONFIG = CONFIG_DIR;[/CODE]
J'ai décidé d'ajoute une sélection de jeux c'est jour ci.
J'ai situé le probleme au niveau de la variableet de
Code : Sélectionner tout - Visualiser dans une fenêtre à part static String GameSelected;
Si je supprime le CONFIG_DIR, ma variable GameSelected me renvoie bien le nom du dossier sélectionné.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 static final String CONFIG_DIR = "src/Games/"+ GameSelected; //ICI Mon game selected reste a null!!!!! private static final String DEFAULT_CONFIG = CONFIG_DIR;
Je ne vois pas pourquoi ni comment régler le problème.
Merci pour l'aide apporté
Cordialement
Fred
Partager