TMagnetB
TMagnetB

Reputation: 35

Add ArrayList into Listbox Java

So I'm trying to add an ArrayList to a Jlist, but I'm getting errors. Here is the code

private void BReActionPerformed(java.awt.event.ActionEvent evt) {                                    
            NameList NL = new NameList();
            NL.name.add("a");
            NL.name.add("b");
            NL.name.add("c");
            NL.name.add("d");
            NL.name.add("e");
            for(int i=0; i < NL.name.size(); i++)
            {
                LOut.add(NL.name.get(i));
            }
}

Upvotes: 0

Views: 620

Answers (2)

camickr
camickr

Reputation: 324147

LOut.add(NL.name.get(i));
  1. First of all learn an use Java naming standards. Variable names do NOT start with an upper case character.

  2. You don't "add" items directly to the JList. You add items to the ListModel. Read the section from the Swing tutorial on How to Use Lists. The ListDemo code is a working example that shows how to add items to the DefaultListModel. The code also shows how to use proper variable names.

Upvotes: 1

Mrunmay Deswandikar
Mrunmay Deswandikar

Reputation: 189

Just remove the name field and try like below,

for(int i=0; i < NL.name.size(); i++)
{
   LOut.add(NL.get(i));
}

Upvotes: 0

Related Questions