Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: JcomboBox showing only one item

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JcomboBox showing only one item

    Whats wrong with my code? the second JcomboBox is showing only one item even if there is more in the database.

    @Override
    	public void actionPerformed(ActionEvent e) {
    		Object o = e.getSource();
    		if(o == comboBox1) {			
    			Object matricule =  comboBox1.getSelectedItem();
    			String sqll = "Select  * FROM clients WHERE matricule = " +matricule;				
     
    				try {
    					rs = stat.executeQuery(sqll);
    					while(rs.next()) {
    						nom.setText(rs.getString("nom"));
    						prenom.setText(rs.getString("prenom"));
    						cin.setText(rs.getString("cin"));
    						adresse.setText(rs.getString("adresse"));					
    					}
    				} catch (SQLException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}	
     
    			comboBox2.removeAllItems();
     
    			Object index = comboBox1.getSelectedItem();
    			String sql = "Select  distinct p.code FROM parcelle p, clients c WHERE c.matricule = p.exploitant AND c.matricule = " +index;
     
    				try {
    					rs = stat.executeQuery(sql);
    					while(rs.next()) {
    						System.out.println(rs.getInt("code"));
    						comboBox2.addItem(rs.getInt("code"));
    					}
    				} catch (SQLException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}			
    			return;
    		}		
     
                 Object code =  comboBox2.getSelectedItem();  
                 String sqll = "Select  * FROM parcelle WHERE code = " +code;  
     
                 try {  
                     rs = stat.executeQuery(sqll);                     
     
                     while(rs.next()) {  
                         sau.setText(rs.getString("sau"));  
                         sol.setText(rs.getString("type_sol"));  
                         irrigation.setText(rs.getString("mode_irrigation"));  
                         exploitation.setText(rs.getString("type_exploitation"));                      
                     }  
     
                 } catch (SQLException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    			}		
    	}


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: JcomboBox showing only one item

    only one item even if there is more in the database.
    It doesn't matter how many are in your database....does your query return more than one? Does the println in your loop print out more than one?

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JcomboBox showing only one item

    Yes the query return more than one:


    The println print out just one.
    Attached Images Attached Images

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: JcomboBox showing only one item

    I can't see anything obvious from the code you posted that would cause this. When the value prints...does it print what you would expect (eg 10)? Have you tried using a PreparedStatement?

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JcomboBox showing only one item

    I expect for exemple 10 and 11 but it only prints 10.
    I just realised that if i delete this part:

    Object code =  comboBox2.getSelectedItem();  
                 String sqll = "Select  * FROM parcelle WHERE code = " +code;  
     
                 try {  
                     rs = stat.executeQuery(sqll);                     
     
                     while(rs.next()) {  
                         sau.setText(rs.getString("sau"));  
                         sol.setText(rs.getString("type_sol"));  
                         irrigation.setText(rs.getString("mode_irrigation"));  
                         exploitation.setText(rs.getString("type_exploitation"));                      
                     }  
     
                 } catch (SQLException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    			}

    It works perfectly but i need some Jlabels to be filled with data from the second comboBox item choice.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JcomboBox showing only one item

    Just as a thought, could you try the getItemCount() method in a println to check to see how many items are indeed in the ComboBox?

    That way you could tell if only one item is being added or if there's more than one but somehow only one is showing up.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: JcomboBox showing only one item

    Quote Originally Posted by javapenguin View Post
    Just as a thought, could you try the getItemCount() method in a println to check to see how many items are indeed in the ComboBox?

    That way you could tell if only one item is being added or if there's more than one but somehow only one is showing up.
    The original poster knows only one is being added, which was discussed above (and under what circumstances would a JComboBox have multiple items but only show one?)

    Quote Originally Posted by ishiro
    I just realised that if i delete this part:
    I don't see anything obvious which would have the affect you are describing. Again though, I would recommend a) using a PreparedStatement b) if you have not yet, print out the query you are executing to be sure it is what you think it is c) create different ActionListeners per component to separate the behaviors
    Last edited by copeg; May 31st, 2012 at 10:32 AM.

Similar Threads

  1. Replies: 48
    Last Post: April 3rd, 2012, 04:26 PM
  2. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  3. JComboBox selection not showing item from object array
    By oper125 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2010, 06:31 AM
  4. Problem getting selected item as string from JComboBox
    By lost in forum AWT / Java Swing
    Replies: 1
    Last Post: October 26th, 2010, 04:22 AM
  5. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 08:37 PM