Error message when I add Buttons to my program

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 17454 Views - Last Post: 30 June 2011 - 08:27 PM Rate Topic: -----

#16 ITStudent02   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 11

Re: Error message when I add Buttons to my program

Posted 29 June 2011 - 08:12 AM

Thank you pbl. I did fix most of error messages but now I have buttons that do absolutely nothing. My logo, my program nothing is showing in my GUI window. I have a First and Last button and that is it. I have tried manipulating, moving and changing this section of code because I think this is the section that holds the printers inventory.
public static Printer[] sortArray(Printer[] printers)
		{
 
			String[] productName = new String[printers.length];
 
 
			Printer[] serial = new Printer [printers.length];
 
 
			for (int i = 0; i < printers.length; i++)
			{
			  productName = printers.getproductName();
			}
 
 
			Arrays.sort(productName);
 
			for (int i = 0; i < printers.length; i++)
			{
			     for (int j = 0; j < productName length; j++)
			     {
			          if (printers.getproductName().equalsIgnoreCase(productName[j]))
					  {
					     serial[j] = printers;
 
					  }
 
			     }
			}
 
			return serial;
 
		}
		public static double totalInventory(Printer[] printers)
		{
			double total = 0;
 
			for (int i = 0; i < printers.length; i++)
			{
				total += printers.totalInventory();
			}
 
			return total;
		}

Unfortunately if I move it from where it is now in my program, I get a tone of error messages. Here is my product class and the Laser class which extends the printer and holds all my info.
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Arrays;

class Printer {

	//declare class variables
	private String itemNumber;
	private String productName;
	private int unitsInStock;
	private double unitPrice;
	private double totalInventory;
	private String serialNumber;
	private double restockFee;
	NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

	//class constructor
	public Printer (String itemNumber, String productName, int unitsInStock, double unitPrice, String serialNumber){
		this.itemNumber = itemNumber;
		this.productName = productName;
		this.unitsInStock = unitsInStock;
		this.unitPrice = unitPrice;
		this.serialNumber = serialNumber;
		this.restockFee = restockFee;
	}

	//get and set methods

	//item number
	public String getItemNumber(){
		return itemNumber;
	}

	public void setItemNumber(String itemNumber){
		this.itemNumber = itemNumber;
	}


	//printer name
	public String getProductName(){
		return productName;
	}

	public void setProductName(String productName){
		this.productName = productName;
	}

	//available units
	public int getUnitsInStock(){
		return unitsInStock;
	}

	public void setUnitsInStock (int unitsInStock){
		this.unitsInStock = unitsInStock;
	}

	//price
	public double getUnitPrice(){
		return unitPrice;
	}

	public void setUnitPrice (double unitPrice){
		this.unitPrice = unitPrice;
	}

	//calculate the total inventory
	public double totalInventory()
	{
		return unitPrice * unitsInStock;
	}

	public double restockFee()
	{
		return unitPrice * .05;
	}

	//out put the variables with a toString method
	public String toString ()
	{
			return "Item Number: " + itemNumber + "\nProduct Name: " + productName + "\nUnits In Stock: " + unitsInStock +
									       "\nPrice : " +  nf.format(unitPrice)  + "\nTotal Value: " + nf.format(totalInventory()) + "\nSerial Number: " + serialNumber + "\nRestock Fee: " + nf.format(restockFee());
	}

}//end Printer class

class Laser extends Printer
{

	//class variables
	public String serialNumber;
	public double restockFee;
	public static final double RESTOCK_FEE_PERCENTAGE = .05;

	//class constructor
	public Laser (String itemNumber, String productName, int unitsInStock, double unitPrice, double totalInventory, String serialNumber)
	{
		super(itemNumber, productName, unitsInStock, unitPrice, serialNumber);
		this.serialNumber = serialNumber;

		//calculate the restock fee
		restockFee = unitPrice * RESTOCK_FEE_PERCENTAGE;
	}
	//get and set methods
	public String getSerialNumber()
	{
		return serialNumber;
	}

	public void setSerialNumber(String serialNumber)
	{
		this.serialNumber = serialNumber;
	}
	public double getRestockFee()
	{
		return restockFee;
	}

	// output restock fee and serial number
	public String toString ()
	{
		return super.toString() + "\n" + "Serial Number: " + serialNumber + "Restock Fee:" + nf.format(getRestockFee());
	}

}



Here is my problem. This is my main class and the class I need to hold my buttons, my logo and info that I need to respond to all my printers inventory as per the navigation of the buttons.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Arrays;

//Filename: <InventoryPart4.java>
//Description: <program outputs Printer inventory>
// Author Name: <Chrstina Ditzel>
// Date: <June 20, 2011>
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Arrays;
import javax.swing.*;
public class InventoryPart5
{
		public static Printer[] sortArray(Printer[] printers)
		{

			String[] productName = new String[printers.length];


			Printer[] serialNumber = new Printer [printers.length];


			for (int i = 0; i < printers.length; i++)
			{
			  productName[i] = printers[i].getProductName();
			}


			Arrays.sort(productName);

			for (int i = 0; i < printers.length; i++)
			{
			     for (int j = 0; j < productName.length; j++)
			     {
			          if (printers[i].getProductName().equalsIgnoreCase(productName[j]))
					  {
					     serialNumber[j] = printers[i];

					  }

			     }
			}

			return serialNumber;

		}
		public static double totalInventory(Printer[] printers)
		{
			double total = 0;

			for (int i = 0; i < printers.length; i++)
			{
				total += printers[i].totalInventory();
			}

			return total;
		}
		static int printerIndex= 0;

