Re: My buttons will not work
I took a look at your code. I strongly recommend that you download an IDE such as Eclipse or NetBeans that will check your spelling. This is why I use Eclipse. (I couldn't spell to save my life) Other than spelling, there were not too many noticable problems.
You were right, you were missing an import (ActionEvent).
Also you are never actually declaring the printerIndex variable in your action listeners. You have to give it a type when you declare it.
Last thing for now. You dont have a prepareDisplay() method.
Re: My buttons will not work
I lied.
Code :
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;
}
You don't pass in a restockFee parameter. You are assigning this.restockFee to one of 2 things: 1) null or 2) restockFee which isn't anything at this point in the code.
--Jams
Re: My buttons will not work
Quote:
I keep getting error's.
When you get errors please copy and paste here the full text of the error message.
Re: My buttons will not work
Thank you for your reply. First I must say that I actually uninstalled Eclipse and Netbeans because we are using TextPad and I failed my first attempt at this class because I spend too much time trying to learn those other IDE's. I did make some changes to my program but first: very confused on your last post about the restock fee I put:
public double restockFee()
{
return unitPrice * .05;
}
Before I added the buttons my program did do everything it was suppose to, including calculate in the restock fee for each printer. I got 100% on that assignment actually. However, I am suppose to modify the program to add buttons. The First, last, previous and next buttons and also add a logo of my choice. I got my program to compile and run, unfortunately my buttons do absolutely nothing. I only added the First, last and logo for now because I am trying to break up the code in pieces. Once I get the first two buttons and logo working it will be easy to reuse the code and add the others. I'm not exactly sure where my problem is at his point but here is my code if you would not mind reviewing it for me one more time. Thank you again for your response it is greatly appreciated.
Quote:
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[] printerList = new Printer[5];
printerList[0] = brother;
printerList[1] = canon;
printerList[2] = dell;
printerList[3] = hp;
printerList[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 = (printerList.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 < printerList.length; i++ )
{
textArea.append("\n"+ printerList[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_CLOS E);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}//end main
}//end Iventory class
Re: My buttons will not work
Quote:
unfortunately my buttons do absolutely nothing.
What are they supposed to do? I see some assignment statements in the listener code.
Try debugging your code by adding some printlns to the actionPerformed methods to show that they are being executed.
BTW Please wrap you code in code tags not quote tags.
Re: My buttons will not work
I tried but for some reason I have not icon to wrap my program in. The big X right? Where did it go?
Re: My buttons will not work
Code tags are in the Go Advanced area, the # icon.
Or just add them manually.
Re: My buttons will not work
Sorry, dah!!! (On Information overload right now, sorry) When the GUI window appears, the First printer ("brother printer" since it is in assorted array (alphabetical order)) should be displayed. The first, last, previous and next buttons should allow the user to navigate through the printers. I don't see how I am missing anything at this point (except of course the next and previous buttons I didn't add yet.)
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Arrays;
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[] printerList = new Printer[5];
printerList[0] = brother;
printerList[1] = canon;
printerList[2] = dell;
printerList[3] = hp;
printerList[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 = (printerList.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 < printerList.length; i++ )
{
textArea.append("\n"+ printerList[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
Here is my printer class:
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());
}
}
Sorry about that.
Re: My buttons will not work
So, is the program working ok for you now?
If not please explain.
Re: My buttons will not work
No, its not working. I have two buttons that appear in the GUI window but nothing else display's in the window. I need each printer to appear through the navigation of the buttons.
Code :
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[] printerList = new Printer[5];
printerList[0] = brother;
printerList[1] = canon;
printerList[2] = dell;
printerList[3] = hp;
printerList[4] = lexmar;
Notice these are all names of printers the brother, canon, dell, hp, lexmar. When the program in run I need the first printer to display as such:
Quote:
Item Number: P1101
Product Name: Brother Inkjet Multi-Function Printer
Price: $217.60
Total Value: $2611.20
Serial Number: BroM200
Restock Fee: $10.88
Then when the NEXT button is clicked I need it to display the next printer ("Canon") and its Item number, product name, etc. And I need the program to navigate through the inventory as long as the buttons are pushed. As of right now I am having a problem with the buttons. They do not work. When Take the buttons and the logo code out of my program, all my inventory displays in one big window. I need each printer to display only when the buttons are clicked. I hope you understood that. :(~
Re: My buttons will not work
Quote:
when the NEXT button is clicked I need it to display the next printer
Where is the data to be displayed?
Re: My buttons will not work
Re: My buttons will not work
Sorry, I meant to say: where will the user see the data when it is displayed.
Re: My buttons will not work
Re: My buttons will not work
I see that this is going to take a long time.
Where on the GUI window?
1 Attachment(s)
Re: My buttons will not work
Attachment 635
I have attached an example of how it should display. The buttons at the botton (SOUTH) the logo center top(NORTH) and the text should be in the middle(CENTER).
Re: My buttons will not work
Sorry, I don't download .doc files. Make it an image.
What are you waiting for to make the needed changes to your code for it to display what you want?
Re: My buttons will not work
I was trying to get some direction or maybe some advice on where I might be going wrong with my program. But your right, this is taking too long. Thank you and have a nice day.
Re: My buttons will not work
You haven't designed a GUI with a slot to display things. You need a design BEFORE you write the code.
Look at the panels you now have and where they are being positioned to be displayed.
One set of GUI overlays another one. Go back and work on the GUI design.
Re: My buttons will not work
Is this a real forum or am I speaking to a computer? I do not understand what you are saying: isn't this my GUI WINDOW?
Code :
//initialize the GUI window and set the parameters
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(textArea));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Don't worry about it I will figure it out. Thanks anyway for time.
Re: My buttons will not work