Im new to java and trying to get buttons to work. My next button works as intended however the first and last do not and the previous one only takes me back one space if i'm on the second item. I need the prev button to rotate backwards through the items the same as the next does and of course the last and first need to go as they should as well.

public class Cameras {
	private String productName;
	private String productDepartment;
	private int productNumber;
	private int unitsNumber;
	private double unitPrice;

	public Cameras() {
	}

	public Cameras(String camera, String department, int number, int stock,
			double price) {
		productName = camera;
		productDepartment = department;
		productNumber = number;
		unitsNumber = stock;
		unitPrice = price;
	}

	public void setproductName(String camera) {
		setproductName(camera);
	}

	public String getproductName() {
		return productName;
	}

	public void setproductDepartment(String department) {
		setproductDepartment(department);
	}

	public String getproductDepartment() {
		return productDepartment;
	}

	public void setproductNumber(int number) {
		productNumber = number;
	}

	public int getproductNumber() {
		return productNumber;
	}

	public void setunitsNumber(int stock) {
		unitsNumber = (stock < 0) ? 0 : stock;
	}

	public double getunitsNumber() {
		return unitsNumber;
	}

	public void setunitPrice(double price) {
		unitPrice = (price < 0.0) ? 0.0 : price;
	}

	public double getunitPrice() {
		return unitPrice;
	}

	public double getValue() {
		return getunitPrice() * unitsNumber;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}
}
public class DigitalCameras extends Cameras {
	double ImageResolution;
	double rate = 0.05;
	private double restockingfee;

	public DigitalCameras(String camera, String department, int number,
			int stock, double price, double ImageResolution) {
		super(camera, department, number, stock, price);
		this.ImageResolution = ImageResolution;
	}

	public double getImageResolution() {
		return ImageResolution;
	}

	public void setImageResolution(double ImageResolution) {
		this.ImageResolution = ImageResolution;
	}

	public double getRestockingfee() {
		return super.getValue() * rate;
	}