		public static JTextArea PrepareDisplay(Printer myPrinter, JTextArea myTextArea)
		{
		NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

		myTextArea.setText("");

		myTextArea.append(myPrinter.toString());
		return myTextArea;
	}

		public static void main (String args[])
	{
		Printer brother = new Printer("P1101", "Brother Inkjet Muliti-function Printer", 12, 217.60, "BroLM200");
		Printer canon = new Printer("P1102","Canon Bubble Jet Photo Printer", 13, 1249.98, "CanJPH300");
		Printer dell = new Printer("P1103", "Dell Multi-Function Laser Printer", 3, 149.99, "DelMFL400");
		Printer hp = new Printer("P1104","HP LaserJet Printer" , 5, 149.99, "HPL500");
		Printer lexmar = new Printer("P1105","Lexmar CLP Printer", 10, 299.98, "LexCLP600");
		//create inventory items in array
		final Printer[] printers = new Printer[5];

		printers[0] = brother;
		printers[1] = canon;
		printers[2] = dell;
		printers[3] = hp;
		printers[4] = lexmar;


		//initialize the JTextArea Class and set the parameters
		final JTextArea textArea = new JTextArea(10,15);
		textArea.setText("");
		textArea.setEditable(false);


	 	 //create the first and last buttons and add them to the button panel
		 JPanel  buttonPanel = new JPanel();
		 buttonPanel.setLayout(new GridLayout(1, 3));

		JButton firstButton = new JButton("First");
			   buttonPanel.add(firstButton);
			   firstButton.addActionListener(new ActionListener()
			   {
			   			public void actionPerformed(ActionEvent e)
			   			{
			   				printerIndex = 0;  //set index to the first item in the array

			   			}
				});


					   JButton lastButton = new JButton("Last");
					   buttonPanel.add(lastButton);
					   lastButton.addActionListener(new ActionListener() {
					   	   			public void actionPerformed(ActionEvent e) {
					   	   				printerIndex = (printers.length - 3); //set index to the last item in the array

					   	   			}
		});

						//create company logo and then add it to the logo panel
					   JLabel logoLabel = new JLabel (new ImageIcon("Company Logo.jpg"));
					   JPanel logoPanel = new JPanel();
					   logoPanel.add(logoLabel);

					   JPanel centerPanel = new JPanel();
					   centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));

				//prepare the text that will be displayed in the GUI
				for (int i = 0; i < printers.length; i++ )
				{
					textArea.append("\n"+ printers[i] + "\n");
				}
		//initialize the GUI window and set the parameters
		JFrame frame = new JFrame();
		frame.getContentPane().add(new JScrollPane(textArea));
		frame.setLayout(new BorderLayout());
		frame.add(logoPanel, BorderLayout.NORTH);
		frame.add(buttonPanel, BorderLayout.SOUTH);
	    frame.add(centerPanel, BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);


	}//end main
}//end Iventory class

Was This Post Helpful? 0
  • +
  • -

#17 ITStudent02   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 11

Re: Error message when I add Buttons to my program

Posted 29 June 2011 - 09:03 AM

View Postpbl, on 28 June 2011 - 06:34 PM, said:

no ); after that statement
firstButton.addActionListener(new ActionListener()]b]);[/b]
in that ActionListener the variable printerIndex is not defined neither the method prepareDisplay()

for the lastButton.actionPerformed() should end
}});
not
});

(I HATE AND NEVER USE ANONYMOUS ACTIONLISTENER)

textArea.append(printerList[i]);
The JTextArea.append() method expects a String as parameter not a Printer object

fram should be frame and it is NORTH, SOUTH, CENTER not North, Sounth, Center
frame.add(logoPanel, BorderLayout.North);
fram.add(buttonPanel, BroderLayout.South);
fram.addCenterPanel, BorderLayout.Center);

Ouf
If I do }}); to end my last button then when I click my previous button it won't go back to the first product.I have not added the extra buttons yet because I was trying to break my program up in pieces. The instructions for this assignment were:
Add navigation buttons (First, Previous, Next and Last) to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should be displayed. If the last item is displayed and the user clicks on the Next button, the first item should displayed. Note: Only, the first item in the inventory should be displayed when the application starts up. The remaining items in the inventory should be accessible only via the navigation buttons.

So if I close the last button with the }}); wouldn't that stop me from using the navigation buttons as per the requirements?
Was This Post Helpful? 0
  • +
  • -

#18 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Error message when I add Buttons to my program

Posted 29 June 2011 - 06:03 PM

Your actionPerformed() methods changes printerIndex to 0 or to

printerIndex = (printers.length - 3);

but then nothing else is done. Your program won't start, like by magic, to use that new printerIndex wherever it uses it.
In you actionPerformed(), after setting the new value to printerIndex, you will have to invoke code (probably a method) that refresh the GUI or display whatever using that new value contained into printerIndex.
Was This Post Helpful? 0
  • +
  • -

#19 ITStudent02   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 11

Re: Error message when I add Buttons to my program

Posted 30 June 2011 - 08:17 AM

I GOT IT!!! WHoo Hoo! OMG I am so excited lol. Thank you pbl you are awesome! ;)
Was This Post Helpful? 0
  • +
  • -

#20 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Error message when I add Buttons to my program

Posted 30 June 2011 - 08:27 PM

:)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2