How to initialize the actions for array of buttons using for loop

0 Basma Mohamed · April 16, 2015
I am trying to add the actionlisteners for an array of buttons by calling a method 
using for loop but it didn't work it showed me no outputs neither any errors just" BUILD SUCCESSFUL" with nothing appearing 
when I separated each element of the array  it worked successfully 
here is the method when it was built successfully ( I created a class for frame and class for panel named it cal which contains the buttons array and made it public just to access it for now easily then made an object of the panel in the frame class )

public void actionsnums ()
  {
         cal.arr[0].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (!"0".equals(reader.getText())
                reader.setText(reader.getText()+1);
            else
                reader.setText(""+1);;
           }
       });
       cal.arr[1].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (!"0".equals(reader.getText())
                reader.setText(reader.getText()+2);
            else
                reader.setText(""+2);;
           }
       });cal.arr[2].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (reader.getText()!="0")
                reader.setText(reader.getText()+3);
            else
                reader.setText(""+3);;
           }
       });cal.arr[4].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (!"0".equals(reader.getText())
                reader.setText(reader.getText()+4);
            else
                reader.setText(""+4);;
           }
       });cal.arr[5].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (!"0".equals(reader.getText())
                reader.setText(reader.getText()+5);
            else
                reader.setText(""+5);;
           }
       });cal.arr[6].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (!"0".equals(reader.getText())
                reader.setText(reader.getText()+6);
            else
                reader.setText(""+6);;
           }
       });cal.arr[8].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (!"0".equals(reader.getText())
                reader.setText(reader.getText()+7);
            else
                reader.setText(""+7);
           }
       });cal.arr[9].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (!"0".equals(reader.getText())
                reader.setText(reader.getText()+8);
            else
                reader.setText(""+8);;
           }
       });cal.arr[10].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (!"0".equals(reader.getText())
                reader.setText(reader.getText()+9);
            else
                reader.setText(""+9);}
       });
       cal.arr[13].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){
            if (reader.getText()!="0")
                reader.setText(reader.getText()+0);
            else
                reader.setText(""+0);                
           }
       });
  }


here when it didn't work
public void actionsnums ()
  {
      for (int i=0;i<=2;i++)
      {
          int y=i++;
           cal.arr.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (reader.getText()!="0")
                reader.setText(reader.getText()+y);
            else
                reader.setText(""+y);;
           }
       });
      }
      for (int i=4;i<7;i++)
      {
          int y=i;
           cal.arr.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (reader.getText()!="0")
                reader.setText(reader.getText()+y);
            else
                reader.setText(""+y);;
           }
       });}
        for (int i=8;i<11;i++)
      {
          int y=i--;
           cal.arr.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){

            if (reader.getText()!="0")
                reader.setText(reader.getText()+y);
            else
                reader.setText(""+y);;
           }
       });
      }
  }


Post a Reply

Replies

Oldest  Newest  Rating
0 steven kitchener · April 16, 2015
Are you sure you received no errors in the code using loops? From an initial scan over it I would believe that you should be getting errors when trying to use the variable "y" inside the actionPerformed method. It is my understanding that you need to use final variables inside the actionPerformed method.

Youve also failed to mention what "reader" is and what it is you are actually trying to achieve here.

Provide this information and we'll be able to help you further 

Regards,

Steven
0 Kuroodo Ditory · April 16, 2015
I may be completely wrong, or this may not be the solution, but why do you do
if (!"0".equals(reader.getText())

Why not do this instead:

if(!reader.getText().equals("0"))


Then here, you do

if (reader.getText()!="0")


Shouldn't that also be

if(!reader.getText().equals("0"))


Everytime I try to compare Strings, I get an error because I don't use the .equals() method.
  • 1

Java / Android Development

107,183 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator