In the actionPerformed
for the previousButton
and nextButton
you need to check what the current bookIndex
is before you decrement or increment, respectively. In the case of previousButton
, if the current bookIndex == 0
, set the bookIndex to inventoryBook.length-1
instead of decrementing. For the nextButton
, if the bookIndex == inventoryBook.length-1
, set bookIndex
to 0
instead of incrementing. So for nextButton
:
JButton nextButton = new JButton("Next");
buttonPanel.add(nextButton);
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(bookIndex == inventoryBook.length - 1) {
bookIndex = 0;
} else {
bookIndex = bookIndex + 1;
}
prepareDisplay(inventoryBook[bookIndex], textArea);
}
});