Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

Multiple Combo box load dynamically

hello there,

i have 3 combo box which should display data based on selecting another CB.i mean when i select a value from the first combo box,the second combo box should get filled with data related to cb1,then again combo box 3 should get filled with data based on the second cb selection.how can i do this.

cheers
zolf
Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER


i have this code which works partly.the problem is hen i select a value in the cb1 the cb2 takes new values.but cb3 does not take the new value. but when i select cb2 then cb3 takes new values.please help

tQuery = "select glid,GLDescription from GeneralLegder order by GLID";
            //comboBox1 = mdlFunctions.fillComboID(tQuery,connect,"glid","GLDescription");
            comboBox1 = mdlFunctions.fillComboID(tQuery,connect,"glid","GLDescription");
            comboBox1.setBounds(150, 50, 255, 25);
            
            final long id;
            
            MyType data = (MyType)comboBox1.getSelectedItem();
            id = data.getId(); // here is your id
            System.out.println(id);
            
            final String queryKol = "Select kolid,KolDescription FROM KolTotal WHERE glid ="+id;
            System.out.println(queryKol);
            comboBox2 = mdlFunctions.fillComboID(queryKol,connect,"kolid","KolDescription");
            comboBox2.setBounds(150, 90, 255, 25);
            
            comboBox1.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e)
                    {
                          if(e.getSource() instanceof JComboBox)
                          {

                                System.out.println("combo Box 1 ");
                            MyType data = (MyType)comboBox1.getSelectedItem();
                            long id = data.getId(); // here is your id
                            final String queryKol = "Select kolid,KolDescription FROM KolTotal WHERE glid ="+id;
                            System.out.println(queryKol);
                            comboBox2.setModel(mdlFunctions.fillComboBoxID(queryKol,connect,"kolid","KolDescription"));
                          }
                    }
                   });
            
            final long kolid;
            MyType data1 = (MyType)comboBox2.getSelectedItem();
            kolid = data1.getId(); // here is your id
            System.out.println(id);
            
            final String queryMohin = "Select Mohinid,MohinDescription FROM Mohin WHERE kolid ="+kolid;
            System.out.println(queryMohin);
            comboBox3 = mdlFunctions.fillComboID(queryMohin,connect,"Mohinid","MohinDescription");
            comboBox3.setBounds(150, 130, 255, 25);
            
            comboBox2.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e)
                    {
                          if(e.getSource() instanceof JComboBox)
                          {
                                System.out.println("combo box 2"+comboBox2.getSelectedItem());
                            MyType data = (MyType)comboBox2.getSelectedItem();
                            long id = data.getId(); // here is your id
                            final String queryMohin = "Select Mohinid,MohinDescription FROM Mohin WHERE kolid ="+id;
                            System.out.println(queryMohin);
                            comboBox3.setModel(mdlFunctions.fillComboBoxID(queryMohin,connect,"Mohinid","MohinDescription"));
                          }
                    }
                   });
add this
comboBox2.setSelectedIndex(0);

just after
comboBox2.setModel(mdlFunctions.fillComboBoxID(queryKol,connect,"kolid","KolDescription"));


Avatar of Zolf

ASKER


when i add a new value in the cb. i update the cb to show the new value in it.to do this i remove the items in the cb and again fill it with new values.now the problem is when i remove the cb.the action listener of the cb is called which gives this error.which is true because the cb is empty

Exception occurred during event dispatching:
java.lang.IllegalArgumentException: setSelectedIndex: 0 out of bounds
      at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
      at src.com.FormFinance$2.actionPerformed(FormFinance.java:169)
      at javax.swing.JComboBox.fireActionEvent(Unknown Source)
      at javax.swing.JComboBox.contentsChanged(Unknown Source)
      at javax.swing.JComboBox.intervalRemoved(Unknown Source)
      at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
      at javax.swing.DefaultComboBoxModel.removeAllElements(Unknown Source)
      at javax.swing.JComboBox.removeAllItems(Unknown Source)
      at src.com.FormFinance.updateComboBox(FormFinance.java:349)
      at src.com.AddNewKol$1.actionPerformed(AddNewKol.java:187)
      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)
