Recuperer contenu jcombobox

wassimch Messages postés 1 Date d'inscription lundi 11 février 2008 Statut Membre Dernière intervention 5 octobre 2011 - 5 oct. 2011 à 21:23
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 6 oct. 2011 à 09:38
/*
 * PanelPerf.java
 *
 * Created on __DATE__, __TIME__
 */

/**
 *
 * @author  __USER__
 */
public class PanelPerf extends javax.swing.JPanel {

String init "", dest "";

/** Creates new form PanelPerf */
public PanelPerf() {
initComponents();
}

/** 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.
 */
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jComboBox2 = new javax.swing.JComboBox();

jLabel1.setText("Choose The Intiator");

jLabel2.setText("Choose The Destinator");

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBox1.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
jComboBox1ItemStateChanged(evt);
}
});
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});

jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"Item 1", "Item 2", "Item 3", "Item 4" }));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout
.setHorizontalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGap(165, 165, 165)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
122,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
jLabel2,
javax.swing.GroupLayout.PREFERRED_SIZE,
133,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(153, 153, 153)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(
jComboBox2,
0,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(
jComboBox1, 0,
114,
Short.MAX_VALUE))
.addContainerGap(257, Short.MAX_VALUE)));
layout
.setVerticalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGap(77, 77, 77)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
26,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
jComboBox1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(42, 42, 42)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jLabel2,
javax.swing.GroupLayout.PREFERRED_SIZE,
27,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
jComboBox2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(166, Short.MAX_VALUE)));
}// </editor-fold>
//GEN-END:initComponents

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
init = jComboBox1.getSelectedItem().toString();
}

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
init = jComboBox1.getSelectedItem().toString();
}

public String GetjComboBox1() {
return jComboBox1.getSelectedItem().toString();
}

public String GetjComboBox2() {
return jComboBox2.getSelectedItem().toString();
}

//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration//GEN-END:variables

}

je voudrai recuperer le contenu de jcombobox à partie du menu principale mais le probleme est que je recupere seulement le 1er item.y a il une sol pour recuperer le contenu et merci :)

2 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
6 oct. 2011 à 09:33
J'avais décidé de ne plus répondre aux gens qui ne commençaient pas leur message par bonjour mais comme c'est ton premier message, ca passe.

Pour récupérer tous les éléments de ton JComboBox, il faut d'abord que tu accèdes au modèle combobox.getModel(), puis que tu récupères la taille de ta combobox en appelant sur le modèle getSize() et ensuite, tu peux itérer sur les éléments avec getElementAt()
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
6 oct. 2011 à 09:38
Et j'ai oublié de te dire que pour faire ceci, tu dois caster le résultat de getModel() en DefaultComboBoxModel.

Cette opération ne lèvera pas de ClassCastException puisque tu as bien ajouté un DefaultComboBoxModel à ta combo.
0
Rejoignez-nous