Bonjour,
Situons le problème :
J'ai une application graphique faite en java avec swing.
Elle elle est composée de deux tables liées à une base de données sqlite.
La deuxième table doit à terme n'afficher que les enregistrements correspondants à l'index sélectionné dans la première table.
-j'ai 3 classes :
-une classe pour la partie graphique
-une classe pour la gestion des données
-une classe appelée lors d'un événement (changement de sélection de ligne dans la première table)
Mon problème est que quand je suis dans la méthode appelée lors de l’événement, je ne peux (ou ne sais) rien faire à part afficher un message dans le terminal.
Pour simplifier, j'ai refait une application très basique sans base de donnée.
Le problème est le même :
classe GUI : événement => classe de l'événement : aucune action possible pour aller dans la classe data et gérer la mise à jours des données.
La classe data a besoin de l'objet "table", de l'objet "model" et de l'index récupéré dans la méthode de l'événement.
Le main est dans la classe GUI.
Un objet data (classe de gestion des données) est instancié dans la classe GUI.
Voici mon code exemple (j'ai remplacé la base de donnée par des tableaux). Si vous voulez tester, les 3 fichiers suffisent.
Il y a beaucoup de lignes dans la classe GUI, j'en suis désolé, c'est du code généré par netbeans pour l'interface graphique.
Classe pour la GUI :
Classe pour la gestion des données :
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 package app; import javax.swing.ListSelectionModel; import javax.swing.table.DefaultTableModel; public class Gui extends javax.swing.JFrame { //FIELDS private DefaultTableModel table1_model; //la table du haut private DefaultTableModel table2_model; //la table du bas ou "sous-table" private final Data data; //objet de la classe data : gestion des donnees private final ListSelectionModel list_slct_model1; //CONSTRUCTOR public Gui() { this.data = new Data(); initComponents(); this.data.getTable1Date(this.jTable1,this.table1_model); //on remplit la table du haut // les 3 lignes suivantes servent à la gestion de l'événement déclenché sur changement de ligne de la table du haut this.list_slct_model1 = this.jTable1.getSelectionModel(); this.list_slct_model1.addListSelectionListener(new Table1SelectionChangedHandler()); this.jTable1.setSelectionModel(list_slct_model1); // this.data.getTable2Date(this.jTable2,this.table2_model);// on remplie la table du bas : ligne a supprimer une fois la solution trouvee } // EN DESSOUS IL N Y A RIEN D INTERESSANT POUR NOUS A MON AVIS //METHODS /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jScrollPane2 = new javax.swing.JScrollPane(); jTable2 = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { }, new String [] { "Id", "Type" } ) { Class[] types = new Class [] { java.lang.Integer.class, java.lang.String.class }; boolean[] canEdit = new boolean [] { false, false }; public Class getColumnClass(int columnIndex) { return types [columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } }); jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jTable1.getTableHeader().setReorderingAllowed(false); jScrollPane1.setViewportView(jTable1); if (jTable1.getColumnModel().getColumnCount() > 0) { jTable1.getColumnModel().getColumn(0).setResizable(false); jTable1.getColumnModel().getColumn(1).setResizable(false); } jTable2.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { }, new String [] { "Id", "TypeId", "Name" } ) { Class[] types = new Class [] { java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class }; boolean[] canEdit = new boolean [] { false, false, false }; public Class getColumnClass(int columnIndex) { return types [columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } }); jTable2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jTable2.getTableHeader().setReorderingAllowed(false); jScrollPane2.setViewportView(jTable2); if (jTable2.getColumnModel().getColumnCount() > 0) { jTable2.getColumnModel().getColumn(0).setResizable(false); jTable2.getColumnModel().getColumn(1).setResizable(false); jTable2.getColumnModel().getColumn(2).setResizable(false); } javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)) ); pack(); }// </editor-fold> public static void main(String args[]) { /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Gui().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTable jTable1; private javax.swing.JTable jTable2; // End of variables declaration }
Classe appelée lors de l'événement :
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 package app; import java.util.ArrayList; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class Data { public String table1_data[][]=new String[3][2]; public String table2_data[][]=new String[10][3]; public Data(){ this.table1_data[0][0]="1"; this.table1_data[0][1]="fruit"; this.table1_data[1][0]="2"; this.table1_data[1][1]="legumes"; this.table1_data[2][0]="3"; this.table1_data[2][1]="autre"; this.table2_data[0][0]="1"; this.table2_data[0][1]="2"; this.table2_data[0][2]="carotte"; this.table2_data[1][0]="2"; this.table2_data[1][1]="1"; this.table2_data[1][2]="banane"; this.table2_data[2][0]="3"; this.table2_data[2][1]="1"; this.table2_data[2][2]="pomme"; this.table2_data[3][0]="4"; this.table2_data[3][1]="2"; this.table2_data[3][2]="radis"; this.table2_data[4][0]="5"; this.table2_data[4][1]="1"; this.table2_data[4][2]="cerise"; this.table2_data[5][0]="6"; this.table2_data[5][1]="3"; this.table2_data[5][2]="cailloux"; this.table2_data[6][0]="7"; this.table2_data[6][1]="1"; this.table2_data[6][2]="poire"; this.table2_data[7][0]="8"; this.table2_data[7][1]="2"; this.table2_data[7][2]="navet"; this.table2_data[8][0]="9"; this.table2_data[8][1]="3"; this.table2_data[8][2]="avion"; this.table2_data[9][0]="10"; this.table2_data[9][1]="1"; this.table2_data[9][2]="abricot"; } public void getTable1Date(JTable table,DefaultTableModel model){ ArrayList array = new ArrayList(); model = (DefaultTableModel)table.getModel(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { array.add(this.table1_data[i][j]); } model.addRow(array.toArray()); array.clear(); } table.setModel(model); } public void getTable2Date(JTable table,DefaultTableModel model){ ArrayList array = new ArrayList(); model = (DefaultTableModel)table.getModel(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 3; j++) { array.add(this.table2_data[i][j]); } model.addRow(array.toArray()); array.clear(); } table.setModel(model); } }
Je suis désolé pour la longueur. J'ai essayé de mettre le maximum d'informations possibles pour la compréhension.
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 package app; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class Table1SelectionChangedHandler implements ListSelectionListener{ public void valueChanged(ListSelectionEvent e){ ListSelectionModel lsm = (ListSelectionModel)e.getSource(); boolean isAdjusting = e.getValueIsAdjusting();//gere le clic (true) et le moment ou on relache le bouton (false) if (isAdjusting==false) { //on effectue une action seulement quand in relache le clic souris ou la touche clavier if (lsm.isSelectionEmpty()) { System.out.println("NONE"); } else { int minIndex = lsm.getMinSelectionIndex(); int maxIndex = lsm.getMaxSelectionIndex(); for (int i = minIndex; i <= maxIndex; i++) { if (lsm.isSelectedIndex(i)) { System.out.println("Voila l'index que je dois utiliser : "+i); } } } } } }
S'il manque quelque chose n'hésitez pas à me le demander.
Je met aussi ces 3 fichiers en pièces jointes.
Pour résumer la question : comment m'en sortir pour mettre à jours ma table2 en fonction de la sélection d'une ligne dans la première table ?
Merci d'avance pour vos réponses.
Partager