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);

}

}

>

[9601 byte] By [k.c.a] at [2007-10-3 10:51:41]
# 1

See if this little demo that I just wrote doesn't help you. If you have any questions, let me know:

import javax.swing.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class Test extends JFrame {

private String[] data = { "Element 1", "Element 2", "Element 3", "Element 4", "Element 5" };

private JButton nextBtn;

private JButton backBtn;

private JButton firstBtn;

private JButton lastBtn;

private JTextField dataField;

private int currIndex = 0;

public Test() {

nextBtn = new JButton("Next");

backBtn = new JButton("Back");

firstBtn = new JButton("First");

lastBtn = new JButton("Last");

dataField = new JTextField(30);

this.getContentPane().setLayout(new java.awt.FlowLayout());

this.getContentPane().add(dataField);

this.getContentPane().add(nextBtn);

this.getContentPane().add(backBtn);

this.getContentPane().add(firstBtn);

this.getContentPane().add(lastBtn);

this.pack();

this.setResizable(false);

this.setLocationRelativeTo(null);

this.setTitle("Demo");

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

goFirst();

nextBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

goNext();

}

});

backBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

goBack();

}

});

firstBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

goFirst();

}

});

lastBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

goLast();

}

});

}

private void goNext() {

if (currIndex < data.length -1) {

currIndex++;

dataField.setText(data[currIndex]);

}

}

private void goBack() {

if (currIndex > 0) {

currIndex--;

dataField.setText(data[currIndex]);

}

}

private void goFirst() {

currIndex = 0;

dataField.setText(data[currIndex]);

}

private void goLast() {

currIndex = data.length - 1;

dataField.setText(data[currIndex]);

}

public static void main(String[] argv) { new Test().setVisible(true); }

}

Navy_Codera at 2007-7-15 6:17:03 > top of Java-index,Java Essentials,New To Java...
# 2

public class InventoryProgramGUI extends JFrame implements ActionListener

{

private Container content = getContentPane();

private JPanel panel;

private BluRayGUI movieArray[] = new BluRayGUI[10];

private JComboBox jcbMovieList;

private int index = 0;//<--move to here

...

public void actionPerformed(ActionEvent ae) // What to do when a button is pushed

{

if(ae.getActionCommand().equals("Exit")) // Exit

{

System.exit(0);

}

else if(ae.getActionCommand().equals("Next") ) // Go to next item

{

if(index < movieArray.length-1)//<check inside bounds

{

index++;//<--

buildProductPanel(index);

repaint();

}

}

}

Michael_Dunna at 2007-7-15 6:17:03 > top of Java-index,Java Essentials,New To Java...
# 3

int index = 0 + 1;

Firstly, what is the point of adding 1 to 0 you could have easily said index = 1. However, your main problem is no matter how many times you click the button index will always be initialised to one. NOTE: index is a local variable and will cease to exist as soon as you exit the actionPerformed method.

floundera at 2007-7-15 6:17:03 > top of Java-index,Java Essentials,New To Java...
# 4

> public class InventoryProgramGUI extends JFrame

> implements ActionListener

> {

>private Container content = getContentPane();

> private JPanel panel;

> private BluRayGUI movieArray[] = new

> BluRayGUI[10];

>private JComboBox jcbMovieList;

> private int index = 0;//<--move to here

>...

>

> public void actionPerformed(ActionEvent ae) //

> What to do when a button is pushed

>{

> if(ae.getActionCommand().equals("Exit")) //

> Exit

>{

>System.exit(0);

>}

> else if(ae.getActionCommand().equals("Next") )

> // Go to next item

>{

> if(index < movieArray.length-1)//<check

> inside bounds

>{

>index++;//<--

>buildProductPanel(index);

>repaint();

> }

>

>}

>

>

>

I wrote an ENTIRE working example before you did ... NEENER NEENER NEEEEEEENERRRRR!!! ;-)

Navy_Codera at 2007-7-15 6:17:03 > top of Java-index,Java Essentials,New To Java...
# 5
> I wrote an ENTIRE working example before you did ... NEENER NEENER NEEEEEEENERRRRR!!! ;-)I like it :-)
Michael_Dunna at 2007-7-15 6:17:03 > top of Java-index,Java Essentials,New To Java...
# 6
> I wrote an ENTIRE working example before you> did ... NEENER NEENER NEEEEEEENERRRRR!!! ;-)WOW! You're my hero.
floundera at 2007-7-15 6:17:03 > top of Java-index,Java Essentials,New To Java...
# 7
> WOW! You're my hero.What can I say? .... ;-)(You did catch that I was picking right?)
Navy_Codera at 2007-7-15 6:17:03 > top of Java-index,Java Essentials,New To Java...
# 8
> (You did catch that I was picking right?)Your nose?
floundera at 2007-7-15 6:17:03 > top of Java-index,Java Essentials,New To Java...
# 9
> Your nose?no, farther south...
Navy_Codera at 2007-7-15 6:17:03 > top of Java-index,Java Essentials,New To Java...
# 10
Is your name George Michael?
floundera at 2007-7-15 6:17:03 > top of Java-index,Java Essentials,New To Java...
# 11
> Is your name George Michael?LOL
Navy_Codera at 2007-7-15 6:17:03 > top of Java-index,Java Essentials,New To Java...