Results 1 to 9 of 9
  1. #1
    fm3c2007 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Combobox dynamic load using String[]

    Hi,

    I'm using NetBeans 6.9.1 . And in a form i place a combobox and a Button. I would like to click the button and fill the combobox with values from a String [].

    I tried:

    Java Code:
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
     
            String[] LotOfItems = {"item1", "item2"};
    
         jComboBox1.removeAllItems();
        jComboBox1.addItem(LotOfItems);
    
        }
    but all i get in the combobox text is "[Ljava.lang.String;@1050169".

    Thanks

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are adding an array to the first index of the combo box. Anytime you see that weird gobbledegook you are seeing the default toString message.

    You can loop through the array and add each item.

    Also, there may be something in the combo box class that let's you add an array, check the API.

  3. #3
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    The code posted adds the array as an item...instead, loop over the values in the array and add them individually
    Edit: too late once again

  4. #4
    fm3c2007 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    There is nothing in the API...only Binding elements from a database/Table.

    I'm trying to avoid writing unnecessary code. I can't believe that there isn't a method that takes an array and fills the combobox.

    I found:

    public void addItem(Object anObject)

    Adds an item to the item list. This method works only if the JComboBox uses a mutable data model

    Or:

    public void addItem(Object anObject)
    Adds an item to the item list. This method works only if the JComboBox uses a mutable data model

    Thanks

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    String[] LotOfItems = {"item1", "item2"};
    Use proper variable names. The first character should NOT be upper cased.

    You can just replace the model:

    Java Code:
    comboBox.setModel( new DefaultComboBoxModel( items ) );

  6. #6
    fm3c2007 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks camickr. My problem is solved. :)

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please mark your thread solved with the thread tools at the top of the page if you are done.

    Also, adding with a loop only adds 2 lines of code
    Java Code:
    for(int i = 0; i < x; i++){
      comboBox.addItem(x[i]);
    }
    If you are trying to optimize away 2 lines of code you are over, and prematurely optimizing.

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Removing/adding items causes extra work. When you clear all the items an event is generated to tell the combo box the model has changed. Then every time you add an individual item another event is fired, again telling the combo box the model has changed. Is this overhead significant? No, but whats wrong with simple replacing the model so that only a single event is generated? The added bonus is that its only a single line of code.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I didn't mean to contest your point, just clarifying mine. Perhaps I have learned something here as well.

Similar Threads

  1. Dynamic String Array?
    By ladykrimson in forum New To Java
    Replies: 4
    Last Post: 10-13-2010, 04:37 AM
  2. String array from file to ComboBox
    By cselic in forum AWT / Swing
    Replies: 3
    Last Post: 05-06-2010, 05:29 PM
  3. How to create dynamic string object???
    By Stephen Douglas in forum New To Java
    Replies: 8
    Last Post: 04-12-2010, 02:35 AM
  4. [SOLVED] Dynamic String -&gt; Date Handeling
    By vagetoanima in forum Advanced Java
    Replies: 2
    Last Post: 03-31-2009, 06:43 PM
  5. Add data to combobox in page load
    By hussainzim in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 05-15-2008, 12:57 PM

Tags for this Thread

Posting Permissions

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