开发者

Update text on a buttonfield when it is clicked

开发者 https://www.devze.com 2023-02-12 17:58 出处:网络
I\'m trying to set the text on a button when it is clicked. I\'m initialising a BigVector which will update the text of the button with its value. I\'m using a counter value to determine wihcih BigVec

I'm trying to set the text on a button when it is clicked. I'm initialising a BigVector which will update the text of the button with its value. I'm using a counter value to determine wihcih BigVector value should be selected. The problem is, the below code is expecting the counter value to be final.

A better methodology for updating the text on开发者_JAVA技巧 a Field when it is clicked is most welcome.

Here is my code -

    final BigVector bigStringVectorA = new BigVector();
    bigStringVectorA.addElement("A Test answer 1");
    bigStringVectorA.addElement("A Test answer 2");
    bigStringVectorA.addElement("A Test answer 3");

    aAnswerOptionButton.setChangeListener(new FieldChangeListener() {  
         public void fieldChanged(Field field, int context) {  
            aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
        }
   });

Thanks


You can make the counter an instance variable, either in the outer class or in the anonymous FieldChangeListener:

aAnswerOptionButton.setChangeListener(new FieldChangeListener() {
  private int counter = 0;
  public void fieldChanged(Field field, int context) {
    counter++;
    if (counter > bigStringVectorA.size()) {
      counter = 0;
    }
    aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
  }
});


You can try to call the invalidate() method of that field and that should force the redraw of that button.

aAnswerOptionButton.setChangeListener(new FieldChangeListener() {  
         public void fieldChanged(Field field, int context) {  
            aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
            aAnswerOptionButton.invalidate();
        }
   });
0

精彩评论

暂无评论...
验证码 换一张
取 消