	// calculates inventory value after adding restocking fee
	public double getValue() {
		double value = super.getValue();
		// Add 5% restocking fee
		value = value + getRestockingfee();
		return value;
	}
}
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class InventoryMain extends JFrame implements ActionListener {
	// Index of current element of array
	private int position = 0;
	// create Cameras array
	Cameras[] productArray = null;

	// Various components that are used in the program
	private JLabel brandLabel = new JLabel("Brand:");
	private JTextField brandText = new JTextField("");
	private JLabel departmentLabel = new JLabel("Department: ");
	private JTextField departmentText = new JTextField("");
	private JLabel productNumberLabel = new JLabel("Product Number: ");
	private JTextField productNumberText = new JTextField("");
	private JLabel unitsInStockLabel = new JLabel("Quantity: ");
	private JTextField unitsInStockText = new JTextField("");
	private JLabel unitPriceLabel = new JLabel("Unit Price: ");
	private JTextField unitPriceText = new JTextField("");
	private JLabel invValueLabel = new JLabel("Inventory Value: ");
	private JTextField invValueText = new JTextField("");
	// Digital Camera object related components
	private JLabel imageResolutionLabel = new JLabel("Image Resolution: ");
	private JTextField imageResoultionText = new JTextField("");
	private JLabel restockingFeeLabel = new JLabel("Restocking Fee: ");
	private JTextField restockingFeeText = new JTextField("");
	private JLabel totalInvValueLabel = new JLabel("Total Inventory Value: ");
	private JTextField totalInvValueText = new JTextField("");

	private JButton nextButton, prevButton, firstButton, lastButton;

	public InventoryMain() {
		setProductData();
		// Sort the products
		sortCameras(productArray);

		JPanel buttonPanel = new JPanel();
		nextButton = new JButton("Next");
		nextButton.addActionListener(this);
		
		prevButton = new JButton("Prev");
		prevButton.addActionListener(this);
		
		firstButton = new JButton("First");
		firstButton.addActionListener(this);
		
		lastButton = new JButton("Last");
		lastButton.addActionListener(this);

		buttonPanel.add(nextButton);
		buttonPanel.add(prevButton);
		buttonPanel.add(firstButton);
		buttonPanel.add(lastButton);

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(buttonPanel, BorderLayout.NORTH);

		JPanel dataPanel = new JPanel();
		dataPanel.setLayout(new GridLayout(9, 2));

		dataPanel.add(brandLabel);
		brandText.setEditable(false);
		dataPanel.add(brandText);

		dataPanel.add(departmentLabel);
		departmentText.setEditable(false);
		dataPanel.add(departmentText);

		dataPanel.add(productNumberLabel);
		productNumberText.setEditable(false);
		dataPanel.add(productNumberText);

		dataPanel.add(unitsInStockLabel);
		unitsInStockText.setEditable(false);
		dataPanel.add(unitsInStockText);

		dataPanel.add(unitPriceLabel);
		unitPriceText.setEditable(false);
		dataPanel.add(unitPriceText);

		dataPanel.add(invValueLabel);
		invValueText.setEditable(false);
		dataPanel.add(invValueText);

		dataPanel.add(imageResolutionLabel);
		imageResoultionText.setEditable(false);
		dataPanel.add(imageResoultionText);

		dataPanel.add(restockingFeeLabel);
		restockingFeeText.setEditable(false);
		dataPanel.add(restockingFeeText);

		dataPanel.add(totalInvValueLabel);
		totalInvValueText.setEditable(false);
		dataPanel.add(totalInvValueText);

		displayProduct();
		getContentPane().add(dataPanel, BorderLayout.CENTER);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		setSize(400, 300);
		setResizable(false);

	}

	private void setProductData() {
		int maxItems = 3;
		productArray = new Cameras[maxItems];
		try {
			String camera;
			String department;
			int number;
			int stock;
			double price;
			String imageResolutionStr;
			double imageResolution;
			for (int k = 0; k < productArray.length; k++) {
				camera = JOptionPane
						.showInputDialog("Enter A Brand For Camera "
								+ String.valueOf(k + 1) + " of "
								+ String.valueOf(maxItems));
				department = JOptionPane
						.showInputDialog("Enter A Department For Camera "
								+ String.valueOf(k + 1) + " of "
								+ String.valueOf(maxItems));
				number = Integer.parseInt(JOptionPane
						.showInputDialog("Enter Product Number For Camera "
								+ String.valueOf(k + 1) + " of "
								+ String.valueOf(maxItems)));
				stock = Integer.parseInt(JOptionPane
						.showInputDialog("Enter Quantity For Camera "
								+ String.valueOf(k + 1) + " of "
								+ String.valueOf(maxItems)));
				price = Double.parseDouble(JOptionPane
						.showInputDialog("Enter A Price For Camera "
								+ String.valueOf(k + 1) + " of "
								+ String.valueOf(maxItems)));
				imageResolutionStr = JOptionPane
						.showInputDialog("Enter Image Resolution "
								+ String.valueOf(k + 1) + " of "
								+ String.valueOf(maxItems)
								+ " (Leave blank if not a digital camera)");
				// If image resolution left blank assume its Camera object
				// otherwise its digital camera
				if (null == imageResolutionStr
						|| imageResolutionStr.trim().equals("")) {
					productArray[k] = new Cameras(camera, department, number,
							stock, price);
				} else {
					imageResolution = Double.parseDouble(imageResolutionStr);
					productArray[k] = new DigitalCameras(camera, department,
							number, stock, price, imageResolution);
				}
			}
		} catch (Exception ec) {
			System.out.println(ec.getMessage());
			System.exit(-1);
		}

	}

	private void sortCameras(Cameras Cameras[]) {
		int in, out, NumOfEle = Cameras.length;
		for (out = 1; out < NumOfEle; out++) {
			Cameras temp = Cameras[out];
			in = out;
			while (in > 0
					&& Cameras[in - 1].getproductName().compareTo(
							temp.getproductName()) > 0) {
				Cameras[in] = Cameras[in - 1];
				--in;
			}
			Cameras[in] = temp;
		}
	}

	private double calculateEntireInventory() {
		Cameras obj = null;
		double totalInventory = 0;
		for (int i = 0; i < productArray.length; i++) {
			obj = productArray[i];
			totalInventory += obj.getValue();
		}
		return totalInventory;
	}

	// button pushes...
	// // Sets the void for the next-button click
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == nextButton) {
			if (position != (productArray.length - 1)) {
				position++;
			} else {
				position = 0;
			}
		}
		displayProduct();
		
		if (event.getSource() == firstButton) {
			position = 0;
		}
		displayProduct();
		
		if (event.getSource() == lastButton) {
			position = -1;
		}
		displayProduct();
		
		if (event.getSource() == prevButton) {
			if (position != (productArray.length - 1)) {
				position--;
			} else {
				position = 0;
			}
		}
		displayProduct();
		
	}

	public void displayProduct() {
		NumberFormat formatter = NumberFormat.getCurrencyInstance();
		Cameras product = productArray[position];
		double totalInvValue = calculateEntireInventory();
		// Start of getting the values
		brandText.setText(product.getproductName());
		departmentText.setText(product.getproductDepartment());
		productNumberText.setText(product.getproductNumber() + "");
		unitsInStockText.setText(product.getunitsNumber() + "");
		unitPriceText.setText(formatter.format(product.getunitPrice()));
		invValueText.setText(formatter.format(product.getValue()));
		if (product instanceof DigitalCameras) {
			imageResolutionLabel.setVisible(true);
			imageResoultionText.setVisible(true);
			restockingFeeLabel.setVisible(true);
			restockingFeeText.setVisible(true);
			imageResoultionText.setText(((DigitalCameras) product)
					.getImageResolution() + "");
			restockingFeeText.setText(formatter
					.format(((DigitalCameras) product).getRestockingfee()));
		} else {
			imageResolutionLabel.setVisible(false);
			imageResoultionText.setVisible(false);
			restockingFeeLabel.setVisible(false);
			restockingFeeText.setVisible(false);
			imageResoultionText.setText("");
			restockingFeeText.setText("");
		}
		totalInvValueText.setText(formatter.format(totalInvValue));
	}

	// main method
	public static void main(String[] args) {
		InventoryMain gui = new InventoryMain();
		gui.setVisible(true);

	}
}

First looks OK to me - what is it doing that's wrong?
Last sets position to -1, that's not valid. Shouldn't it be productArray.length - 1?
Previous is testing for position at the end of the array (it's a copy of the logic for next), shouldn't this be testing for position at the start of the array (position == 0) ?

I think you messed up the position variable in the button handler a bit. I changed the code to this, and it works:

public void actionPerformed(ActionEvent event) {
		if (event.getSource () == nextButton) {
			if (position < productArray.length - 1)
				position++;
			else
				position = 0;
		} else if (event.getSource () == firstButton) {
			position = 0;
		} else if (event.getSource () == lastButton) {
			position = productArray.length - 1;
		} else if (event.getSource () == prevButton) {
			if (position > 0)
				position--;
			else
				position = productArray.length - 1;
		}
		displayProduct ();

	}

Thank you that worked great

Please mark as solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.