voila un nouveau utilisateur de jTable;
je veux créer un tableau dont le nombre de colonn change selon un nombre de colonne saisie.
jai essayé avec un boucle mais ca ne marche pas pouvez vous m'aidez
Version imprimable
voila un nouveau utilisateur de jTable;
je veux créer un tableau dont le nombre de colonn change selon un nombre de colonne saisie.
jai essayé avec un boucle mais ca ne marche pas pouvez vous m'aidez
Tu peux passer par un tableModel pour ça
merci pour laide mais pouvez vous mexpliquez comment le faire avec jtablemodel
Code:
1
2
3
4
5 int nbLignes = 4; int nbColonnes = 6; DefaultTableModel model = new DefaultTableModel(nbLignes, nbColonnes); JTable t = new JTable(model);
merci pour laide mais je pense que ce nas pas marche a cause des fautes dans la classe table Model pouvez vous maidez
ça n'a pas marché ?
il faut donner plus de détails : à quoi vois-tu que ça n'a pas marché ? le programme se plante ? as-tu un message d'erreur ?
montre-nous aussi ton code ça peut aider...
voici le code de classe table model:
package MyPackage;
import javax.swing.table.AbstractTableModel;
public class PayoffTableModel extends AbstractTableModel{
String ColumnName[];
Object Donnees[][];
//Construction de la table
public PayoffTableModel(int Outcomes,int Actions)
{
int i,j;
for(i=0;i<=Actions;i++)
{
if(i==0)
ColumnName[i]="test";
else
ColumnName[i]="Outcomes"+i;
}
for(i=0;i<=Actions;i++)
{
if(i==0){
for(j=0;j<=Outcomes;j++)
if(j==0)
Donnees[i][j]="Probability";
else
Donnees[i][j]="Actions"+i;
}
else{
for(j=0;j<=Outcomes;j++)
Donnees[i][j]=" test";
}
}
}
public int getColumnCount()
{
return ColumnName.length;
}
public int getRowCount()
{
return Donnees.length;
}
public String getColumnName(int col)
{
return ColumnName[col];
}
public Class getColumnClass(int col)
{
return String.class;
}
public Object getValueAt(int parm1, int parm2)
{
return Donnees[parm1][parm2];
}
public void setValueAt(Object aValue, int parm1, int parm2) {
}
public boolean isCellEditable(int row, int col) {
if(col==0)
return false;
else
return true;
}
}
et voici le code de la classe graphique
private JScrollPane getJScrollPaneTable() {
if (jScrollPaneTable == null) {
jScrollPaneTable = new JScrollPane();
jScrollPaneTable.setBounds(new Rectangle(5, 258, 10, 10));
jScrollPaneTable.setVisible(false);
jScrollPaneTable.setViewportView(getPayoffMatrice());
}
return jScrollPaneTable;
}
/**
* This method initializes PayoffMatrice
*
* @return javax.swing.JTable
*/
private JTable getPayoffMatrice() {
if (PayoffMatrice == null) {
//int nbLignes = Integer.parseInt(Actions.getText());
//int nbColonnes = Integer.parseInt(Outcomes.getText());
int nbLignes = 3;
int nbColonnes = 4;
PayoffTableModel model = new PayoffTableModel(nbLignes, nbColonnes);
//JTable t = new JTable(model);
PayoffMatrice = new JTable(model);
}
return PayoffMatrice;
}
private JButton getNext() {
if (Next == null) {
Next = new JButton();
Next.setBounds(new Rectangle(335, 209, 72, 32));
Next.setText("Next");
Next.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()");
jPanel1.setVisible(false);
jScrollPaneTable.setVisible(true);
}
});
}
return Next;
}
ce code me renvoie les exceptions suivantes:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MyPackage.PayoffTableModel.<init>(PayoffTableModel.java:14)
at MyPackage.InterfaceProjet.getPayoffMatrice(InterfaceProjet.java:189)
at MyPackage.InterfaceProjet.getJScrollPaneTable(InterfaceProjet.java:173)
at MyPackage.InterfaceProjet.getJContentPane(InterfaceProjet.java:243)
at MyPackage.InterfaceProjet.initialize(InterfaceProjet.java:229)
at MyPackage.InterfaceProjet.<init>(InterfaceProjet.java:216)
at MyPackage.InterfaceProjet$2.run(InterfaceProjet.java:204)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
:help: il ya quelqun qui peut maider
Il faudrait que tu remettes ton code avec les balises [code], car là c'est limite illisible :/
Ton problème c'est que ton tableau ColumnName[] dans le constructeur vaut null.
J'arrive à dire ça simplement en regardant la pile.
Souviens-toi toujours que la pile te donne l'endroit exact à peu de chose près ou survient l'erreur.
Les données membres "ColumnName" et "Donnees" ne sont pas instanciées.
jai pas pu corriger mon code alors je lenvoie plus claire,
voici le code deTable model
et ceci le code de mon interface graphiqueCode:
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 public class PayoffTableModel extends AbstractTableModel{ String[] ColumnName; String[][] Donnees; public PayoffTableModel(int Outcomes,int Actions) { int i,j; for(i=0;i<=Actions;i++) { if(i==0) ColumnName[i]="test"; else ColumnName[i]="Outcomes"+i; } for(i=0;i<=Actions;i++) { if(i==0){ for(j=0;j<=Outcomes;j++) if(j==0) Donnees[i][j]="Probability"; else Donnees[i][j]="Actions"+i; } else{ for(j=0;j<=Outcomes;j++) Donnees[i][j]=" test"; } } } public int getColumnCount() { return ColumnName.length; } public int getRowCount() { return Donnees.length; } public String getColumnName(int col) { return ColumnName[col]; } public Class getColumnClass(int col) { return String.class; } public String getValueAt(int parm1, int parm2) { return Donnees[parm1][parm2]; } public void setValueAt(Object aValue, int parm1, int parm2) { } public boolean isCellEditable(int row, int col) { if(col==0) return false; else return true; } }
en gras le code concernant le tableauCode:
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 public class InterfaceProjet extends JFrame{ private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JPanel jPanel1 = null; private JLabel jLabel11 = null; private JRadioButton TypeDecision1 = null; private JRadioButton TypeDecision2 = null; private JRadioButton TypeDecision3 = null; private JLabel jLabel12 = null; private JTextField Outcomes = null; private JButton Next = null; private JLabel jLabel13 = null; private JTextField Actions = null; private JScrollPane jScrollPaneTable = null; private JTable PayoffMatrice = null; /** * This method initializes jPanel1 * * @return javax.swing.JPanel */ private JPanel getJPanel1() { if (jPanel1 == null) { jLabel13 = new JLabel(); jLabel13.setBounds(new Rectangle(210, 150, 111, 31)); jLabel13.setText("Number of Actions:"); jLabel12 = new JLabel(); jLabel12.setBounds(new Rectangle(15, 150, 138, 28)); jLabel12.setText("Number of Outcomes:"); jLabel11 = new JLabel(); jLabel11.setText("Please Insert the switable type of decision"); jLabel11.setBounds(new Rectangle(16, 15, 320, 22)); jPanel1 = new JPanel(); jPanel1.setLayout(null); jPanel1.setBounds(new Rectangle(0, 0, 451, 253)); jPanel1.setVisible(true); jPanel1.add(jLabel11, null); jPanel1.add(getTypeDecision1(), null); jPanel1.add(getTypeDecision2(), null); jPanel1.add(getTypeDecision3(), null); jPanel1.add(jLabel12, null); jPanel1.add(getOutcomes(), null); jPanel1.add(getNext(), null); jPanel1.add(jLabel13, null); jPanel1.add(getActions(), null); } return jPanel1; } /** * This method initializes TypeDecision1 * * @return javax.swing.JRadioButton */ private JRadioButton getTypeDecision1() { if (TypeDecision1 == null) { TypeDecision1 = new JRadioButton(); TypeDecision1.setBounds(new Rectangle(31, 45, 302, 21)); TypeDecision1.setText("Decision Making under Pure Uncertainly"); } return TypeDecision1; } /** * This method initializes TypeDecision2 * * @return javax.swing.JRadioButton */ private JRadioButton getTypeDecision2() { if (TypeDecision2 == null) { TypeDecision2 = new JRadioButton(); TypeDecision2.setBounds(new Rectangle(30, 75, 303, 21)); TypeDecision2.setText("Decision Making Under Perfect Information"); } return TypeDecision2; } /** * This method initializes TypeDecision3 * * @return javax.swing.JRadioButton */ private JRadioButton getTypeDecision3() { if (TypeDecision3 == null) { TypeDecision3 = new JRadioButton(); TypeDecision3.setBounds(new Rectangle(29, 105, 302, 21)); TypeDecision3.setText("Decision Making By Buying Reliable Information"); } return TypeDecision3; } /** * This method initializes Outcomes * * @return javax.swing.JTextField */ private JTextField getOutcomes() { if (Outcomes == null) { Outcomes = new JTextField(); Outcomes.setBounds(new Rectangle(154, 151, 32, 28)); } return Outcomes; } /** * This method initializes Next * * @return javax.swing.JButton */ private JButton getNext() { if (Next == null) { Next = new JButton(); Next.setBounds(new Rectangle(335, 209, 72, 32)); Next.setText("Next"); Next.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("actionPerformed()"); jPanel1.setVisible(false); int nbColonnes = 3; int nbLignes = 4; PayoffTableModel model = new PayoffTableModel(nbLignes, nbColonnes); // creation de la table PayoffMatrice = new JTable(model); /// Le scrollPane de la table jScrollPaneTable = new JScrollPane(PayoffMatrice); } }); } return Next; } /** * This method initializes Actions * * @return javax.swing.JTextField */ private JTextField getActions() { if (Actions == null) { Actions = new JTextField(); Actions.setBounds(new Rectangle(321, 151, 32, 31)); } return Actions; } /** * This method initializes jScrollPaneTable * * @return javax.swing.JScrollPane */ private JScrollPane getJScrollPaneTable() { if (jScrollPaneTable == null) { jScrollPaneTable = new JScrollPane(); jScrollPaneTable.setBounds(new Rectangle(5, 258, 10, 10)); jScrollPaneTable.setVisible(false); jScrollPaneTable.setViewportView(getPayoffMatrice()); } return jScrollPaneTable; } /** * This method initializes PayoffMatrice * * @return javax.swing.JTable */ private JTable getPayoffMatrice() { if (PayoffMatrice == null) { PayoffMatrice = new JTable(); } return PayoffMatrice; } /** * @param args */ public static void main(final String[] args) { // TODO Auto-generated method stub SwingUtilities.invokeLater(new Runnable() { public void run() { InterfaceProjet thisClass = new InterfaceProjet(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); } /** * This is the default constructor */ public InterfaceProjet() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(475, 289); this.setContentPane(getJContentPane()); this.setTitle("Decision Tree"); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getJPanel1(), null); jContentPane.add(getJScrollPaneTable(), null); } return jContentPane; } }
l'erreur je lai déja initialisé
Code:
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 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at MyPackage.PayoffTableModel.<init>(PayoffTableModel.java:14) at MyPackage.InterfaceProjet$1.actionPerformed(InterfaceProjet.java:145) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
merci pour laide c vrai quil nya plus d'exception avec l'instantiation mais sans affichage du tableau:roll:
please help me
c'est fait c simple il suffit d'ajouter le scrollpanel au panel principal du frame merci pour vos aide