1

These two buttons are cycling through an array and when pressed increment to many times and depending on what the maximum index is. I thought it might be because (for backwards a least I have "i set as maximum index" but changing to current index stops the button functioning all together.

btnprev.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for (int i = dataArrayMaxIndex; i >= 0; i-- ) {
                System.out.println("backwards");
                dataArrayCurrentIndex = i;
                websitetxt.setText(dataArray[i].getWebsitename());
                usernametxt.setText(dataArray[i].getUsername());
                passwordtxt.setText(dataArray[i].getPassword());
                notestxt.setText(dataArray[i].getNotes());
            }
        }
    });

    btnnext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i<dataArrayMaxIndex; i++) {
                System.out.println("foward");
                dataArrayCurrentIndex = i;
                websitetxt.setText(dataArray[i].getWebsitename());
                usernametxt.setText(dataArray[i].getUsername());
                passwordtxt.setText(dataArray[i].getPassword());
                notestxt.setText(dataArray[i].getNotes());
            }
        }
    });

I am unsure as to fixing this problem and could use some help and suggestions. I feel it would be more helpful for myself to not be given the answer but to have some constructive criticism to lead myself to it, that being said feel free to post an a straight, working answer if that's your thing.

1 Answer 1

1

I think that your for loops don't belong in this code as you'll loop all the way to the end or all the way to the beginning with each button press. Instead, simply increment or decrement an index variable and use that index. I assume that you'll be using the dataArrayCurrentIndex for this functionality.

i.e.,

btnprev.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      dataArrayCurrentIndex--;

      // if you want to do a cyclic loop of the data
      if (dataArrayCurrentIndex < 0) {
        dataArrayCurrentIndex= maxIndex - 1;
      }

      System.out.println("backwards");
      websitetxt.setText(dataArray[dataArrayCurrentIndex].getWebsitename());
      usernametxt.setText(dataArray[dataArrayCurrentIndex].getUsername());
      passwordtxt.setText(dataArray[dataArrayCurrentIndex].getPassword());
      notestxt.setText(dataArray[dataArrayCurrentIndex].getNotes());
    }
});

btnnext.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        dataArrayCurrentIndex++;

        // if you want to do a cyclic loop of the data
        if (dataArrayCurrentIndex >= maxIndex) {
            dataArrayCurrentIndex = 0;
        }

        // etc...
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Just tried it works like a charm, thanks heaps! I guess the way I was going about doing wasn't the most elegant (or working!) solution to the problem.
@Beid: I can see where you're coming from. You're thinking that the button press should cause the data to loop forward, and thus used a for loop for the job, but that's not what is happening. When the button is pressed, the data should instead step forward or backward, which is what the increment/decrement operations will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.