Bonjour, j'essaye d'ajouter et de supprimer des items dans un fichier csv en utilisant opencsv. J'ai crée l'ihm ci-dessous:

Nom : qsd.png
Affichages : 250
Taille : 4,6 Ko

En bas pour ajouter un item par ligne dans le fichier csv (sous la forme le0, "le1", "le2") et dans la combobox (seul le 0 sera affiché dans la combobox). Et en haut l'item sélectionné fait mettre à jour la valeur des deux textbox. Et on peut suppirmer un item en la choisissant dans la combox. Ca parait simple mais ça fait 2 jours que j'essaye de bien faire tout ça mais n'arrive pas résoudre les bugs. Là, la sélection fait bugguer et le delete aussi. Il supprime que certaine partie de la ligne a,"a","a". Peut être que vous verrez des erreurs ou me conseillerez d'autres manières de mieux le faire.

Code de l'ihm:
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.awt.BorderLayout;
import java.awt.EventQueue;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
 
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
 
 
public class Fenetre extends JFrame {
 
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
 
	/**
         * Launch the application.
         */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Fenetre frame = new Fenetre();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
 
	/**
         * Create the frame.
         */
	public Fenetre() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
 
 
		List<String> lesMag = Serveur.getMag();
		String[] stock = new String[lesMag.size()];
		String[] tour = lesMag.toArray(stock);
 
		final JComboBox comboBox = new JComboBox(tour);
		comboBox.setBounds(10, 33, 285, 20);
		contentPane.add(comboBox);
		comboBox.addItemListener(new ItemListener() {   
			@Override
			public void itemStateChanged(ItemEvent arg0) {			
				if (arg0.getStateChange() == ItemEvent.SELECTED) {
					textField.setText(Serveur.getInfoMag("ip", comboBox.getSelectedIndex()));
					textField_1.setText(Serveur.getInfoMag("port", comboBox.getSelectedIndex()));
				}
 
			}
	    });
 
 
 
 
		JButton btnSupprimer = new JButton("Delete");
		btnSupprimer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(comboBox.getItemCount() != 0){
					Serveur.SuppMag(comboBox.getSelectedIndex());
					comboBox.removeItem(comboBox.getSelectedItem());
				}
			}
		});
		btnSupprimer.setBounds(335, 32, 89, 50);
		contentPane.add(btnSupprimer);
 
		textField = new JTextField();
		textField.setBounds(56, 80, 86, 20);
		contentPane.add(textField);
		textField.setColumns(10);
 
		textField_1 = new JTextField();
		textField_1.setBounds(56, 111, 86, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
 
		JLabel label = new JLabel("1 :");
		label.setBounds(10, 83, 46, 14);
		contentPane.add(label);
 
		JLabel label_1 = new JLabel("2 :");
		label_1.setBounds(10, 114, 35, 14);
		contentPane.add(label_1);
 
		textField_2 = new JTextField();
		textField_2.setBounds(24, 219, 86, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
 
		textField_3 = new JTextField();
		textField_3.setBounds(141, 219, 86, 20);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
 
		textField_4 = new JTextField();
		textField_4.setBounds(250, 219, 86, 20);
		contentPane.add(textField_4);
		textField_4.setColumns(10);
 
		JLabel label_2 = new JLabel("0 :");
		label_2.setBounds(24, 194, 46, 14);
		contentPane.add(label_2);
 
		JLabel label_3 = new JLabel("1 :");
		label_3.setBounds(141, 194, 46, 14);
		contentPane.add(label_3);
 
		JLabel label_4 = new JLabel("2 :");
		label_4.setBounds(249, 194, 46, 14);
		contentPane.add(label_4);
 
		JButton btnNewButton = new JButton("Add");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
 
				String a = textField_2.getText();
				String b = textField_3.getText();
				String c = textField_4.getText();
				String d = a+","+b+","+c;
 
 
				if(!a.isEmpty() && !b.isEmpty() && !c.isEmpty() ){
					Serveur.AjouterMag(d);
					comboBox.addItem(a);
				}
 
			}
		});
		btnNewButton.setBounds(345, 157, 79, 82);
		contentPane.add(btnNewButton);
	}
}

Code la la partie traitement:
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
 
import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
 
 
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
 
public class Serveur implements Runnable {
 
 
	@Override
	public void run(){
 
	}
 
 
 
   static void AjouterMag(String newMag){
 
		String chemin = System.getProperty("user.dir")+"/src/mag.csv";
		File fichier = new File(chemin);
		CSVWriter ecrire;
 
 
		try {
			//true permet de ne pas écraser mais d'ajouter à la suite
			ecrire = new CSVWriter(new FileWriter(fichier, true));
 
			String [] ajout = newMag.split(",");
			ecrire.writeNext(ajout);
			ecrire.close();
 
 
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}   
   }
 
 
 
	static List<String> getMag() {
 
		String chemin = System.getProperty("user.dir")+"/src/mag.csv";
		File fichier = new File(chemin);
 
 
		CSVReader reader = null;
 
		try {
 
			reader = new CSVReader(new FileReader(fichier));
 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		List<String> maListe = new ArrayList<String>();
 
 
	     String [] nextLine;
	     try {
 
			while ((nextLine = reader.readNext()) != null) {
			    // nextLine[] is an array of values from the line
			    maListe.add(nextLine[0]);
			 }
 
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
 
		return maListe;
 
    }
 
 
	static String getInfoMag(String s, int lindice) {
		System.out.println(lindice);
		String chemin = System.getProperty("user.dir")+"/src/mag.csv";
		File fichier = new File(chemin);
 
 
		CSVReader reader = null;
 
		try {
 
			reader = new CSVReader(new FileReader(fichier));
 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		List<String> maListe = new ArrayList<String>();
 
 
	     String [] nextLine;
	     try {
	    	if (s == "ip"){
	    		while ((nextLine = reader.readNext()) != null) {
				    // nextLine[] is an array of values from the line
				    maListe.add(nextLine[1]);
				 }
	    	}
	    	if (s == "port"){
	    		while ((nextLine = reader.readNext()) != null) {
				    // nextLine[] is an array of values from the line
				    maListe.add(nextLine[2]);
				 }	
	    	}
 
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
	     if (!maListe.get(lindice).isEmpty()){
	    	 return maListe.get(lindice);	
	     }else{
	    	 return "";
	     }
    }
 
 
 
 
 
 
    static void setMag(List<String> a){
 
    	String chemin = System.getProperty("user.dir")+"/src/mag.csv";
		File fichier = new File(chemin);
		CSVWriter ecrire;
 
		try {
			ecrire = new CSVWriter(new FileWriter(fichier));
			for (int i = 0; i < a.size(); i++){
				String [] country = a.get(i).split(",");
				ecrire.writeNext(country);
			}
 
			ecrire.close();
 
 
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
 
    }
 
 
 
	public static void SuppMag(int selectedIndex) {
		// TODO Auto-generated method stub
		List<String> magEnregistres = getMag();
 
		if (!magEnregistres.get(selectedIndex).isEmpty()){
			magEnregistres.remove(magEnregistres.get(selectedIndex));
 
			setMag(magEnregistres);
		}
 
	}
 
 
 
 
}

Je vous remercie d'avance pour toute aide, suggestion...