We help IT Professionals succeed at work.

Multiple Combo box load dynamically

zolf
zolf asked
on
Medium Priority
1,200 Views
Last Modified: 2010-07-27
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
Comment
Watch Question

Author

Commented:

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"));
                          }
                    }
                   });

Commented:
add this
comboBox2.setSelectedIndex(0);

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


Author

Commented:

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

Author

Commented:

before adding the new values the action listener of cb1 is called in void updateComboBox()
Commented:
>>setSelectedIndex: 0 out of bounds
so you need to check if combobox is empty.

here is a sample to update Multiple Combo box load dynamically with which you can inspire.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;
 
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class ComboBoxMenuExample extends JFrame {
 
	static JComboBox combo1 = null;
	static JComboBox combo2 = null;
	static JComboBox combo3 = null;
	
	public class MyType {
		
		long id;
		String value;		
		
		public MyType(long id, String value) {
			this.id = id;
			this.value = value;
		}
		public String toString() {
			
			return value;
		}
		
		public long getId() {
			
			return id;
		}		
	}
 
	/**
	 * @param args
	 */
	
	public JComboBox test1() {
		
		final JComboBox combo = new JComboBox();
		 
		MyType  d1 = new MyType(1, "a");
		MyType  d2 = new MyType(2, "b");
		 
		Vector v = new Vector();
		v.add(d1);
		v.add(d2);
		 
		ComboBoxModel model = new DefaultComboBoxModel(v);
		combo.setModel(model);		
		 
		combo.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
			 	  MyType data = (MyType)combo.getSelectedItem();
			 	  long id = data.getId(); // here is your id
			 	  
			 	  ComboBoxModel model2 = new DefaultComboBoxModel(getData2(id));
			 	  combo2.setModel(model2);
			 	  if (combo2.getItemCount()>0)
			 		  combo2.setSelectedIndex(0);
			}
		});
		
		return combo;
	}
	
	public Vector getData2(long sender) {
 
		Vector v = new Vector();
		if (sender == 1) {
			MyType  d1 = new MyType(1, "a1");
			MyType  d2 = new MyType(2, "a2");		 	
			v.add(d1);
			v.add(d2);
		} else {
			MyType  d1 = new MyType(3, "b1");
			MyType  d2 = new MyType(4, "b2");		 		
			v.add(d1);
			v.add(d2);
		}
		return v;
	}
	
	public Vector getData3(long sender) {
 
		Vector v = new Vector();
		if (sender == 1) {
			MyType  d1 = new MyType(1, "a1 1");
			MyType  d2 = new MyType(2, "a1 2");		 	
			v.add(d1);
			v.add(d2);
		} else if (sender == 2){
 
			MyType  d1 = new MyType(1, "a2 1");
			MyType  d2 = new MyType(2, "a2 2");		 	
			v.add(d1);
			v.add(d2);
		} else if (sender == 3){
 
			MyType  d1 = new MyType(1, "b1 1");
			MyType  d2 = new MyType(2, "b1 2");		 	
			v.add(d1);
			v.add(d2);
		} else {
			
			MyType  d1 = new MyType(1, "b2 1");
			MyType  d2 = new MyType(2, "b2 2");		 	
			v.add(d1);
			v.add(d2);
		}
		
		
		return v;
	}
 
	
	public JComboBox test2() {
		
		final JComboBox combo = new JComboBox();
		 		 		
		combo.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
			 	  MyType data = (MyType)combo.getSelectedItem();
			 	  long id = data.getId(); // here is your id
			 	  
			 	  System.out.println("combo2 id: " + id);
			 	  ComboBoxModel model3 = new DefaultComboBoxModel(getData3(id));
			 	  combo3.setModel(model3);
			}
		});
		return combo;
	}
 
	public JComboBox test3() {
		
		final JComboBox combo = new JComboBox();
		 		 
		combo.addItemListener(new ItemListener(){
		   public void itemStateChanged(ItemEvent ie){
		 	  
		 	  MyType data = (MyType)combo.getSelectedItem();
		 	  long id = data.getId(); // here is your id
		   }
		 });		
		
		return combo;
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		ComboBoxMenuExample c = new ComboBoxMenuExample();
				
		JFrame frame = new JFrame("Creating a JComboBox Component");
	    JPanel panel = new JPanel();
	    combo1 = c.test1();
	    combo2 = c.test2();
	    combo3 = c.test3();
	    panel.add(combo1);
	    panel.add(combo2);
	    panel.add(combo3);
	    	    
	    if (combo1.getItemCount()>0)
	    	combo1.setSelectedIndex(0);
	    
	    frame.getContentPane().add(panel);
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setSize(400,400);
	    frame.setVisible(true);
	}
 
}

Open in new window

Author

Commented:

thanks mate

Explore More ContentExplore courses, solutions, and other research materials related to this topic.