Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 16/04/2008, à 20:44

jalelouss

Commander un JcomboBox2 par un autre JcomboBox1

Bonsoir tout le monde ,ben ,Voici un exemple de mon probleme et comme l'indique le titre je vais faire commander un JcomBox2 avec un autre mais la j'ai un probleme :
lors du deuxieme click sur jComboBox1 pour remplir des elts dans JcomboBox2
(si je click sur un item du JcomboBox2 , le programme m'affiche deux fois de click sur JcomBox2.
Ben Exécuter le code et vous verrez mon probleme .Aidez moi SVP c'est tres urgent parce que je l'utiliserai dans mon code

 
/*
 * Ex.java
 *
 * Created on __DATE__, __TIME__
 */
 
package pac;
 
import java.awt.Event;
 
import javax.swing.JComboBox;
 
/**
 *
 * @author  __USER__
 */
public class Ex extends java.awt.Frame {
 
	/** Creates new form Ex */
	public Ex() {
		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() {
		jPanel1 = new javax.swing.JPanel();
		jComboBox1 = new javax.swing.JComboBox();
		jComboBox2 = new javax.swing.JComboBox();
 
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				exitForm(evt);
			}
		});
 
		jPanel1.add(jComboBox1);
		String []xml=new String[3] ;
		xml[0]=new String("jalel");
		xml[1]=new String("titi");
		xml[2]=new String("tata");
		for(int i=0;i<3;i++)
		jComboBox1.addItem(xml[i]);
 
		jPanel1.add(jComboBox2);
 
		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(jPanel1,
				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 400,
				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE));
		layout.setVerticalGroup(layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(jPanel1,
				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 300,
				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE));
		pack();
		jComboBox1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				OuvrirActionPerformed(evt);
			
		}});
		}// </editor-fold>//GEN-END:initComponents
 
	/** Exit the Application */
	private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
		System.exit(0);
	}//GEN-LAST:event_exitForm
	private void OuvrirActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		System.out.println("click");
		
		//jPanel1.remove(jComboBox2);
	//	jComboBox2=null;
		//jComboBox2=new JComboBox();
		//jComboBox2.setVisible(true);
		//jComboBox2.setEnabled(true);
	//	jPanel1.add(jComboBox2);
		System.out.println("click1");
		String []xml=new String[3] ;
		xml[0]=new String("jalel");
		xml[1]=new String("titi");
		xml[2]=new String("tata");
		for(int i=0;i<3;i++)
		jComboBox2.addItem(xml[i]);
		System.out.println("click3");
		for(int i=0;i<3;i++)
			System.out.println(jComboBox2.getItemAt(i).toString());
		//System.out.println(evt.getSource().toString());
		jComboBox2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				Ouvrir1ActionPerformed(evt);
			
		}});
		
	}
	private void Ouvrir1ActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		//jComboBox2.removeAllItems();
		//System.out.println(evt.getSource().toString());
	
	System.out.println(jComboBox2.getSelectedIndex());
		
	}
	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Ex().setVisible(true);
			}
		});
	}
 
	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JComboBox jComboBox1;
 
	private javax.swing.JComboBox jComboBox2;
 
	private javax.swing.JPanel jPanel1;
	// End of variables declaration//GEN-END:variables
 
}

Hors ligne