0

I have a function that returns a list of selected items in my application and I got 4 JCheckBoxes. Every one of them has this code to fill it from a List of objects that I have:

List<Answer> list = new ArrayList<>();
...
jCheckBox1.setText(list.get(0).toString());
jCheckbox1.setActionCommand(String.ValueOf(list.get(0).getid());

It's working but the problem is with my itemListener in which I use a list of selected items.

List<Answer> selected = new ArrayList<>();

My itemListener:

ItemListener item = new ItemListener(){
     @Override
     public void itemStateChanged(ItemEvent e) {
        if (e.getSource() instanceof JCheckBox){
             JCheckBox mycheck = (JCheckBox) e.getSource();

             if (mycheck.isSelected()){                
               selected.add(Integer.parseInt(mycheck.getActionCommand()));
             } else {
               selected.remove(Integer.parseInt(mycheck.getActionCommand()));
             }
};

I have a function that returns size in order to get the problem but apparently the problem is when I unselect the JCheckBox. For example: if I Select 3, the size's function returns 3, but if I unselect one of them and I execute the function, the error comes.

Thanks for you help.

error log:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
    at java.util.ArrayList.rangeCheck(ArrayList.java:657)
    at java.util.ArrayList.remove(ArrayList.java:496)
    at frames.QuizFrame$1.itemStateChanged(QuizFrame.java:128)
    at javax.swing.AbstractButton.fireItemStateChanged(AbstractButton.java:2050)
    at javax.swing.AbstractButton$Handler.itemStateChanged(AbstractButton.java:2355)
    at javax.swing.DefaultButtonModel.fireItemStateChanged(DefaultButtonModel.java:455)
    at javax.swing.JToggleButton$ToggleButtonModel.setSelected(JToggleButton.java:272)
    at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:289)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Dev web
  • 81
  • 1
  • 13
  • Include the stack trace with the error that you are getting. Also notice that something is totally wrong in your code, you have declared `selected` as a List and you are trying to add Integers to it. Please edit the question according with your actual code. – RubioRic May 11 '18 at 10:00
  • 1
    I think you want to remove item with index 3 from your list. But it doesn't exist: `selected.remove(...)` You want to remove object from ArrayList not something under index 3. – Planck Constant May 11 '18 at 10:04
  • @RubioRic I saw it, I've changed it but that's not the problem – Dev web May 11 '18 at 10:08
  • @Uata when I select the first and second choices for example in the first time, I have the right selected: {1,2} but if I unselect one, I have the error in this case – Dev web May 11 '18 at 10:10
  • I think that @Uata is right. Please take a look here https://stackoverflow.com/questions/4534146/properly-removing-an-integer-from-a-listinteger You're using remove(int) instead of remove(Object) there. Your results may depend on the selected order. Try replacing Integer.parseInt by Integer.valueOf in your method that remove from the selected list. (Also in your add method, if you please, it is not relevant in this case) – RubioRic May 11 '18 at 10:24
  • @RubioRic still have the same problem, the itemListener doesn't work when I unselect, if I select or unselect, it does the same thing, he does only the add, never remove from the list. – Dev web May 11 '18 at 10:28
  • I'm not sure what you are testing right know. Your previous example match perfectly with the situation described and pointed by @Uata. If you got {1,2} selected, and deselect 2, you try to remove the item with index 2 in the list. And List is zero based. There is no item in position 2. And that's the reason that you get an IndexOutOfBoundsException – RubioRic May 11 '18 at 10:33
  • @RubioRic 1st step: I select first and third choices. 2nd step: click the button to trigger the function. The answer is [1,3] 3rd step: I unselected 3 for example the result is [1,3,3] and I want to get [1] because I unselected, he tool it like if I selected it twice – Dev web May 11 '18 at 10:36
  • Use valueOf in the add function. But be aware that it will only work for a range of Integers https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf(int) – RubioRic May 11 '18 at 10:41
  • 1
    @RubioRic thanks a lot, it worked. – Dev web May 11 '18 at 10:54
  • Possible duplicate of [Properly removing an Integer from a List](https://stackoverflow.com/questions/4534146/properly-removing-an-integer-from-a-listinteger) – RubioRic May 11 '18 at 10:55

0 Answers0