Reputation: 21
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:
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);
}
Upvotes: 0
Views: 1066
Reputation: 5019
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
Upvotes: 1
Reputation: 2172
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
Upvotes: 0
Reputation: 285403
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();
Upvotes: 0