bonjour a tous
je vous fournis mon code des deux classes afin de pouvoir m'aider
je veux récupérer l'item selectionné de mon combobox de la class "combo" sur le label recup_item de ma classe "label"
j'ai pu l'afficher sur la console mais pas sur la classe label
voici le code
---------------------------LE CODE DE LA CLASS LABEL-----------------------
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 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * combo.java * * Created on 21 mai 2012, 22:58:20 */ /** * * @author Microbox */ /*--------------------------------------------------------------------------------------------------------------*/ public class combo extends JFrame implements ActionListener { /** * */ JLabel recup_item; /** Creates new form combo */ public combo() { initComponents(); this.setLocationRelativeTo(null); } /** 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() { jPanel1 = new javax.swing.JPanel(); jComboBox1 = new javax.swing.JComboBox(); btn_ok = new javax.swing.JButton(); btn_ok.setText("ok"); /*--------------------------------------------------------------------------------------------------------------*/ btn_ok.addActionListener(new label(this)); /*--------------------------------------------------------------------------------------------------------------*/ setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "rouge", "vert", "bleu" })); jComboBox1.addActionListener(new java.awt.event.ActionListener() { /*--------------------------------------------------------------------------------------------------------------*/ public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); /*--------------------------------------------------------------------------------------------------------------*/ btn_ok.setText("ok"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(89, 89, 89) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(btn_ok) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(63, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(71, 71, 71) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 107, Short.MAX_VALUE) .addComponent(btn_ok) .addGap(51, 51, 51)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); pack(); }// </editor-fold> private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) { //public void itemStateChanged(ItemEvent ie) { recup_item.setText(jComboBox1.getSelectedItem().toString()); //} // TODO add your handling code here: } /*--------------------------------------------------------------------------------------------------------------*/ private void btn_okActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: //new label().setVisible(true); } /*--------------------------------------------------------------------------------------------------------------*/ /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new combo().setVisible(true); } }); } // Variables declaration - do not modify public javax.swing.JButton btn_ok; public javax.swing.JComboBox jComboBox1; private javax.swing.JPanel jPanel1; // End of variables declaration /*--------------------------------------------------------------------------------------------------------------*/ public JComboBox getjComboBox1(){ //JLabel recup_item; return jComboBox1; } /*--------------------------------------------------------------------------------------------------------------*/ @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }
j'éspére que vous allez m'aidé
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 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import javax.swing.JComboBox; import javax.swing.JLabel; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * label.java * * Created on 21 mai 2012, 23:02:10 */ /** * * @author Microbox */ /*--------------------------------------------------------------------------------------------------------------*/ public class label extends javax.swing.JFrame implements ActionListener { private combo combo1; public label(combo combo1) { this.combo1=combo1; } /** Creates new form label */ public label() { initComponents(); this.setLocationRelativeTo(null); //pour ne pas fermer la fenetre précedente aprés close de label// setDefaultCloseOperation(this.DISPOSE_ON_CLOSE); } /** 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() { //public void actionPerformed1(ActionEvent arg0) { jPanel1 = new javax.swing.JPanel(); couleur = new javax.swing.JLabel(); recup_item = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); couleur.setText("couleur choisi"); recup_item.setForeground(new java.awt.Color(255, 51, 0)); recup_item.setOpaque(true); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(28, 28, 28) .addComponent(couleur, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(30, 30, 30) .addComponent(recup_item, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(45, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(64, 64, 64) .addComponent(recup_item, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(77, 77, 77) .addComponent(couleur))) .addContainerGap(151, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new label().setVisible(true); } }); } // Variables declaration - do not modify public javax.swing.JLabel couleur; private javax.swing.JPanel jPanel1; public javax.swing.JLabel recup_item; // End of variables declaration @Override /*--------------------------------------------------------------------------------------------------------------*/ public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub //recup_item.setText(combo1.jComboBox1.getSelectedItem().toString()); String jComboBox1=combo1.getjComboBox1().getSelectedItem().toString(); // recup_item.setText("jComboBox1"); System.out.println(jComboBox1); new label().setVisible(true); } /*--------------------------------------------------------------------------------------------------------------*/ }
merci d'avance
Partager