Results 1 to 4 of 4
  1. #1
    cool is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default Problem in adding arrayList elements

    Hi,
    I have some check boxes to add selected days of a month to an arrayList,
    when i select the element it gets added, but when next time i add other elements, it shows arrayList empty and add only the selected element.

    // this is my class to implement actionListener
    public void actionPerformed(ActionEvent e)
    {
    JCheckBox obj = (JCheckBox) e.getSource();
    String name= e.getActionCommand(); // gives name of the checkbox

    if(obj.isSelected()== true)
    {
    mycon.addMonthItem(name);

    }
    }



    // This function is my class
    public void addMonthItem(String item)
    {
    System.out.println("list is "+monthList);
    monthList.add(item); // monthList is my arrayList
    System.out.println("added item, "+item+" new list is "+monthList);

    }


    when i select checkbox having value 1,2,3
    // initially list is empty
    i get output as
    ***********

    list is []
    added item 1, new list is [1]
    list is []
    added item 2, new list is [2]
    list is []
    added item 3, new list is [3]


    can somebody help????

  2. #2
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Does any other part of the code use the list, except for using the method get(int)?
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    i couldn't find any problems in the code you posted but perhaps this example may help you.

  4. #4
    cool is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    Thanks Hibernate, thanks j2me64,

    I implemented the action listener in same class, its working fine now....

Similar Threads

  1. Adding the Elements of a 2D Array
    By RMcLuckie45 in forum New To Java
    Replies: 0
    Last Post: 11-07-2010, 11:04 PM
  2. Cannot swap elements in ArrayList
    By glchau in forum New To Java
    Replies: 4
    Last Post: 05-30-2010, 08:03 AM
  3. Replies: 5
    Last Post: 01-18-2010, 07:00 AM
  4. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:24 AM
  5. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 03:55 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
  •