public FormFinance(Connection srcCN,JFrame getParentFrame) throws SQLException
	{
		//Set the form caption and some properties start
		 super("Finance", 
	              true, //resizable
	              true, //closable
	              true, //maximizable
	              true);//iconifiable
		//End set the form caption and some properties
 
		 JFParentFrame = getParentFrame;
		 
		 connect = MainForm.getDBConnection();
		 
		 container = getContentPane();
		 container.setLayout(new BorderLayout());
		//Start initialize variables
		 
		JPLeftContainer.setLayout(new BorderLayout());
		JPRightContainer.setLayout(null);
 
		//-- Add the JLPicture1
		JLPicture1.setBounds(5,5,48,48);
		JPRightContainer.add(JLPicture1);
		pleaseFillupAllLabel = mdl.setJLabel(pleaseFillupAllLabel, 55, 5, 255, 42);
		
		label1 = mdl.setJLabel(label1, 55, 50, 55, 25);//add40 for y
		label2 = mdl.setJLabel(label2, 55, 90, 55, 25);	
		label3 = mdl.setJLabel(label3, 55, 130, 55, 25);
		label4 = mdl.setJLabel(label4, 55, 170, 55, 25);
		label5 = mdl.setJLabel(label5, 55, 210, 55, 25);
		label6 = mdl.setJLabel(label6, 55, 250, 55, 25);
		
		//tf1 = mdl.setJTextField(tf1, 150, 90, 255, 25);
		//tf2 = mdl.setJTextField(tf2, 150, 130, 255, 25);
		tf3 = mdl.setJTextField(tf3, 150, 170, 255, 25);
		//tf4 = mdl.setJTextField(tf4, 150, 210, 255, 25);
		
		tf11 = mdl.setJTextField(tf11, 455, 90, 55, 25);
		tf22 = mdl.setJTextField(tf22, 455, 130, 55, 25);
		tf33 = mdl.setJTextField(tf33, 455, 170, 55, 25);
		//tf44 = mdl.setJTextField(tf44, 450, 210, 55, 25);
		
		JTabbedPane tabPane = new JTabbedPane();
		
		JPanel cityPane = new JPanel();
		cityPane.setBackground(Color.GREEN);
		
		JPanel statePane = new JPanel();
		statePane.setBackground(Color.BLUE);
		
		JPanel wPane = new JPanel();
		wPane.setBackground(Color.RED);
		
		tabPane.addTab("  cityPane  ", cityPane);
		tabPane.addTab("  statePane  ", statePane);
		tabPane.addTab("  www Pane  ", wPane);
		
		
		
		tQuery = "select glid,GLDescription from GeneralLegder order by GLID";
		//comboBox1 = mdlFunctions.fillComboID(tQuery,connect,"glid","GLDescription");
		comboBox1 = mdlFunctions.fillComboID(tQuery,connect,"glid","GLDescription");
		comboBox1.setBounds(150, 50, 255, 25);
		
		final long id;
		
		MyType data = (MyType)comboBox1.getSelectedItem();
		id = data.getId(); // here is your id
		//System.out.println(id);
		
		final String queryKol = "Select kolid,KolDescription FROM KolTotal WHERE glid ="+id; 
		System.out.println(queryKol);
		comboBox2 = mdlFunctions.fillComboID(queryKol,connect,"kolid","KolDescription");
		comboBox2.setBounds(150, 90, 255, 25);
		
		comboBox1.addActionListener(new ActionListener() {
			  public void actionPerformed(ActionEvent e)
			  {
				  if(e.getSource() instanceof JComboBox)
				  {
 
					  System.out.println("combo Box 1 ");
					  MyType data;
					  long id = 1;
					  if(flag)
					  {
						  //data = (MyType)comboBox1.getSelectedItem();
						  System.out.println(flag);
						  comboBox1.setSelectedIndex(0);
						  data = (MyType)comboBox1.getSelectedItem();
						  id = data.getId();
					  }
					  else
					  {
						  System.out.println(flag);
						  data = (MyType)comboBox1.getSelectedItem();
						  id = data.getId(); // here is your id
					  }
			          
			          final String queryKol = "Select kolid,KolDescription FROM KolTotal WHERE glid ="+id; 
			          System.out.println(queryKol);
			          comboBox2.setModel(mdlFunctions.fillComboBoxID(queryKol,connect,"kolid","KolDescription"));
			          
 
			          
			          System.out.println("combo box 2"+comboBox2.getSelectedItem());
			          Object obj = comboBox2.getSelectedItem();
			          System.out.println("combo box 2  "+obj);
			          if(obj == null)
			          {
			        	  System.out.println("if  "+obj);
			        	  comboBox3.removeAllItems();
			          }
			          else
			          {
			        	  System.out.println("else  "+obj);
			        	  MyType data1 = (MyType)comboBox2.getSelectedItem();
				          long id1 = data1.getId(); // here is your id
				          final String queryMohin = "Select Mohinid,MohinDescription FROM Mohin WHERE kolid ="+id1; 
				          System.out.println(queryMohin);
				          comboBox3.setModel(mdlFunctions.fillComboBoxID(queryMohin,connect,"Mohinid","MohinDescription"));
			          }
				  }
			  }
			 });
		
		final long kolid;
		MyType data1 = (MyType)comboBox2.getSelectedItem();
		kolid = data1.getId(); // here is your id
		//System.out.println(id);
		
		final String queryMohin = "Select Mohinid,MohinDescription FROM Mohin WHERE kolid ="+kolid; 
		System.out.println(queryMohin);
		comboBox3 = mdlFunctions.fillComboID(queryMohin,connect,"Mohinid","MohinDescription");
		comboBox3.setBounds(150, 130, 255, 25);
		
		comboBox2.addActionListener(new ActionListener() {
			  public void actionPerformed(ActionEvent e)
			  {
				  if(e.getSource() instanceof JComboBox)
				  {
					  System.out.println("combo box 2"+comboBox2.getSelectedItem());
			          MyType data = (MyType)comboBox2.getSelectedItem();
			          long id = data.getId(); // here is your id
			          final String queryMohin = "Select Mohinid,MohinDescription FROM Mohin WHERE kolid ="+id; 
			          System.out.println(queryMohin);
			          comboBox3.setModel(mdlFunctions.fillComboBoxID(queryMohin,connect,"Mohinid","MohinDescription"));
				  }
			  }
			 });
		
 
================================================================
 
 
public static void updateComboBox()
    {
          System.out.println(comboBox1.getItemCount());
          flag = true;
          for( int i = 0; i < comboBox1.getItemCount(); i++ )
          {
            System.out.println(comboBox1.getItemAt(i));
          }
         
          System.out.println("Before remove  "+comboBox1.getItemCount());
          comboBox1.removeAllItems();
          System.out.println("After remove  "+comboBox1.getItemCount());
          //comboBox1 = mdlFunctions.fillComboID(tQuery,connect,"glid","GLDescription");
          JComboBox temp = mdlFunctions.fillComboID(tQuery,connect,"glid","GLDescription");
          System.out.println(temp.getItemCount());
          
          for( int i = 0; i < temp.getItemCount(); i++ )
          {
        	  comboBox1.addItem(temp.getItemAt(i));
          }
    }

Open in new window

Avatar of Zolf

ASKER


before adding the new values the action listener of cb1 is called in void updateComboBox()
ASKER CERTIFIED SOLUTION
Avatar of ysnky
ysnky
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zolf

ASKER


thanks mate