1

i am adding around 10 check boxes in a JFrame , These are added in for loop being iterted on Array , code goes like this

    JFrame f=new JFrame("hello ");
    FlowLayout fl= new FlowLayout();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300,300);
    f.setVisible(true);
    f.setLayout(fl);

    for (int i=0 ; i<10; i++)
    {

        b[i]=new JCheckBox();
        b[i].setVisible(true);
        b[i].addItemListener(this);
        f.add(b[i]);

    }  /// and so on . 

My Question is when i implement ItemListener will i have to access each of the CheckBox like This
if(b[1].isSelected()) , if(b[2].isSelected()) or there is anyother technique can be used like a loop or something like This .. Thanks in advance

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sikander
  • 2,799
  • 12
  • 48
  • 100
  • Two thoughts come to mind. You can use the loop idea, or you can maintain a list of those checkboxes that are selected by adding/removing the event source when the item listener is triggered – MadProgrammer Oct 20 '12 at 19:18
  • 1
    If you are going to have a lot of JCheckBoxes, consider instead using a JTable holding a column of Booleans (which render as checkboxes). – Hovercraft Full Of Eels Oct 20 '12 at 19:27

2 Answers2

5

The ItemListener will be invoked with an instance of ItemEvent. This event has a source (EventObject#getSource()), which will be the component which triggered the event, e.g. your JCheckBox.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
sarcan
  • 3,145
  • 19
  • 22
  • so it means if i use 100 jcheckBoxes Jbuttons etc There is going to be 100 if , else or switch Conditions that will be too heavy code , And i have done that , i wanted something like iterating a loop or some similar solution that gets the seleted item itself instead of going for b[1] , b[2] etc .. – Sikander Oct 20 '12 at 19:15
  • @Sikander @sarcans method allows for you to gain access to the Object/CheckBox through `EventObject#getSource()` you will have to explictly cast is to a `JCheckBox` though like: `JCheckBox cb=(JCheckBox)e.getSource();` and then you can simply `cb.isSelected()` or whatever – David Kroukamp Oct 20 '12 at 19:18
  • i dont need to cast if i do it like this Object obj=e.getsource(); , but my question if i iterate a loop and instead of accessing each one separate is it possible to access them same way as they are added ... They are added in loop and [i] assigns them values , now can we access required one using arrays of loops if i m not wrong ? – Sikander Oct 20 '12 at 19:22
  • Of course you can iterate over your array in a for-loop, or rather a for-each loop. `for (JCheckBox c : b) if (c.isSelected()) ...`. Question is why you'd want to do that. – sarcan Oct 20 '12 at 19:26
  • @Sikander: **you** should be the one writing the code. We'll be more than glad to help you, but please don't ask others to do your homework or write your code for you. That's your responsibility. -1 vote for your comment, to be removed when/if you retract this. – Hovercraft Full Of Eels Oct 20 '12 at 19:32
  • Sorry i will, Ok plz guide me about it – Sikander Oct 20 '12 at 19:36
1

so it means if i use 100 jcheckBoxes Jbuttons etc There is going to be 100 if , else or switch Conditions that will be too heavy code , And i have done that , i wanted something like iterating a loop or some similar solution that gets the seleted item itself instead of going for b1 , b2 etc

you can

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319