0

I have an issue with Java Arraylist: when I click the previous button it won't get the index of the last element.

Note: InfoStudent is a separate class that contains all student info like id, name and email; and arraylist student contains the info of the new students. I am not sure if my problem is the index like student.get (get the current student's id and minus 1 since index starts 0). My screenshot:

screenshot

My code:

JButton btnFirst = new JButton("First");
    btnFirst.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textFieldId.setText(Integer.toString(student.get(0).getId()));
            textFieldName.setText(student.get(0).getName());
            txtEmailbox.setText(student.get(0).getEmail());
        }
    });
    btnFirst.setBounds(10, 215, 89, 22);
    frame.getContentPane().add(btnFirst);

    JButton btnPrev = new JButton("Prev");
    btnPrev.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            current = Integer.parseInt(textFieldId.getText());
            if(current== student.size()){
                current= 0;
                }
            else {
                current = current-1;
            }
                textFieldId.setText(Integer.toString(student.get(current).getId()));
                textFieldName.setText(student.get(current).getName());
                txtEmailbox.setText(student.get(current).getEmail());

            //student.get(current).getId()  is not working 

        }
    });
    btnPrev.setBounds(122, 215, 89, 22);
    frame.getContentPane().add(btnPrev);

    JButton btnNext = new JButton("Next");
    btnNext.setBounds(230, 215, 89, 23);
    frame.getContentPane().add(btnNext);

    JButton btnLast = new JButton("Last");
    btnLast.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textFieldId.setText(Integer.toString(student.get(student.size()-1).getId()));
            textFieldName.setText(student.get(student.size()-1).getName());
            txtEmailbox.setText(student.get(student.size()-1).getEmail());
        }
    });
    btnLast.setBounds(329, 215, 89, 23);
    frame.getContentPane().add(btnLast);
}
2
  • What if current=0 (current = current -1;)? You should also address that case. Commented Mar 13, 2015 at 22:24
  • There is no ArrayList or even a List interface in the code you posted. Commented Mar 13, 2015 at 22:26

3 Answers 3

1

Current variable is the student id and not the index in the array to solve it the best solution is to add variable that save your current index in the student array

Sign up to request clarification or add additional context in comments.

Comments

0

Change

        if(current== student.size()){
            current= 0;
            }
        else {
            current = current-1;
        }

To

        if(current < 0 || current >= student.size()){
            current = 0;
            }
        else {
            //Do Not Change the value since it is valid
        }

And you can use a field to store current index value

2 Comments

how about when i went to go to next index
you need to check if the index is valid first just like the in the example above
0

First off, don't do this, btnFirst.setBounds(10, 215, 89, 22);. You should avoid use of null layout and use of setBounds(...) for component placement as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain.

Next of all, store your current index in a field, and simply change its value as needed.

e.g., in next:

currentIndex++;  // increment it
currentIndex %= myCollection.size(); // mod it so that you don't go out of bounds

and in previous, something like:

currentIndex--;
currentIndex += myCollection.size(); // mod doesn't work well w/ neg numbers
currentIndex %= myCollection.size();

Comments

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.