CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    427

    JComboBox AddItem

    For the purpose of discussion and learning for a newbi like me

    I have a JComboBox in my window , and will fill it after retrieving some values from the Database. I use "addItem" to insert each data (String ) to my ComboBox and it works fine.

    The problem was when there is no data retrieved. In this case I just use the same "addItem" function to insert an item to the combobox.
    Code:
    String noStr  = new String("DB Empty");
    this.comboDataList.addItem(noStr);
    The code above, will return an Null Exception;

    I tried using "insertItemAt()", and it will insert the item to the comboBox but when I tried to select the Item, it will give the same NULL exception.

    I found this solution using the "setModel"

    Code:
     
    String [] testStr = new String[1];
     
    for(int i=0; i < 1; i++ )
    {
    	testStr[i] = new String("DB Empty");
    }
     
    this.comboDataList.setModel(new DefaultComboBoxModel(testStr));
    This was the solution that works among all the tutorials I found. I probably don't need to loop it since I only have one data to add anyway.

    Is there some kind of problem with addItem? Any Idea.

    Im using NetBeans IDE 5.5.1 and JDK 1.6
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: JComboBox AddItem

    "The code above, will return an Null Exception"
    Is comboDataList null?
    You need to create an object before you can use it.
    Can you show the full text of the error message?
    Norm

  3. #3
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    427

    Re: JComboBox AddItem

    @Norm, the combobox object was created during the initialization and is used during insertion of items to it.


    Right , Ive noticed that the behavior of addItems is inconsistent.

    this is how the original code looks like
    Code:
     
    	ArrayList<ParameterBean> beanList= new ArrayList<ParamaterBean>();
    	beanList= getListNames();  //  this will return all names retrieve from the DB
     
       if( beanList.size() > 0 )
       {
    		   //get each item from the list and insert to the comboBox using addItems
    	}
       else
       {
    	   // set a temporary value to combobox
    	  String strTemp = new String("DB Is Empty " );
    	  this.comboDataList.addItem(strTemp)
     
       }
    the else part will cause an exception. In "ParameterBean" , I have a getName() function that returns a String. So for each Parameter, I insert the String value using addItem to the ComboBox and no Problems. But I explicitly insert a String like the one in ELSE part, it will cause and exception.
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: JComboBox AddItem

    Can you show the full text of the error message?
    Norm

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JComboBox AddItem

    @Norm, the combobox object was created during the initialization and is used during insertion of items to it.
    When you create the JComboBox are you suppling a ComboBoxModel and if so is it mutable?

    As well as the full error message requested twice now by Norm, please also show the insertion code that works.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured