CSE 1341LabPacket10.docx

looneyvillestaticSoftware and s/w Development

Aug 15, 2012 (5 years and 29 days ago)

224 views


CSE 1341


Lab Packet
10

Due:

Implementation


April 14
, 2008 @ 6:00 p.m.



Overview:

This project will have you create a simple GUI program that will iterate through a list of items in
inventory. Here is an example of what the final GUI:


To create
the GUI, we will use the built
-
in GUI editor in NetBeans. This is an extremely powerful and
useful feature. It allows you to graphically lay out buttons, text fields, or any other GUI component. It
also streamlines the process of event handling.


Step 1
: Inventory Item Class

Create a new Java project in NetBeans.

Before you get started with the GUI part of this project, implement a class named InventoryItem. An
inventory item object is represented by three instance variables: item sku (an int), item

name, (a string),
and item unit price (a double). In the class, provide accessor and mutator methods for each of the
instance variables. Implement a no
-
argument constructor that initialized sku and price to 0 and name to
the empty string (“”


open/close

quotes with no spaces). Also implement a constructor that accepts
three arguments, one for each instance variable, and stores them in the appropriate instance variable.


Step 2


Create a Form

a.

Create an JPanel in Netbeans by choosing File|New File | Swin
g GUI Forms


then choose
JPanel
Form

on the right.

b.

Click Next. Give the panel a name such as InventoryItemDisplayPanel.

c.

Layout the panel similar to the example above using the controls in the control palette. You may
modify the layout if you wish.

d.

Righ
t click on each Control and choose Change Variable Name. Give each control a reasonable
name.

e.

Change the text on each control to something similar to example screen capture above.

Step 3


Create Main Application Driver

a.

Choose File | New File | Java Cat
egory


Choose Empty Java File from File Types.

b.

Click
Next

and give the file a valid Class Name such as InventoryApplication. In this file just created,
we will instantiate a JFrame component, a component created in STEP 2 above, and make it visible.

c.

In
this file, begin implementation of a class with only a main method. Here is some sample code
that you can modify

import javax.swing.*;


public class InventoryApplication {




public static void main(String [] args ) {


//Create a new JFrame r
eference variable and instantiate


//a JFrame object. Give the window a title.


JFrame jf = new JFrame("SMU Inventory");



//Declare and instantiate a JPanel object of the


//type created in STEP 2.


InventoryItemDis
play iid = new InventoryItemDisplay();




//Add the JPanel to the frame


jf.add(iid);



//Adjust the size of the frame


will require tweaking


jf.setSize(300, 250);



//Make the frame visible


jf.setVisible(tru
e);



//Make sure the app closes when we close the window/frame


jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

}


You should now have three Java files in your NetBeans Project:



InventoryItem class



Panel class



Inventory
Application class


Step 4


Creating Inventory Items in the Panel

When viewing the JPanel designer, you can switch back and forth between the designer view and the
source code view of your Panel (or other container). When you place components on the panel
,
NetBeans is actually “writing” the code for you behind the scenes


pretty handy if you ask me.

Remember the goal here
: Whenever we click the next button, it will display the next item in the list of
items that we have in our list. So we need to keep a

list of inventory items. We’ll use an
ArrayList

for
this.


a.

Switch to the source code view. Typically, NetBeans automatically places all instance variables (in
this case all of the JComponents) at the BOTTOM of the class. Scroll down to the bottom of

the
code.

b.

Below the JComponent instance variable, add an
ArrayList

of InventoryItem Objects, perhaps named
items
.

c.

Find the constructor for this class (How do you know a method is a constructor?). Inside the
constructor, initialize the ArrayList referen
ce variable with a new ArrayList that can hold Inventory
Item Objects.

d.

For debugging and testing purposes, it will be handy for the ArrayList to contain a few inventory
items. In the constructor, instantiate three Inventory Item objects
add them to the
ArrayList
i
tems
.
For instance, in my constructor, I added the following:


InventoryItem

temp = new InventoryItem(122, “Hammer”, 12.12);


items.add(temp);


temp =
ne
w InventoryItem(232, "Nail", 0.02
);


items.add(temp);


temp = new InventoryItem(111, “wire”
, 0.55);


items.add(temp);


e.

If you think about it, whenever we click the Next Button, we will have to know which item is
currently being displayed in order to determine the next object to display. So we will need an
additional instance variable in the pane
l that holds the index of where the currently displayed item
is stored in our ArrayList. Add an instance variable named
current
Displayed.
In the constructor,
initialize current to zero.

If we need to ever know how many elements are in the ArrayList, we c
an
use the size() method.


Now we
can track

the number of inventory items we are tracking as well as the one currently being
displayed.


Step 5


Making the “Next” button Do something

I’m going to assume for the purposes of this explanation that your JButton component has the name
btnNext
.

a.

Switch back to the design view of your class. Click the btnNext on the form. On the right are the
button properties.

Click on Events to bring up t
he events list.


b.


Click in the area next to actionPerformed. It will automatically be populated with an event handler
name.

c.

Hit enter and it will bring you to the source code view with the cursor positioned inside a new
method. This will be where we
handle events for the actionPerformed event for this particular
button.

d.

Inside this method, using instance variable
current
, set the text value of each of the appropriate
textFields to the value from the current object in the ArrayList.

e.

Increment the va
lue of the current instance variable.

f.

At some point,
current

will need to be reset to 0. When? Code this to happen appropriately.

Now run your application again. Does the next button work?

Step 6


adding a New Item

Your task is to make the Add Item

Button work. The goal is to elicit information from the user on the
SKU, name, and unit price of an inventory item and add that item to the ArrayList. To elicit information
from the user, you may use the JOptionPane class.



What you need to submit

Yo
u need to submit the following items to blackboard:



Zip file of your project directory. Submit the entire directory to BlackBoard.



Activity Diagram for adding new item to database



Activity Diagram for the next button



Complete Class diagram for Inventory
Item


Grading Rubric


Total Possible Points


35 Points


Design


10 points



2 Activity diagrams



class diagram for inventory item

Implementation


20 points



complete implementation of class Inventory Item



implementation of JPanel GUI class



implementation
of main driver application class

Source Documentation


5 points



consistent source code formatting



proper and sufficient file, method, and inline comments


Do a good job on this assignment as you will extend this code in Lab Packet 11.