Making a "next" button to move through a array.
I have been stressing over this for a few days now and just can not seem to get it to work. Basically I have an array and a GUI, the GUI displays the information from the first index in the array. I have created a button that when pushed DOES cycle to the next index but then will no longer work. I also need to create a button that cycles backwards and then one that jumps to the first and last index's. Of course I know how to make the buttons just not the rest of the code.
I really hope some one could give me some pointers with this
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
publicclass InventoryProgramGUIextends JFrameimplements ActionListener
{
private Container content = getContentPane();
private JPanel panel;
private BluRayGUI movieArray[] =new BluRayGUI[10];
private JComboBox jcbMovieList;
public InventoryProgramGUI()
{
initializeData();// call method initializeData
content.setLayout(null);
panel =new JPanel();
panel.setLayout(null);
buildProductPanel(0);// Call method to display info in GUI
panel.setBounds(0, 50, 480, 170);
content.add(panel);
//jcbMovieList = new JComboBox();
//for(int i = 1; i<=movieArray.length; i++)
//jcbMovieList.addItem(""+1);
JButton button =new JButton("Next");// Make the next button
button.setBounds(200, 25, 90, 22);
button.addActionListener(this);
content.add(button);
button =new JButton("Exit");// Make the exit button
button.setBounds(370, 25, 90, 22);
button.addActionListener(this);
content.add(button);
double sum = 0;
for(int i = 0; i<movieArray.length; i++)
{
sum += movieArray[i].calculateTotal();
}
int temp = (int)sum*100;
sum = temp/100;
JLabel label =new JLabel("Value: $"+sum+"");
label.setForeground(Color.blue);
label.setBounds(160, 230, 300, 30);
content.add(label);
}
publicvoid buildProductPanel(int id)
{
Border border = BorderFactory.createEtchedBorder();
panel.removeAll();
JLabel label =new JLabel("Movie Title: ", JLabel.RIGHT);// Create label
label.setBounds(5, 20, 150, 22);
panel.add(label);
label =new JLabel ("Sku Number: ", JLabel.RIGHT);// Create label
label.setBounds(0, 50, 150, 22);
panel.add(label);
label =new JLabel ("Units in Stock: ", JLabel.RIGHT);// Create label
label.setBounds(0, 80, 150, 22);
panel.add(label);
label =new JLabel ("Item Price: ", JLabel.RIGHT);// Create label
label.setBounds(0, 110, 150, 22);
panel.add(label);
label =new JLabel ("Blu-Ray Disc: ", JLabel.RIGHT);
label.setBounds(0, 140, 150, 22);
panel.add(label);
label =new JLabel (" "+movieArray[id].getName());// Display Name
label.setBorder(border);
label.setForeground(Color.red);
label.setBounds(160, 20, 300, 22);
panel.add(label);
label =new JLabel (" "+movieArray[id].getSku());// Display Sku
label.setBorder(border);
label.setForeground(Color.red);
label.setBounds(160, 50, 300, 22);
panel.add(label);
label =new JLabel (" "+movieArray[id].getAmount());// Display Amount
label.setBorder(border);
label.setForeground(Color.red);
label.setBounds(160, 80, 300, 22);
panel.add(label);
label =new JLabel (" "+movieArray[id].getPrice());// Display price
label.setBorder(border);
label.setForeground(Color.red);
label.setBounds(160, 110, 300, 22);
panel.add(label);
label =new JLabel (" "+movieArray[id].getMediaType());//Display type
label.setBorder(border);
label.setForeground(Color.red);
label.setBounds(160, 140, 300, 22);
panel.add(label);
}
publicvoid initializeData()// Initialize the data in the array
{
movieArray[0] =new BluRayGUI(100, 10, 14.99,"The Java Code","yes");
movieArray[1] =new BluRayGUI(101, 3, 11.99,"Return of the Java","yes");
movieArray[2] =new BluRayGUI(102, 20, 16.99,"The Java Strikes Back","no");
movieArray[3] =new BluRayGUI(103, 50, 9.99,"Working out with Java","no");
movieArray[4] =new BluRayGUI(104, 4, 19.99,"The JavaFather","yes");
movieArray[5] =new BluRayGUI(105, 6, 18.99,"Silence of the Java","no");
movieArray[6] =new BluRayGUI(106, 11, 14.99,"Gone with the Java","no");
movieArray[7] =new BluRayGUI(107, 8, 15.99,"You, Me and Java","yes");
movieArray[8] =new BluRayGUI(108, 3, 8.99,"Java the Explorer","no");
movieArray[9] =new BluRayGUI(109, 2, 9.99,"The Last Java","no");
}
publicvoid actionPerformed(ActionEvent ae)// What to do when a button is pushed
{
if(ae.getActionCommand().equals("Exit"))// Exit
{
System.exit(0);
}
elseif(ae.getActionCommand().equals("Next") )// Go to next item
{
int index = 0 + 1;
buildProductPanel(index);
repaint();
}
}
publicstaticvoid main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
InventoryProgramGUI frame =new InventoryProgramGUI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 320);
frame.setVisible(true);
}
}
>

