Hello everyone. I am working on this assignment:
Modify the Inventory Program by adding a button 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
display. If the last item is displayed and the user clicks on the Next button, the first item
should display.
· Add a company logo to the GUI using Java graphics classes.
I have added the buttons, but I'm not sure how to get my existing array to display in the GUI. I also wanted to know if I have to assign event handlers and listeners and create code whether the fields are editable or non editable if I am only displaying information from an existing array? I read my textbook thoroughly, but it doesn't say. Here is all my codes for this project. Any help would be greatly appreciated!
CODE
/*
* TestProductUI.java
*
* Created on November 29, 2008, 9:50 AM
*/
package my.testproduct;
import testproduct.Subclass;
/**
*
* @author Administrator
*/
public class TestProductUI extends javax.swing.JFrame {
/** Creates new form TestProductUI */
public TestProductUI() {
initComponents();
}
private Subclass [] items; //The inventory
private int index; //An index to keep track of the TestProduct to display
private void handleFirstButton() {
//Set the index to the first element in the array
index = 0;
//Update and disable modification
}
private void handlePreviousButton() {
//Decrement the index
index--;
//If index is less than 0, wrap around to the last element of the array
if ( index < 0 )
{
index = items.length - 1;
}//end if
}//end method handlePreviousButton
private void handleNextButton()
{
//Increment the index
index++;
//If index exceeds the last valid array, wrap around to the first element of the array
if ( index > items.length - 1)
{
index = 0;
}//End if
}//End method handleNextButton
private void handleLastButton()
{
//Set the index to the last book in the array
index = items.length - 1;
}//End method handleLastButton
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
mainPanel = new javax.swing.JPanel();
nameLabel = new javax.swing.JLabel();
itemNumLabel = new javax.swing.JLabel();
unitsInStockLabel = new javax.swing.JLabel();
valueLabel = new javax.swing.JLabel();
actorLabel = new javax.swing.JLabel();
totalValueLabel = new javax.swing.JLabel();
nameText = new javax.swing.JTextField();
itemNumText = new javax.swing.JTextField();
unitsInStockText = new javax.swing.JTextField();
valueText = new javax.swing.JTextField();
actorText = new javax.swing.JTextField();
totalValueText = new javax.swing.JTextField();
firstButton = new javax.swing.JButton();
previousButton = new javax.swing.JButton();
nextButton = new javax.swing.JButton();
lastButton = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
logoLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "DvD Inventory", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 12))); // NOI18N
nameLabel.setText("Title and Price:");
itemNumLabel.setText("Item Number:");
unitsInStockLabel.setText("Units in Stock:");
valueLabel.setText("Value:");
actorLabel.setText("Actor:");
totalValueLabel.setText("Total Inventory Value:");
totalValueText.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
totalValueTextActionPerformed(evt);
}
});
firstButton.setText("First");
firstButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
firstButtonActionPerformed(evt);
}
});
previousButton.setText("Previous");
previousButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
previousButtonActionPerformed(evt);
}
});
nextButton.setText("Next");
nextButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextButtonActionPerformed(evt);
}
});
lastButton.setText("Last");
lastButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lastButtonActionPerformed(evt);
}
});
logoLabel.setText("Myles Singleton Inc.");
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(logoLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(57, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(logoLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
.addContainerGap())
);
org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainPanelLayout.createSequentialGroup()
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainPanelLayout.createSequentialGroup()
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, actorLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, valueLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, unitsInStockLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, itemNumLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, nameLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, totalValueLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(actorText)
.add(valueText)
.add(unitsInStockText)
.add(itemNumText)
.add(nameText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, totalValueText)))
.add(mainPanelLayout.createSequentialGroup()
.add(firstButton)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(previousButton)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(nextButton)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(lastButton))))
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(98, Short.MAX_VALUE))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(nameLabel)
.add(nameText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(itemNumLabel)
.add(itemNumText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(unitsInStockLabel)
.add(unitsInStockText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(valueLabel)
.add(valueText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(actorLabel)
.add(actorText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(totalValueLabel)
.add(totalValueText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(18, 18, 18)
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(firstButton)
.add(previousButton)
.add(nextButton)
.add(lastButton))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(22, Short.MAX_VALUE))
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {
if( evt.getSource() == previousButton ) //|< | pressed
{
handlePreviousButton();
}
}
private void lastButtonActionPerformed(java.awt.event.ActionEvent evt) {
if( evt.getSource() == lastButton ) //|>>| pressed
{
handleLastButton();
}//End else if
}
private void totalValueTextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void firstButtonActionPerformed(java.awt.event.ActionEvent evt) {
if( evt.getSource() == firstButton ) //|<<| pressed
{
handleFirstButton();
}
}
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
if( evt.getSource() == nextButton ) //| >| pressed
{
handleNextButton();
}//End if
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestProductUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel actorLabel;
private javax.swing.JTextField actorText;
private javax.swing.JButton firstButton;
private javax.swing.JLabel itemNumLabel;
private javax.swing.JTextField itemNumText;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton lastButton;
private javax.swing.JLabel logoLabel;
private javax.swing.JPanel mainPanel;
private javax.swing.JLabel nameLabel;
private javax.swing.JTextField nameText;
private javax.swing.JButton nextButton;
private javax.swing.JButton previousButton;
private javax.swing.JLabel totalValueLabel;
private javax.swing.JTextField totalValueText;
private javax.swing.JLabel unitsInStockLabel;
private javax.swing.JTextField unitsInStockText;
private javax.swing.JLabel valueLabel;
private javax.swing.JTextField valueText;
// End of variables declaration
}
CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testproduct;
/**
*
* @author Administrator
*/
import java.text.DecimalFormat;//imports decimal formatter
import javax.swing.JFrame;
public class TestProduct {
/**
* @param args the command line arguments
*/
static DecimalFormat formatter = new DecimalFormat("$##,###.00");//
public static void main(String[] arg)
{
Subclass item1 = new Subclass("Indiana Jones", 23457, 12, 14.99, "Harrison Ford");//assigns values to items
Subclass item2 = new Subclass("The Hulk", 67842, 17, 14.99, "Edward Norton"); //assigns values to items
Subclass item3 = new Subclass("Iron Man", 77089, 10, 14.99, "Robert Downey JR."); //assigns values items
Subclass item4 = new Subclass("Spiderman 3", 22547, 8, 10.99, "Tobey Maguire"); //assigns values to items
Subclass item5 = new Subclass ("Superman Returns", 40439, 11, 11.99, "Brandon Routh");//assigns values to items
Subclass item6 = new Subclass("Spiderwick Chronicles", 50459, 9, 12.99, "Freddie Highmore"); //assigns values to items
Subclass item7 = new Subclass ("Kung Fu Panda", 47865, 14, 17.99, "Jack Black"); //assigns values to items
Subclass item8 = new Subclass ("Speed Racer", 23969, 6, 15.99, "Emile Hirsch");//assigns values to items
Subclass item9 = new Subclass ("Get Smart", 60571, 18, 19.99, "Steve Carrell"); //assigns values to items
Subclass item10 = new Subclass ("Batman The Dark Knight", 18590, 19, 19.99,"Christian Bale");//assigns values to items
Subclass items = new Subclass();//creates object items
items.addProduct(item1);
items.addProduct(item2);
items.addProduct(item3);
items.addProduct(item4);
items.addProduct(item5);
items.addProduct(item6);
items.addProduct(item7);
items.addProduct(item8);
items.addProduct(item9);
items.addProduct(item10);
items.printInventory();//prints inventory list
}
}
CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testproduct;
/**
*
* @author Administrator
*/
public class Subclass extends Product
{
protected String movieActor;
public Subclass(String productName, int itemNum, int unitsInStock, double unitPrice, String actor)
{
// Pass on the values needed for creating the Product class
super(productName, itemNum, unitsInStock, unitPrice);
movieActor = actor;
}
public Subclass()
{
productName = " ";
itemNum = 0;
unitsInStock = 0;
unitPrice = 0;
movieActor = " ";
}
public void setActor(String strMovieActor)
{
movieActor = strMovieActor;
}
public String getActor()
{
return movieActor;
}
@Override
public double productValue()
{
return super.productValue() * 1.05;
}
public double getRestockingFee()
{
return super.productValue() * .05;
}
@Override
public String toString()
{
return super.toString() + " Actor:" + movieActor;
}
}
CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testproduct;
/**
*
* @author Administrator
*/
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Product
{
int inventorySize = 15;
protected Product items[] = new Product[inventorySize];
DecimalFormat formatter = new DecimalFormat("$##,###.00");
public void addProduct(Product item) {
for (int i = 0; i < inventorySize; i++) {
if (items[i] == null) {
items[i] = item;
return;
}
}
}
public double totalInvValue(Product items[])
{
double sumOfInventory = 0;
for (Product item : items)
{
if (item != null){sumOfInventory += item.productValue();}
}
return sumOfInventory;
}
public void printInventory()
{
for (Product item : items)
{
String str = item.toString() + " Item Number:" + item.getItemNum();
str += " Units In Stock:" + item.getUnitsInStock();
str += " Value:" + formatter.format(item.productValue())+ "\n Total inventory is:" + formatter.format(totalInvValue(items));
JOptionPane.showMessageDialog(null, str, "DvD Inventory", JOptionPane.PLAIN_MESSAGE);
}
}
protected String productName;
protected int itemNum;
protected int unitsInStock;
protected double unitPrice;
public Product(String dvd, int number, int stock, double price)
{
productName = dvd;
itemNum = number;
unitsInStock = stock;
unitPrice = price;
}
public Product()
{
productName = " ";
itemNum = 0;
unitsInStock = 0;
unitPrice = 0;
}
// String representation of the product
@Override
public String toString()
{
return productName + " Price:" + unitPrice;
}
public double productValue()
{
return (double)unitsInStock * unitPrice;
}
public String getproductName()
{
return productName;
}
public void setProductName(String strProductName)
{
productName = strProductName;
}
public void setUnitPrice(double strUnitPrice)
{
unitPrice = strUnitPrice;
}
public double getUnitPrice()
{
return unitPrice;//retrieves unit price
}
public void setUnitsInStock(int strUnitsInStock)
{
unitsInStock = strUnitsInStock;//sets product name
}
public int getUnitsInStock()
{
return unitsInStock;//retrieves units in stock
}
public void setItemNum(int strItemNum)
{
itemNum = strItemNum;//sets product name
}
public int getItemNum()
{
return itemNum;//retrieves item number
}
public static void sortItems(Product items[])
{
String test, itemName;
Product prodTemp;
for(int i=0; i<items.length; i++)
{
test = items[i].toString();
for(int j=i; j<items.length; j++)
{
itemName = items[j].toString();
if (itemName.compareToIgnoreCase(test) < 0)
{
prodTemp = items[i];
items[i] = items[j];
items[j] = prodTemp;
}
}
}
}
}