GUI Array display one item
I need to be able to display one item at a time of an array into JTextFields. The way I'm trying to do it here gives me this error for each line where I try to add an array item's method to the display. Error: int cannot be dereferenced.
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.awt.event.*;
import java.lang.*;
publicclass Inventoryextends JFrame// Main class
{
private JLabel prodNameLabel;// name label
private JLabel numberLabel;// item number label
private JLabel unitLabel;// units in stock label
private JLabel priceLabel;// price each label
private JLabel featureLabel;// added faeture label
private JLabel valueLabel;// value of that item label
private JLabel rstkLabel;// cost to restock label
private JLabel totalLabel;// total value of inventory label
private JTextField prodNameField;// name display
private JFormattedTextField numberField;// item number display
private JFormattedTextField unitField;// units in stock display
private JFormattedTextField priceField;// price each display
private JTextField featureField;// added feature display
private JFormattedTextField valueField;// value of that item display
private JFormattedTextField rstkField;// cost to restock display
private NumberFormat numberFormat;// format field and parse numbers
private NumberFormat unitFormat;// format field and parse numbers
private NumberFormat priceFormat;// format field and parse numbers
private NumberFormat valueFormat;// format field and parse numbers
private NumberFormat rstkFormat;// format field and parse numbers
private JButton firstBtn;// first button
private JButton prevBtn;// previous button
private JButton nextBtn;// next button
private JButton lastBtn;// last button
private JPanel buttonJPanel;// JPanle to hold buttons
private JPanel fieldJPanel;// JPanel to hold labels and displays
private JPanel fontJPanel;// JPanel to display logo
privateint currProd;// current product display to use for button action
privateint i;// iterator
privatedouble total = 0;// variable for total inventory
public Inventory()// create class and method to perform GUI build
{
initComponents();
}
privatevoid initComponents()
{
// create label names
prodNameLabel =new JLabel("Product Name:");
numberLabel =new JLabel("Item Number:");
unitLabel =new JLabel("In Stock:");
priceLabel =new JLabel("Each Item Cost:");
featureLabel =new JLabel("Type of Item:");
valueLabel =new JLabel("Value of Item Inventory:");
rstkLabel =new JLabel("Cost to Re-Stock Item:");
totalLabel =new JLabel("Total Value of Inventory:$"+total);
// create button names
firstBtn =new JButton("First");
prevBtn =new JButton("Previous");
nextBtn =new JButton("Next");
lastBtn =new JButton("Last");
// create textFields and set uneditable
prodNameField =new JTextField(15);
prodNameField.setEditable(false);
numberField =new JFormattedTextField(numberFormat);
numberField.setEditable(false);
numberField.setColumns(10);
unitField =new JFormattedTextField(unitFormat);
unitField.setEditable(false);
unitField.setColumns(10);
priceField =new JFormattedTextField(priceFormat);
priceField.setEditable(false);
priceField.setColumns(10);
featureField =new JTextField(15);
featureField.setEditable(false);
valueField =new JFormattedTextField(valueFormat);
valueField.setEditable(false);
valueField.setColumns(10);
rstkField =new JFormattedTextField(rstkFormat);
rstkField.setEditable(false);
rstkField.setColumns(10);
// add buttons to panel
JPanel buttons =new JPanel();// set up panel
buttons.setLayout(new GridLayout(1, 4));//set layout
// add buttons to buttons
buttons.add(firstBtn);
buttons.add(prevBtn);
buttons.add(nextBtn);
buttons.add(lastBtn);
// Instantiate a product object
ProductAdd[] nwProduct;
nwProduct =new ProductAdd[5];
// Instantiate objects for the array
for (currProd = 0; currProd < 5; currProd++)
{
nwProduct[0] =new ProductAdd("Paper", 101, 10, 1.00,"Box");
nwProduct[1] =new ProductAdd("Pen", 102, 10, 0.75,"Pack");
nwProduct[2] =new ProductAdd("Pencil", 103, 10, 0.50,"Pack");
nwProduct[3] =new ProductAdd("Staples", 104, 10, 1.00,"Box");
nwProduct[4] =new ProductAdd("Clip Board", 105, 10, 3.00,"Two Pack");
}
for (currProd = 0; currProd < 5; currProd++)
total += nwProduct[currProd].calcValue();// calculate total inventory cost
// add values to textFields
prodNameField.setText(currProd.getProdName());
numberField.setValue(new Integer(currProd.getItmNumber()));
unitField.setValue(new Integer(currProd.getUnits()));
priceField.setValue("$"+new Double(currProd.getPrice()));
featureField.setText(currProd.getFeature());
valueField.setValue("$"+new Double(currProd.calcValue()));
rstkField.setValue("$"+new Double(currProd.calcValueRstk()));
// add labels to panel
JPanel labels =new JPanel();
labels.setLayout(new GridLayout(0, 1));
labels.add(prodNameLabel);
labels.add(numberLabel);
labels.add(unitLabel);
labels.add(priceLabel);
labels.add(featureLabel);
labels.add(valueLabel);
labels.add(rstkLabel);
labels.add(totalLabel);
// add fields to panel
JPanel fields =new JPanel();
fields.setLayout(new GridLayout(0, 1));
fields.add(prodNameField);
fields.add(numberField);
fields.add(unitField);
fields.add(priceField);
fields.add(featureField);
fields.add(valueField);
fields.add(rstkField);
//pair label and fields
prodNameLabel.setLabelFor(prodNameField);
numberLabel.setLabelFor(numberField);
unitLabel.setLabelFor(unitField);
priceLabel.setLabelFor(priceField);
featureLabel.setLabelFor(featureField);
valueLabel.setLabelFor(valueField);
rstkLabel.setLabelFor(rstkField);
// add labels and fields to panel
// labels left, fields right
JPanel display =new JPanel();
display.add(labels, BorderLayout.CENTER);
display.add(fields, BorderLayout.LINE_END);
// create display frame
JFrame invFrame =new JFrame();
invFrame.setLayout(new BorderLayout());
invFrame.setTitle("Office Min Inventory");
invFrame.add(fontJPanel, BorderLayout.NORTH);
invFrame.add(buttons, BorderLayout.SOUTH);
invFrame.add(display, BorderLayout.CENTER);
invFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// termination command
invFrame.setSize(800, 800);// set size of JPanel
invFrame.setLocationRelativeTo(null);// set screem location
invFrame.setVisible(true);// display window
firstBtn.addActionListener(new ActionListener()// first button listener
{
publicvoid actionPerformed(ActionEvent evt)
{
goFirst();
}
});
lastBtn.addActionListener(new ActionListener()// last button listener
{
publicvoid actionPerformed(ActionEvent evt)
{
goLast();
}
});
prevBtn.addActionListener(new ActionListener()// previous button listener
{
publicvoid actionPerformed(ActionEvent evt)
{
goBack();
}
});
nextBtn.addActionListener(new ActionListener()// next button listener
{
publicvoid actionPerformed(ActionEvent evt)
{
goNext();
}
});
}
privatevoid goFirst()// first button action method
{
currProd = 0;
prodNameField.setText(currProd.getProdName());
numberField.setValue(new Integer(currProd.getItmNumber()));
unitField.setValue(new Integer(currProd.getUnits()));
priceField.setValue("$"+new Double(currProd.getPrice()));
featureField.setText(currProd.getFeature());
valueField.setValue("$"+new Double(currProd.calcValue()));
rstkField.setValue("$"+new Double(currProd.calcValueRstk()));
}
privatevoid goLast()// last button action method
{
currProd = 4;
prodNameField.setText(currProd.getProdName());
numberField.setValue(new Integer(currProd.getItmNumber()));
unitField.setValue(new Integer(currProd.getUnits()));
priceField.setValue("$"+new Double(currProd.getPrice()));
featureField.setText(currProd.getFeature());
valueField.setValue("$"+new Double(currProd.calcValue()));
rstkField.setValue("$"+new Double(currProd.calcValueRstk()));
}
privatevoid goBack()// previous button action method
{
if(currProd > 0)
{
currProd--;
prodNameField.setText(currProd.getProdName());
numberField.setValue(new Integer(currProd.getItmNumber()));
unitField.setValue(new Integer(currProd.getUnits()));
priceField.setValue("$"+new Double(currProd.getPrice()));
featureField.setText(currProd.getFeature());
valueField.setValue("$"+new Double(currProd.calcValue()));
rstkField.setValue("$"+new Double(currProd.calcValueRstk()));
}
}
privatevoid goNext()// next button action method
{
if(currProd < 4)
{
currProd++;
prodNameField.setText(currProd.getProdName());
numberField.setValue(new Integer(currProd.getItmNumber()));
unitField.setValue(new Integer(currProd.getUnits()));
priceField.setValue("$"+new Double(currProd.getPrice()));
featureField.setText(currProd.getFeature());
valueField.setValue("$"+new Double(currProd.calcValue()));
rstkField.setValue("$"+new Double(currProd.calcValueRstk()));
}
}
//main method begins execution of java application
publicstaticvoid main(String args[])
{
Inventory inventory =new Inventory();
}// end main method
}// end class Inventory
class Product
{
protected String prodName;// name of product
protectedint itmNumber;// item number
protectedint units;// number of units
protecteddouble price;// price of each unit
protecteddouble value;// value of total units
public Product(String name,int number,int unit,double each)// Constructor for class Product
{
prodName = name;
itmNumber = number;
units = unit;
price = each;
}// end constructor
publicvoid setProdName(String name)// method to set product name
{
prodName = name;
}
public String getProdName()// method to get product name
{
return prodName;
}
publicvoid setItmNumber(int number)// method to set item number
{
itmNumber = number;
}
publicint getItmNumber()// method to get item number
{
return itmNumber;
}
publicvoid setUnits(int unit)// method to set number of units
{
units = unit;
}
publicint getUnits()// method to get number of units
{
return units;
}
publicvoid setPrice(double each)// method to set price
{
price = each;
}
publicdouble getPrice()// method to get price
{
return price;
}
publicdouble calcValue()// method to set value
{
return units * price;
}
}// end class Product
class ProductAddextends Product
{
private String feature;// variable for added feature
public ProductAdd(String name,int number,int unit,double each, String addFeat)
{
// call to superclass Product constructor
super(name, number, unit, each);
feature = addFeat;
}// end constructor
publicvoid setFeature(String addFeat)// method to set added feature
{
feature = addFeat;
}
public String getFeature()// method to get added feature
{
return feature;
}
publicdouble calcValueRstk()// method to set value and add restock fee
{
return units * price * 0.05;
}
}// end class ProductAdd
class FontJPanelextends JPanel
{
// display welcome message
publicvoid paintComponent( Graphics g )
{
super.paintComponent( g );//call superclass's paintComponent
// set font to Monospaced (Courier), italic, 12pt and draw a string
g.setFont(new Font("Monospaced", Font.ITALIC, 12 ) );
g.drawString("min", 90, 70 );
// set font to Serif (Times), bold, 24pt and draw a string
g.setColor( Color.RED );
g.setFont(new Font("Serif", Font.BOLD, 24 ) );
g.drawString("OFFICE", 60, 60 );
}// end method paintComponent
}// end class FontJPanel

