I am trying to set up 4 buttons that will move through my array in the GUI. I am entering 2 items and telling the GUI to spit out the 2nd record. If I can just figure out how to get the "First" button to work, I think I can get the rest to work. The code compiles just fine but I am unable to get it to go back to the first record when clicking the firstButton. I think my problem is with getting my variable "index" to hook up with the record of my array???
Any help is much appreciated!
Thanks.
Brandon
TestBooks.java
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.text.*;
import java.awt.event.*;
import java.applet.*;
public class TestBooks extends JFrame// define class
{
public static JButton firstButton;
public static JButton prevButton;
public static JButton nextButton;
public static JButton lastButton;
DecimalFormat decimal = new DecimalFormat("$0.00");
public TestBooks(Books[] arrayOfBooks)
{
super("Inventory GUI");
Container con = getContentPane();
BoxLayout layout = new BoxLayout(con, BoxLayout.Y_AXIS);
con.setLayout(layout);
JPanel topPanel= new JPanel();
JPanel bottomPanel= new JPanel();
con.add(topPanel, BorderLayout.NORTH);
con.add(bottomPanel, BorderLayout.SOUTH);
Books book = arrayOfBooks[1];
JLabel label1 = new JLabel( "Title: " + book.getBookTitle() );
add( label1 );
JLabel isbnLabel = new JLabel( "ISBN: " +
((Books)book).getBookIsbn() );
add( isbnLabel );
JLabel label2 = new JLabel( "Price: " +
(decimal.format(book.getBookPrice())) );
add( label2 );
JLabel label3 = new JLabel( "Quantity in stock: " +
book.getBookQuantity() );
add( label3 );
JLabel label4 = new JLabel( "Book Total: " +
(decimal.format(book.getBookInventory())) );
add( label4 );
topPanel.add(label1);
topPanel.add(isbnLabel);
topPanel.add(label2);
topPanel.add(label3);
topPanel.add(label4);
firstButton = new JButton("|<<");
firstButton.addActionListener(new FirstButtonHandler());
bottomPanel.add(firstButton);
prevButton = new JButton("<<");
bottomPanel.add(prevButton);
nextButton = new JButton(">>");
bottomPanel.add(nextButton);
lastButton = new JButton(">>|");
bottomPanel.add(lastButton);
pack();
setVisible( true);
}
int index = 0;
private class FirstButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (index > 0)
{
index = 0;
}
}
}
// main method begins execution of Java application
public static void main( String args[] )
{
// This explains the program
System.out.println( "\nThis program will provide an inventory
of books." );
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
// Prompt user for number of books to enter
System.out.println( "\nHow many books would you like to
inventory today?" );
int numBooks = input.nextInt(); // Reads the data entered
// Create an array of Book objects according to the number that
the user inputs above
Books[] bookArray = new Books[numBooks];
// set the total inventory value
double bookTotal = 0.00;
// Begin for loop & declare variable i to store array items in
until total number of books has been met
for (int i = 0; i < numBooks; i++)
{
// create book objects within for loop so array is not
overwritten on each itteration
Books book = new Books();
// create another Scanner to obtain input from command
window during for loop
Scanner in = new Scanner( System.in );
// Request the book title
System.out.println( "\nPlease enter book title: " );
book.setBookTitle(in.nextLine()); // Reads the data entered
// Request the isbn for the book
System.out.println( "\nPlease enter the ISBN for \"" +
book.getBookTitle() + "\"" );
book.setBookIsbn(in.nextInt()); // Reads the data entered
// Request the price of the book
System.out.println( "\nPlease enter the dollar amount for
\"" + book.getBookTitle() + "\"" );
book.setBookPrice(in.nextDouble()); // Reads the data entered
//Request the number of books in stock
System.out.println( "\nPlease enter the number of books
currently in stock:" );
book.setBookQuantity(in.nextInt()); // Reads the data entered
System.out.println( "\nBook information entered
successfully!" );
// marks the completion of 1 itteration of for loop
bookArray[i] = book;
} // end for loop that populates the array
TestBooks myFrame = new TestBooks(bookArray);
myFrame.setSize( 400, 300 );
myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}// end method main
}// end class TestBooks
Books.java
public class Books // define class
{
private String bookTitle;
private int bookIsbn;
private double bookPrice;
private int bookQuantity;
//This constructor takes no arguments
public Books(){
}
//This constructor takes title, isbn, price, and quantity as
arguments
public Books(String title, int isbn, double price, int quantity )
{
this.bookTitle=title;
this.bookIsbn=isbn;
this.bookPrice=price;
this.bookQuantity=quantity;
}
public void setBookTitle(String title)
{
bookTitle = title;
}
public String getBookTitle()
{
return bookTitle;
}
public void setBookIsbn(int isbn)
{
bookIsbn=isbn;
}
public int getBookIsbn()
{
return bookIsbn;
}
public void setBookPrice(double price)
{
bookPrice=price;
}
public double getBookPrice()
{
return bookPrice;
}
public void setBookQuantity(int quantity)
{
bookQuantity=quantity;
}
public int getBookQuantity()
{
return bookQuantity;
}
public double getBookInventory()
{
return bookPrice * bookQuantity;
}
}
Magazine.java
public class Magazine extends Books // define class
{
private String magazineIssue;
// This constructor takes no arguments
public Magazine(){
}
// this constructor pulls from class Books and adds book genre
public Magazine(String title, int isbn, double price, int quantity,
String issue )
{
super(title, isbn, price, quantity);
this.magazineIssue=issue;
}
//
public void setMagazineIssue(String issue)
{
magazineIssue = issue;
}
//
public String getMagazineIssue()
{
return magazineIssue;
}
public double getMagazineValue()
{
return getBookInventory() * 1.05;
}
}