Results 1 to 4 of 4
Like Tree3Likes
  • 1 Post By Fubarable
  • 1 Post By sh4rif
  • 1 Post By Fubarable

Thread: Jcombobox an empty row

  1. #1
    sh4rif is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Jcombobox an empty row

    Hello Every One....

    I am new in java and i am making a goods intake form for a company.. my question is when I load the data from database to a Jcombobox, the combobox always show the first record.. (displayed in the jcombobox)
    I want an empty row at the to be displayed in the combobox at the top of the combobox and if user has selected any item then this empty space should disappear... (sorry for the language i dont have a very good comman over english)....

    by the way the code is below
    Java Code:
    private void setComboBoxForProduct(){
    		
    	Products.getProductsForCombo();
    		
    	cmbProductName = new JComboBox<Object>();
    		
    	for(int i = 0; i<Products.productName.size(); i++){
    		cmbProductName.addItem(Products.productName.get(i));
    	}
    	cmbProductName.setEditable(true);
    	cmbProductName.addItemListener(this);
    	new ComboBoxAutoFill(cmbProductName);
    }
    
    //item listener on combobox
    public void itemStateChanged(ItemEvent ie){
    	if(ie.getStateChange() == ItemEvent.SELECTED){
    		productID = Products.productID.get(cmbProductName.getSelectedIndex());
    		productType = Products.productType.get(cmbProductName.getSelectedIndex());
    		
    		if(productType.equals("Chilled"))
    			rdoChilled.setSelected(true);
    		else if(productType.equals("Ambient"))
    			rdoAmbient.setSelected(true);		
    	}
    }

    Thank you very much for viewing the code
    Thanks
    Usman

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Jcombobox an empty row

    Before you add the ItemListener to the JComboBox and after adding your items, try setting the JComboBox's selected index to -1 via cmbProductName.setSelectedItem(-1).
    sh4rif likes this.

  3. #3
    sh4rif is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Jcombobox an empty row

    Thanks alot Fubarable.... that works i actuall used setSelectedIndex(-1)... and that is what i want ... thanks a ton...
    Fubarable likes this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. mysql empty set
    By cucucur in forum JDBC
    Replies: 0
    Last Post: 04-25-2011, 03:01 PM
  2. Empty JFrame
    By Wednesday Bass in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2011, 07:30 PM
  3. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  4. empty arrays.
    By circuspeanuts in forum New To Java
    Replies: 11
    Last Post: 04-06-2009, 07:08 PM
  5. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 07:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •