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

}

Hi.
Well, at first - you don't change the "index" variable from the time, when it was created. You should specify the current index (since you're displaying not the first record of your array).

The second one - when your prog catch the proper event to process - you're changing only "index" value .. you also need to refresh your form.

Hi.
Well, at first - you don't change the "index" variable from the time, when it was created. You should specify the current index (since you're displaying not the first record of your array).

The second one - when your prog catch the proper event to process - you're changing only "index" value .. you also need to refresh your form.

Well I think I follow what you're saying but I still don't quite understand. And just so you know, the only reason I am displaying the 2nd record in the array is just to verify that my "First" button is working. Once I get that working I am going to set it back to arrayOfBooks[0]

Any chance you could give me an example of what you're talking about?
Thanks

I think you need to refactor a bit. Refreshing the form will be hard since all the variables are stored inside the constructor, even the input from the user.
Declare all the forms and the arrayOfBooks as global, and then use the actionevent listener to fire an update of the text labels.
you can use the same listener for all the buttons, just check which button has been pressed, and change the index accordingly

Making variables global for the sake of easily doing something is usually a bad programming practice. Instead, you'd usually make get methods and set methods to get your data and to modify it.

Thanks BestJewSinceJC for valuable suggestion.
Do not use static modifier unless it is required. static modifier is used when we want to declare someting globally. In your program it is unnecessary.

Anonymous Inner class is the best practice to attach event handler.

...
    nextButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
 	  index++;
          showBookRecord();
        }
     });
    ...

Here is your code with few changes.
1. All JComponents are instance - declared without static modifier.
2. Declare arrayOfBook array.
3. I wrote a new method to showBookRecord()

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 JButton firstButton;
	public JButton prevButton;
	public JButton nextButton;
	public JButton lastButton;
	DecimalFormat decimal = new DecimalFormat("$0.00");
        Books []arrayOfBooks;     
        int index=0;

        JLabel label1;
        JLabel isbnLabel;
        JLabel label2;
        JLabel label3;
        JLabel label4;

   public TestBooks(Books[] arrayOfBooks)
   {
    super("Inventory GUI");
    Container con = getContentPane();
    this.arrayOfBooks=arrayOfBooks;
    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[0];

		label1 = new JLabel( "Title: " + book.getBookTitle() );
     		add( label1 );
       		isbnLabel = new JLabel( "ISBN: " + ((Books)book).getBookIsbn() );
		add( isbnLabel );
		label2 = new JLabel( "Price: " + (decimal.format(book.getBookPrice())) );
     		add( label2 );
		label3 = new JLabel( "Quantity in stock: " + book.getBookQuantity() );
     		add( label3 );
		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(">>");
                nextButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
			 
			  index++;
                          showBookRecord();
			 
		      }
                 });
		bottomPanel.add(nextButton);

		lastButton = new JButton(">>|");
		bottomPanel.add(lastButton);

     		pack();
     		setVisible( true);
   	}

	private class FirstButtonHandler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			 
			  index = 0;
                          showBookRecord();
			 
  		  }
		}
         // New Method
         void showBookRecord() {

                if(index<0)
                     index=0;
                if(index>=arrayOfBooks.length)
                     index=arrayOfBooks.length - 1;

            	Books book = arrayOfBooks[index];
                 
		label1.setText("Title: " + book.getBookTitle() );
       		isbnLabel.setText( "ISBN: " + ((Books)book).getBookIsbn() );
		label2.setText("Price: " + (decimal.format(book.getBookPrice())) );
		label3.setText( "Quantity in stock: " + book.getBookQuantity() );
		label4.setText("Book Total: " + (decimal.format(book.getBookInventory())) );
         }

   	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

     		Books[] bookArray = new Books[numBooks];

     		// set the total inventory value
     		double bookTotal = 0.00;

     		for (int i = 0; i < numBooks; i++)
     		{
       			Books book = new Books();
       			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
         		System.out.println( "\nPlease enter the ISBN for \"" + book.getBookTitle() + "\"" );
         		book.setBookIsbn(in.nextInt()); // Reads the data entered
         		System.out.println( "\nPlease enter the dollar amount for \"" + book.getBookTitle() + "\"" );
         		book.setBookPrice(in.nextDouble()); // Reads the data entered

         		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

Making variables global for the sake of easily doing something is usually a bad programming practice. Instead, you'd usually make get methods and set methods to get your data and to modify it.

I meant private class members :$

adatapost, I can't thank you enough for helping me with this.... now time to do some studying of your code to make sure I understand it.

Works like a charm! Thanks.

This question has already been answered. Start a new discussion instead.