0

i have been trying to generate an array of check boxes and dynamic click handler for them but the handler is not working.

Any suggestion will be most welcomed. Thanks in Advance for the time.

private void addButtonListener() {      
    goButton.addClickHandler(new ClickHandler() {           
        @SuppressWarnings("rawtypes")
        @Override
        public void onClick(ClickEvent arg0) {              
            String strQuery="Select BRANCH_NAME from SAMPLE_ACC_BRANCH where GL_CODE='"+gll_textfield.getText().trim()+"'";
            HibernateImplUtils.getSearchResult(strQuery, new AsyncCallback() {                  
                private int i;
                @Override
                public void onFailure(Throwable arg0) 
                {arg0.printStackTrace();}
                @Override
                public void onSuccess(Object arg0) {                        
                    System.err.println("Inside Success");
                    List branchNameList=(List) arg0;
                    System.err.println("Branch List:::"+branchNameList);                        
                        for(i=0;i<branchNameList.size();i++){
                            checkbox[i]=new CheckBox((String) branchNameList.get(i));                               
                            vpanel.add(checkbox[i]);
                            checkbox[i].addClickHandler(new ClickHandler() {                                    
                                @Override
                                public void onClick(ClickEvent arg0) {                                      
                                    if(checkbox[i].getValue()){
                                        System.out.println("NAME::::"+checkbox[i].getText());
                                    }
                                    System.out.println("Selected check box ::::"+checkbox[i].getText());
                                }
                            });                                             
                    }                       

                }
            });             

        }
    });

}   
Akash Narayan
  • 300
  • 3
  • 14
  • Saying "it's not working" isn't helpful. Please state exactly what what the problem is, don't make potential helpers have to try and figure it out. – Synchro Jul 07 '14 at 15:52
  • ValueChangeHandler is probably better way to go. – salk31 Jul 07 '14 at 20:48
  • @Synchro Dude, extremely sorry for not stating for problem specifically. Actually the click handler is reading the last index value only each time if any check box is being clicked. – Akash Narayan Jul 08 '14 at 05:06
  • @salk31 :- Dude it's having the same effect :( !!! – Akash Narayan Jul 08 '14 at 06:06
  • However if am trying to print the `code`checkbox[i].getText() its throwing a null pointer. – Akash Narayan Jul 08 '14 at 06:11
  • BTW: I guess it's a very unsecure way to store SQL statements in the client of your application.The SQL Statement can be modified and be used to mess around with your database. – Akkusativobjekt Jul 08 '14 at 06:29
  • @Akkusativobjekt :- You got a point !! +1 – Akash Narayan Jul 08 '14 at 10:22
  • Ah. "int i" is an odd place. You should keep it in the scope of the "for" loop but make it "final" so that the inner class can reference it. ie for (final int i ...) – salk31 Jul 08 '14 at 10:24

2 Answers2

1

The scope of "i" is dodgy. Quickest fix would be to make a "final" copy for your event handler. e.g. "final int i2 = i"

The inner class probably wants the index value during its creation.

I'd be tempted to do use a final reference to the checkbox you create or the reference passed to the event handler (that way you could also use a single instance).

(Modified)

salk31
  • 995
  • 2
  • 8
  • 13
  • Tried as you said but the compound assignment operator won't take it. It seems the dynamic handler can't be created this way, inside "for loop". – Akash Narayan Jul 08 '14 at 11:26
  • Sorry, yes. I'm normally iterating over a collection where it is allowed. Either use a final reference to the checkbox or make a final copy of the int e.g. "final int i2 = i; – salk31 Jul 08 '14 at 12:24
  • Bingo, you got dis one !! :) – Akash Narayan Jul 08 '14 at 13:08
0
    final int i2=i;                         
    checkbox[i2].addClickHandler(new ClickHandler() {                                    
      @Override
        public void onClick(ClickEvent arg0) {
        if(checkbox[i2].getValue()){                                           
           System.out.println("NAME::::"+checkbox[i2].getText());
                }

              }
          }); 
Akash Narayan
  • 300
  • 3
  • 14