Error message when I add Buttons to my program

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 ITStudent02  Icon User is offline

  • New D.I.C Head

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

Error message when I add Buttons to my program

Posted 28 June 2011 - 07:32 AM

I am a little unclear on how to add these buttons. I have read posts on this, tried various ways, but it's not what my instructor wants and I am actually confused about the logic being used for these buttons after reading other forums. I feel like there is a much easier way to accomplish this, but when I add my instructors logic I get 75 errors. Am I suppose to delete some of my code? Did I use the code in the wrong place? I was told not to use an extended class for these buttons as well, so that just confuses me even more. My instructor wants me to put these buttons in my main class. Which I did, but I am getting all these errors and can't fix them. here my code:

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

}
import javax.swing.*;
import java.awt.*;
import java.

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

	public static void main (String args[])
	{

		NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);


		//create inventory items in array
		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");

		final Printer[] printerList = new Printer[5];
		printerList[0] = brother;
		printerList[1] = canon;
		printerList[2] = dell;
		printerList[3] = hp;
		printerList[4] = lexmar;


		//calculate inventory total
		double totalInventory = totalInventory(printerList);


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

		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;
				prepareDisplay(printerList[printerIndex], textArea);
			}
		});

		JButton lastButton = new JButton("Last");
		buttonPanel.add(lastButton);
		lastButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				printerIndex = (printerList.length - 3);
				prepareDisplay(printerList[printerIndex], textArea);

		});

		JLabel logoLabel = new JLabel (new ImageIcon("CompanyLogo.jpg"));
		JPanel logoPanel = new JPanel();
		logoPanel.add(logoLabel);

			   JPanel centerPanel = new JPanel();
			   centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
			   centerPanel.add(prepareDisplay(printerList[printerIndex], textArea));



		//prepare the text that will be displayed in the GUI
		for (int i = 0; i < printerList.length; i++ )
		{
		   textArea.append(printerList[i]);
		}
		//initialize the GUI window and set the parameters
		JFrame frame = new JFrame();
		frame.setLayout(new BorderLayout());
		frame.add(logoPanel, BorderLayout.North);
		fram.add(buttonPanel, BroderLayout.South);
		fram.addCenterPanel, BorderLayout.Center);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);

		textArea.append("\nTotal Inventory: " + nf.format(totalInventory));

	}//end main
}//end Iventory class



Is This A Good Question/Topic? 0
  • +

Replies To: Error message when I add Buttons to my program

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Error message when I add Buttons to my program

Posted 28 June 2011 - 07:36 AM

Without seeing your errors, we are the blind leading the blind.

For this method, you may consider using the Comparator interface. You can define it to compare two Printers based on their productNames. Then pass that Comparator as the second param to the Arrays.sort() method, with the first param being the Printer[].
public static Printer[] sortArray(Printer[] printers)


Was This Post Helpful? 0
  • +
  • -

#3 ITStudent02  Icon 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 28 June 2011 - 07:45 AM

View Postmacosxnerd101, on 28 June 2011 - 07:36 AM, said:

Without seeing your errors, we are the blind leading the blind.

For this method, you may consider using the Comparator interface. You can define it to compare two Printers based on their productNames. Then pass that Comparator as the second param to the Arrays.sort() method, with the first param being the Printer[].
public static Printer[] sortArray(Printer[] printers)

I tried to post my errors but I can't seem to get all 75 errors to copy. How could I show you my errors without having to type each one out individually? Is there a way?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Error message when I add Buttons to my program

Posted 28 June 2011 - 07:46 AM

If you are using an IDE, you should be able to copy/paste them all? Are you getting the same error(s) repeatedly? If so, post a few of them and we'll see what we can do to tackle them. :)
Was This Post Helpful? 0
  • +
  • -

#5 ITStudent02  Icon 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 28 June 2011 - 08:07 AM

I am using TextPad and it won't let me copy and paste. I got errors on lines:
97 = illegal start of expression and ';' expected
102 = illegal start of expression }); (pointer at ) sign)
113 = illegal start type });
117 = identifier expected
120 = illegal start, identifier expected, illegal start type, ')'expected ';'expected (10 errors on this line alone)

121 = illegal start, identifier expected, same thing (9error messages)
126 = (10errors) identifier expected, >expected and ';' expected etc.
128
133
134
135
136
137
138
139
141
143
144
all have same type of errors I reached end of file wile parsing. (I can fix 144 that is an easy fix missing a bracket somewhere)but don't understand why I'm getting all the rest. Oh, and I tired to use your example and I got 80 error messages. I will tell you this, My program compiles and runs fine until I get to this line of code:
		JButton firstButton = new JButton("First");
		buttonPanel.add(firstButton);
		firstButton.addActionListener(new ActionListener());
Then I start getting errors. I thought maybe I forgot to add the action listener but I did this and got more errors. I don't what I am doing wrong at this point. BTW, thank you! ;)
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Error message when I add Buttons to my program

Posted 28 June 2011 - 08:22 AM

The line numbers in your error messages don't really correspond to those in the code you posted.

