Results 1 to 13 of 13
  1. #1
    DanielS is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default [SOLVED] ArrayList to listbox

    I have an arraylist (of strings) that I want to transfer into the elements of a listbox. I cant seem to figure out how to do this. I am using netbeans and didnt create the listbox myself.

    Java Code:
    ArrayList arr = new ArrayList();
    // populate arraylist with strings
    
    listBox.SetListData(arr); // Wont work
    
    listBox.SetListData(arr.ToString()); // Wont work either

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

    Default

    sorry, but what is a listBox?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You cannot add ArrayList into a list box, only Object[]. So you have to convert the ArrayList into Object[]

    Java Code:
    listBox.setListData(arr.toArray());
    See what happen on the above line of code.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    sorry, but what is a listBox?
    I think he's talking about List. In early NetBeans it's tag as ListBox

  5. #5
    DanielS is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    You cannot add ArrayList into a list box, only Object[]. So you have to convert the ArrayList into Object[]

    Java Code:
    listBox.setListData(arr.toArray());
    See what happen on the above line of code.
    Thanks for the reply... I actually tried that, it says

    Java Code:
    Cannot Find Symbol
    symbol: method SetListData(java.lang.Object[])
    location: class javax.swing.JList
    This is what is confusing me. SetListData() takes an Object[] as an argument but this fails.

  6. #6
    DanielS is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    sorry, but what is a listBox?
    Sorry, I come from the C++ world. I should have put JList.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by DanielS View Post
    Thanks for the reply... I actually tried that, it says

    Java Code:
    Cannot Find Symbol
    symbol: method SetListData(java.lang.Object[])
    location: class javax.swing.JList
    This is what is confusing me. SetListData() takes an Object[] as an argument but this fails.
    I don't think so, it's should work. Here is a quick code segment.

    Java Code:
        ArrayList<String> arr = new ArrayList<String>();
        arr.add("one");
        arr.add("two");
        arr.add("Three");
        
        jList1.setListData(arr.toArray());
    Nothing much to do here.

  8. #8
    DanielS is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    I don't think so, it's should work. Here is a quick code segment.

    Java Code:
        ArrayList<String> arr = new ArrayList<String>();
        arr.add("one");
        arr.add("two");
        arr.add("Three");
        
        jList1.setListData(arr.toArray());
    Nothing much to do here.
    Here is my code...

    Java Code:
    private void GetEntryList()
    {
        int ch;
        Dir = "H:\\Entries\\All.txt";
        FileReader fReader = null;
        String Title = "";
        ArrayList<String> arrEntries = new ArrayList<String>();
        try 
        {
            fReader = new FileReader(Dir);
        } catch (FileNotFoundException e) {e.printStackTrace();}
        do
        {
            do
            {
                try
                {
                    ch = fReader.read();
                    if ((char)ch == '~')
                    {
                        do
                        {
                            Title += (char)ch;
                            ch = fReader.read();
                        } while ((char)ch != '{');
                        Title = Title.replace("~", "");
                        arrEntries.add(Title);
                        Title = "";
                    }
                } catch(IOException e){e.printStackTrace();}
            } while(true);
        } while(true);
    
        lstEntries.SetListData(arrEntries.toArray()); // <--- Error Here
    }
    Is it the error I mentioned above.

    If this should work then maybe its a problem in NetBeans (v6.5).


    EDIT: I know they are infinite loops... This method is in progress.
    Last edited by DanielS; 11-30-2008 at 04:43 AM.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Can you show up the declaration of lstEntries?

    I don't think this is an issue on NetBeans 6.5. I'm working on that version too, but no issues at all.

  10. #10
    DanielS is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Can you show up the declaration of lstEntries?

    I don't think this is an issue on NetBeans 6.5. I'm working on that version too, but no issues at all.
    I added the JList via the design editor.

    Java Code:
        private javax.swing.JList lstEntries;

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Java is case sensitive. Method names, by convention, start with a lowercase letter.

    ergo, JList doesn't have a method SetListData.

    db

  12. #12
    DanielS is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Java is case sensitive. Method names, by convention, start with a lowercase letter.

    ergo, JList doesn't have a method SetListData.

    db
    I made the S in 'set' lowercase and it worked. Damn!

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. To highlight multiple selected values in listbox
    By swapnanair in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-06-2008, 11:17 AM
  2. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  3. Listbox Add/Delete
    By Rageagainst20 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 04:49 PM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 08:48 PM
  5. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 07:30 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
  •