Need more help with a GUI

It's the ever popular Inventory program again! I'm creating a GUI to display the information contained within an array of objects which, in this case, represent compact discs. I've received some good help from other's posts on this project since it seems there's a few of us working on the same one but now, since each person working on this project has programmed theirs differently, I'm stuck. I'm not sure how to proceed with my ActionListeners for the buttons I've created.

Here's my code:

// GUICDInventory.java

// uses CD class

import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

publicclass GUICDInventoryextends JFrame

{

protected JPanel panel;//panel to hold buttons

protected JPanel cdImage;// panel to hold image

int displayElement = 0;

public String display(int element)

{

return CD2[element].toString();

}//end method

publicstaticvoid main( String args[] )

{

new GUICDInventory();

}// end main

public GUICDInventory()

{

CD completeCDInventory[] =new CD2[ 5 ];// creates a new 5 element array

// populates array with objects that implement CD

completeCDInventory[ 0 ] =new CD2("Sixpence None the Richer" ,"D121401" , 12 , 11.99, 1990 );

completeCDInventory[ 1 ] =new CD2("Clear" ,"D126413" , 10 , 10.99, 1998 );

completeCDInventory[ 2 ] =new CD2("NewsBoys: Love Liberty Disco" ,"2438-51720-2" , 10 , 12.99, 1999 );

completeCDInventory[ 3 ] =new CD2("Skillet: Hey You, I Love Your Soul" ,"D122966" , 9 , 9.99, 1998 );

completeCDInventory[ 4 ] =new CD2("Michael Sweet: Real" ,"020831-1376-204" , 15 , 12.99, 1995 );

//declares totalInventoryValue variable

double totalInventoryValue = CD.calculateTotalInventory( completeCDInventory );

final JTextArea textArea =new JTextArea(display(displayElement));

final JButton prevBtn =new JButton("Previous");

final JButton nextBtn =new JButton("Next");

final JButton lastBtn =new JButton("Last");

final JButton firstBtn =new JButton("First");

prevBtn.addActionListener(new ActionListener()

{

publicvoid actionPerformed(ActionEvent ae)

{

displayElement = (//what goe here? ) % //what goes here?

textArea.setText(display(displayElement));// <--is this right?

}

});

nextBtn.addActionListener(new ActionListener()

{

publicvoid actionPerformed(ActionEvent ae)

{

displayElement = (//what goes here? ) % //what goes here?

textArea.setText(display(displayElement));// <--is this right?

}

});

firstBtn.addActionListener(new ActionListener()

{

publicvoid actionPerformed(ActionEvent ae)

{

displayElement = 0;

textArea.setText(display(displayElement));// <--is this right?

}

});

lastBtn.addActionListener(new ActionListener()

{

publicvoid actionPerformed(ActionEvent ae)

{

displayElement =//what goes here?;

textArea.setText(display(displayElement));// <--is this right?

}

});

JPanel panel =new JPanel(new GridLayout(1,2));

panel.add(firstBtn); panel.add(nextBtn); panel.add(prevBtn); panel.add(lastBtn);

JPanel cdImage =new JPanel(new BorderLayout());

cdImage.setPreferredSize(new Dimension(600,400));

JFrame cdFrame =new JFrame();

cdFrame.getContentPane().add(new JScrollPane(textArea),BorderLayout.CENTER);

cdFrame.getContentPane().add(panel,BorderLayout.SOUTH);

cdFrame.getContentPane().add(new JLabel("",new ImageIcon("cd.gif"),JLabel.CENTER),BorderLayout.NORTH);

cdFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

cdFrame.pack();

cdFrame.setSize(500,200);

cdFrame.setTitle("Compact Disc Inventory");

cdFrame.setLocationRelativeTo(null);

cdFrame.setVisible(true);

}//end GUICDInventory constructor

}// end class GUICDInventory

// CD2.java

// subclass of CD

publicclass CD2extends CD

{

protectedint copyrightDate;// CDs copyright date variable declaration

privatedouble price2;

// constructor

public CD2( String title, String prodNumber,double numStock,double price,int copyrightDate )

{

// explicit call to superclass CD constructor

super( title, prodNumber, numStock, price );

this.copyrightDate = copyrightDate;

}// end constructor

publicdouble getInventoryValue()// modified subclass method to add restocking fee

{

price2 = price + price * 0.05;

return numStock * price2;

}//end getInventoryValue

// Returns a formated String contains the information about any particular item of inventory

public String displayInventory()// modified subclass display method

{

return String.format("\n%-22s%s\n%-22s%d\n%-22s%s\n%-22s%.2f\n%-22s%s%.2f\n%-22s%s%.2f\n%-22s%s%.2f\n \n" ,

"CD Title:", title,"Copyright Date:", copyrightDate,"Product Number:", prodNumber ,"Number in Stock:",

numStock ,"CD Price:" ,"$" , price ,"Restocking fee (5%):","$", price*0.05,"Inventory Value:" ,"$" ,

getInventoryValue() );

}// end method

}//end class CD2

// CD.java

// Represents a compact disc object

import java.util.Arrays;

class CDimplements Comparable

{

protected String title;// CD title (name of product)

protected String prodNumber;// CD product number

protecteddouble numStock;// CD stock number

protecteddouble price;// price of CD

protecteddouble inventoryValue;//number of units in stock times price of each unit

// constructor initializes CD information

public CD( String title, String prodNumber,double numStock,double price )

{

this.title = title;// Artist: album name

this.prodNumber = prodNumber;//product number

this.numStock = numStock;// number of CDs in stock

this.price = price;//price per CD

}// end constructor

publicdouble getInventoryValue()

{

return numStock * price;

}//end getInventoryValue

//Returns a formated String contains the information about any particular item of inventory

public String displayInventory()

{

//return the formated String containing the complete information about CD

return String.format("\n%-22s%s\n%-22s%s\n%-22s%.2f\n%-22s%s%.2f\n%-22s%s%.2f\n%-22s%s%.2f\n \n" ,

"CD Title:", title,"Product Number:", prodNumber ,"Number in Stock:",

numStock ,"CD Price:" ,"$" , price ,"Restocking fee (5%):","$", price*0.05,"Inventory Value:" ,"$" ,

getInventoryValue() );

}// end method

//method to calculate the total inventory of the array of objects

publicstaticdouble calculateTotalInventory( CD completeCDInventory[] )

{

double totalInventoryValue = 0;

for (int count = 0; count < completeCDInventory.length; count++ )

{

totalInventoryValue += completeCDInventory[count].getInventoryValue();

}// end for

return totalInventoryValue;

}// end calculateTotalInventory

// Method to return the String containing the Information about Inventory's Item

//as appear in array (non-sorted)

publicstatic String displayTotalInventory( CD completeCDInventory[] )

{

//string which is to be returned from the method containing the complete inventory's information

String retInfo ="\nInventory of CDs (unsorted):\n";

//loop to go through complete array

for (int count = 0; count < completeCDInventory.length; count++ )

{

retInfo = retInfo +"Item# " + (count + 1);//add the item number in String

retInfo = retInfo + completeCDInventory[count].displayInventory();//add the inventory detail in String

}// end for

return retInfo;//return the String containing complete detail of Inventory

}// end displayTotalInventory

publicint compareTo( Object obj )//overlaod compareTo method

{

CD tmp = ( CD )obj;

if( this.title.compareTo( tmp.title ) < 0 )

{

return -1;//instance lt received

}

elseif( this.title.compareTo( tmp.title ) > 0 )

{

return 1;//instance gt received

}

return 0;//instance == received

}// end compareTo method

//Method to return the String containing the Information about Inventory's Item

// in sorted order (sorted by title)

publicstatic String sortedCDInventory( CD completeCDInventory[] )

{

//string which is to be returned from the method containing the complete inventory's information

String retInfo ="\nInventory of CDs (sorted by title):\n";

Arrays.sort( completeCDInventory );// sort array

//loop to go through complete array

for(int count = 0; count < completeCDInventory.length; count++ )

{

retInfo = retInfo +"Item# " + (count + 1);//add the item number in String

retInfo = retInfo + completeCDInventory[count].displayInventory();//add the inventory detail in String

}

return retInfo;//return the String containing complete detail of Inventory

}// end method sortedCDInventory

}// end class CD

[18268 byte] By [sammyboy78a] at [2007-11-27 9:28:42]
# 1
Well I don't understand the question.Don't ask the question: "Is this right?". Try it and then debug the program if it doesn't work. Thats what assignments are about. Learning to problem solve.
camickra at 2007-7-12 22:34:38 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for the great advice but I've already tried that for at least a couple hours. That's why I'm here now. Although I'm beginningto wonder why I bother.
sammyboy78a at 2007-7-12 22:34:38 > top of Java-index,Desktop,Core GUI APIs...
# 3

nextBtn.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

displayElement = (//what goes here? ) % //what goes here?

textArea.setText(display(displayElement));// <--is this right?

}

});

Above is your code for the "Next" button.

You ask the question "What goes here"? Well what do you think goes there?

If you are looking at item 1 and you press the "Next" button do you not want to look at item 2?

So the obvious solution would be to add 1 to the previous item value. Does that not make sense? What problem do you have when you try that code?

Next you say "Is this right"? Well how are we supposed to know? You wrote the code. We don't know what the code is supposed to do. Again you try it. If it doesn't work then describe the problem you are having. I for one can't execute your code because I don't use JDK5. So looking at the code it looks reasonable, but I can't tell by looking at it what is wrong. Only you can add debug statements in the code to see whats happening.

If you want help learn to as a proper question and doen't expect us to spoon feed the code to you. This is your assignment, not ours. We are under no obligation to debug and write the code for you. The sooner you learn that, the more help you will receive.

camickra at 2007-7-12 22:34:39 > top of Java-index,Desktop,Core GUI APIs...