Some of your problems seem to lie here. Remove the ); at the end of this section firstButton.addActionListener(new ActionListener());. That way, the anonymous inner class declaration becomes legal.
firstButton.addActionListener(new ActionListener());
	        {
	            public void actionPerformed(ActionEvent e)
	            {
	                printerIndex = 0;
	                prepareDisplay(printerList[printerIndex], textArea);
	            }
	        });


Was This Post Helpful? 0
  • +
  • -

#7 ITStudent02  Icon 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 28 June 2011 - 08:39 AM

View PostITStudent02, on 28 June 2011 - 07:45 AM, said:

View Postmacosxnerd101, on 28 June 2011 - 07:36 AM, said:

Without seeing your errors, we are the blind leading the blind.

For this method, you may consider using the Comparator interface. You can define it to compare two Printers based on their productNames. Then pass that Comparator as the second param to the Arrays.sort() method, with the first param being the Printer[].
public static Printer[] sortArray(Printer[] printers)

I tried to post my errors but I can't seem to get all 75 errors to copy. How could I show you my errors without having to type each one out individually? Is there a way?


Sorry, I added my printer class that's why. Here is where my error's are:
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Arrays;
import javax.swing.*;
import java.awt.*;
import java.ActionListener;

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

	public static void main (String args[])
	{

		NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);


		//create inventory items in array
		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");

		final Printer[] printerList = new Printer[5];
		printerList[0] = brother;
		printerList[1] = canon;
		printerList[2] = dell;
		printerList[3] = hp;
		printerList[4] = lexmar;


		//calculate inventory total
		double totalInventory = totalInventory(printerList);


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

		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;
				prepareDisplay(printerList[printerIndex], textArea);
			}
		});

		JButton lastButton = new JButton("Last");
		buttonPanel.add(lastButton);
		lastButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				printerIndex = (printerList.length - 3);
				prepareDisplay(printerList[printerIndex], textArea);

		}

		JLabel logoLabel = new JLabel (new ImageIcon("CompanyLogo.jpg"));
		JPanel logoPanel = new JPanel();
		logoPanel.add(logoLabel);

			   JPanel centerPanel = new JPanel();
			   centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
			   centerPanel.add(prepareDisplay(printerList[printerIndex], textArea));



		//prepare the text that will be displayed in the GUI
		for (int i = 0; i < printerList.length; i++ )
		{
		   textArea.append(printerList[i]);
		}
		//initialize the GUI window and set the parameters
		JFrame frame = new JFrame();
		frame.setLayout(new BorderLayout());
		frame.add(logoPanel, BorderLayout.North);
		fram.add(buttonPanel, BroderLayout.South);
		fram.addCenterPanel, BorderLayout.Center);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);

		textArea.append("\nTotal Inventory: " + nf.format(totalInventory));

	}//end main
}//end Iventory class
I fixed some errors but I still have errors on lines: 114, 117, 118, 123, 125, 129, 130, 131, basically where all my buttons are which is why I can't get the buttons to work I guess. Would't this logic be easier:
 JButton firstButton = new JButton("First"); add(firstButton);
Why do I need to add all the public/void keywords? I don't understand the logic behind this format I guess. Could you help me understand. Maybe then I will be able to compile my program and fix those errors. Right now I am just really confused on the entire logic of this coding format that I am using.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Error message when I add Buttons to my program

Posted 28 June 2011 - 08:44 AM

Are you sure that code corresponds to the lines with the error messages? These are lines 114 and 117, and I don't see anything wrong with them. Also, please remember to post your revised errors as well.
logoPanel.add(logoLabel); //line 114
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS)); //line 117


Was This Post Helpful? 1
  • +
  • -

#9 ITStudent02  Icon 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 28 June 2011 - 08:54 AM

Oh, sorry. Ok I got it, I fixed most of the error's now I only have 4 error messages on one line. These are the error messages: not a statement and ';' expected
frame.addCenterPanel, BorderLayout.Center);


Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Error message when I add Buttons to my program

Posted 28 June 2011 - 08:55 AM

You are missing the ( for the add() method. You only have the end-parenthesis.
frame.add(CenterPanel, BorderLayout.CENTER);


Was This Post Helpful? 1
  • +
  • -

#11 ITStudent02  Icon 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 28 June 2011 - 09:02 AM

LOL, yeah I found that, then when I fixed it it gave me 10 error's. Do I need a package or a different import for the action Listener? Error says cant find symbol : class ActionListener
location : package java
import java.ActionListener;

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Error message when I add Buttons to my program

Posted 28 June 2011 - 09:03 AM

Check out the documentation. It is located in the java.awt.event package.
Was This Post Helpful? 1
  • +
  • -

#13 ITStudent02  Icon 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 28 June 2011 - 09:09 AM

yeah I saw that too. I don't understand what is wrong with this code. I will work on it some more and if I have more questions I will post it later. Thank you so much for your time and feedback. I appreciate it.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Error message when I add Buttons to my program

Posted 28 June 2011 - 09:10 AM

You have to change the import to reflect the full package name (java.awt.event) rather than just java.

Glad I could help! :)
Was This Post Helpful? 1
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Error message when I add Buttons to my program

Posted 28 June 2011 - 06:34 PM

would be easier if you put each of your classes between different :code:

import java.awt.event.*;

is missing from the class that uses ActionListener

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
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2