Sie sind auf Seite 1von 97

1

Tutorial 19 – Shipping Hub Application


Introducing Collections, ArrayList and Iterators

Outline
19.1 Test-Driving the Shipping Hub Application
19.2 Parcel Class
19.3 JList Component
19.4 Using Mnemonics (Keyboard Shortcuts)
19.5 Collections
19.6 Constructing the Shipping Hub Application
19.7 Using Iterators
19.8 Wrap-Up
2

Objectives

• In this tutorial, you will learn to:


– Create and manipulate an ArrayList object.
– Create a mnemonic for a component.
– Use an iterator to iterate through an ArrayList.
– Display items in a JList.
3
19.1 Test-Driving the Shipping Hub
Application
Application Requirements
A shipping company receives parcels at its shipping hub. Parcels will be
represented as Parcel objects, where class Parcel is provided for you. The
company then ships these Parcels to a distribution center in one of the
following states: Alabama, Florida, Georgia, Kentucky, Mississippi, North
Carolina, South Carolina, Tennessee, Virginia or West Virginia. The company
needs an application to track the Parcels that pass through its shipping hub.
When the user clicks the application’s Scan New JButton, the application
generates an ID and arrival time for the new Parcel. Once a Parcel has been
scanned, the user should be able to enter the recipient’s name and shipping
address for the Parcel. JButtons should be provided for the user to remove or
modify the information of Parcels that have already been scanned. The user
should be able to navigate through the list of scanned Parcels by using the <
Back or Next > JButtons. Finally, users should be able to view a list of all
Parcels destined for a particular state.
4
19.1 Test-Driving the Shipping Hub
Application (Cont.)
• Running the completed application
– Open a Command Prompt
• Change to ShippingHub directory
• Type java ShippingHub
5
19.1 Test-Driving the Shipping Hub
Application (Cont.)

Figure 19.1   Running the completed Shipping Hub application.

JPanel contains fields for user


to view or edit the Parcel JComboBoxes list
available states
6
19.1 Test-Driving the Shipping Hub
Application (Cont.)
Figure 19.2   Scanning a new Parcel.

User can now enter


Parcel information
7
19.1 Test-Driving the Shipping Hub
Application (Cont.)
Figure 19.3   Entering a new Parcel’s information.

New Parcel
listed in JList
8
19.1 Test-Driving the Shipping Hub
Application (Cont.)
Figure 19.4   Parcel information is editable when Edit JButton is clicked.

Fields are
made editable

Update JButton is
enabled
9
19.1 Test-Driving the Shipping Hub
Application (Cont.)
Figure 19.5   New information is stored when Update JButton is clicked.

Updated zip code


10
19.1 Test-Driving the Shipping Hub
Application (Cont.)
Figure 19.6   Next Parcel is displayed when Remove JButton is clicked.

When a Parcel is
removed, the next
Parcel is displayed
11
19.1 Test-Driving the Shipping Hub
Application (Cont.)
Figure 19.7   Viewing all Parcels going to South Carolina.

All Parcels being sent


to South Carolina
12

19.2 Parcel Class

• Store each Parcel’s shipping information.


– Each Parcel has a unique ID number.
• Multiple packages can be shipped to the same person at the
same address.
– Instance variables are made private
• recipient’s name, address, city, state and zip
code.
13

19.2 Parcel Class (Cont.)


Method Description
getName/setName Provides access to instance variable name (a
String).
getAddress/ Provides access to instance variable address (a
setAddress String).
getCity/setCity Provides access to instance variable city (a
String).
getState/- Provides access to instance variable state (a
setState String). States are represented as two-letter
abbreviations.
getZip/setZip Provides access to instance variable zip (an int).
getParcelID Provides access to instance variable parcelID (an
int).
getArrivalTime Provides access to instance variable arrivalTime
(a String).
Parcel Constructor that is used to create a new Parcel object
with two arguments parcelID (an int) and
arrivalTime (a String).
Figure 19.8 Methods listing for class Parcel.
14

19.3 JList Component

• Displays a series of items from which the user


may select one or more items.
– setListData method sets the items displayed in the
JList.
15

19.3 JList Component (Cont.)

Figure 19.9   Declaring new references to a JList and a JScrollPane.


16

19.3 JList Component (Cont.)

Figure 19.10   Creating a new JList object.

Initializing parcelStateJList
with a new JList
17

19.3 JList Component (Cont.)

Adding the parcelStateJList to


the JScrollPane
Figure 19.11   Declaring a new containing the JList.
parcelStateJScrollPane

• JScrollPane allows user to scroll through items if there are


more items in the list than the number of visible rows.
18

19.4 Using Mnemonics (Keyboard Shortcuts)

• Mnemonics allow users to perform an action on a


component using the keyboard.
• Specifying a mnemonic key
– Set the component’s mnemonic property
• Use setMnemonic method
• Virtual key code
19

19.4 Using Mnemonics (Keyboard Shortcuts)

Figure 19.12   Creating a mnemonic.

Setting the mnemonic


of scanNewJButton
to S

•Pressing Alt+S will have the same effect as if the user clicks the
JButton.
20

19.4 Using Mnemonics (Keyboard Shortcuts)

Figure 19.13   JButton with mnemonic property set to 'S'.

Mnemonic underlined
21

19.4 Using Mnemonics (Keyboard Shortcuts)

Figure 19.14   JButtons with mnemonics.


22

19.5 Collections

• Predefined classes
– Provide methods that make it easy for you to
store, organize and retrieve data
• ArrayList class
– Provides all of the capabilities of an array
– Dynamic resizing capabilities.
• enables an ArrayList object to vary its size.
23
19.6 Constructing the Shipping Hub
Application
When the Edit JButton is clicked
Enable the input components (nameJTextField, addressJTextField,
cityJTextField, zipJTextField and stateJComboBox)
When the Update JButton is clicked
Store the new name, address, city, state and zip code values in the Parcel object
Update the parcelStateJList based on the user’s changes
When the < Back JButton is clicked
Display the previous Parcel in the parcelsArrayList (or the last Parcel if the
current Parcel is the first in the parcelsArrayList)
When the Next > JButton is clicked
Display the next Parcel in the parcelsArrayList (or the first Parcel if the
current Parcel is the last in the parcelsArrayList)
When the user chooses a different state in the Parcels by State JComboBox
Iterate through each Parcel in the parcelsArrayList
Add IDs of Parcels destined for the selected state to the parcelStateArrayList
Display the Parcel iDs from the parcelStateArrayList in the parcelStateJList
24
19.6 Constructing the Shipping Hub
Application (Cont.)
Action Component/Class/Object Event/Method
Labeltheapplication’scomponents arrivedAtJLabel ,
parcelIDJLabel ,
nameJLabel ,
addressJLabel ,
cityJLabel ,
stateJLabel ,
zipJLabel ,
parcelInformationJPanel ,
parcelStateJPanel
Createparcels Array List tocontain all parcelsArrayList
Parcelsbeingsent
Createparcel State ArrayList to parcelStateArray List
containtheParcelsbeingsent toa
specificstate
DisplaythearrivaltimeandtheID arrivedAtJTextField , User clicks
num berofthenewParcel parcelIDJTextField Scan New
JButton
CreateaParcelobject forthenew newParcel
entry
Retrieveinput valuesfromtheuser nameJTextField , User clicks
addressJTextField , Add JButton
cityJTextField ,
zipJTextField ,
stateJComboBox
Figure 19.15   ACE table for the Shipping Hub application. (Part 1 of 4.)
25
19.6 Constructing the Shipping Hub
Application (Cont.)
Storeinput values inthenew newParcel ,
Parcel object parcelsArrayList
AddthenewParcel totheparcels - parcelsArrayList
ArrayList
AddtheParcel IDtothe parcelStateJList
parcelStateJList
RemovetheParcel IDfromthe parcelStateJList User clicks
parcelStateJList Remove
JButton
RemovetheParcel fromthe parcelsArrayList
parcels ArrayList
Retrievevalues fromthenext newParcel ,
Parcel parcelsArrayList
Displaythenext Parcel nameJTextField ,
addressJTextField ,
cityJTextField ,
zipJTextField ,
stateJComboBox
Figure 19.15   ACE table for the Shipping Hub application. (Part 2 of 4.)
26
19.6 Constructing the Shipping Hub
Application (Cont.)
Enabletheinput components nameJTextField , User clicks
addressJTextField , Edit JButton
cityJTextField ,
zipJTextField ,
stateJComboBox
Retrieveinput valuesfromthe nameJTextField , User clicks
user addressJTextField , Update
cityJTextField , JButton
zipJTextField ,
stateJComboBox
Storethenewinput valuesinthe newParcel ,
Parcelobject parcelsArrayList
UpdatetheparcelStateJList parcelStateJList
basedontheuser’schanges
Retrievevaluesfromtheprevious newParcel , User clicks
Parcel parcelsArrayList < Back
JButton
DisplaythepreviousParcelinthe nameJTextField ,
parcels ArrayList addressJTextField ,
cityJTextField ,
zipJTextField ,
stateJComboBox
Figure 19.15   ACE table for the Shipping Hub application. (Part 3 of 4.)
27
19.6 Constructing the Shipping Hub
Application (Cont.)
Retrievevalues fromthenext newParcel, User clicks
Parcel parcelsArrayList Next >
JButton
Display thenext Parcel in the nameJTextField,
parcelsArrayList addressJTextField,
cityJTextField,
zipJTextField,
stateJComboBox
Iteratethrough each Parcel in the parcelsArrayList User selects a
parcelsArrayList state in -
JComboBox
Add IDs of Parcels destined for parcelStateArray-
theselected state to theparcel- List
StateArrayList
Display theParcel iDs fromthe parcelStateJList
parcelStateArrayList in the
parcelStateJ List
Figure 19.15   ACE table for the Shipping Hub application. (Part 4 of 4.)
28
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.16   Importing class ArrayList.

Importing the java.util package

• The ArrayList class is located in java.util


29
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.17   Creating the ArrayList.

Creating the ArrayList


30
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.18   Entering information for a new Parcel.

Parcel’s arrival time and


ID not yet displayed

• Save the changes to your code


• Compile and run in the Command Prompt
31
19.6 Constructing the Shipping Hub
Application (Cont.)
• toString method of the Date class
– Returns a String that represents the Date
• String is in format: Tue Feb 13 16:50:00 EST 2003
32
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.19   Displaying the Parcel’s number and arrival time.

Displaying the arrival time


and Parcel’s ID number
in the JTextFields
33
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.20   Creating a Parcel object.

Create a new Parcel object


with an ID and arrival time
34
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.21   Adding a Parcel to the ArrayList.

Adding a Parcel object to the ArrayList

• Use ArrayList’s add method.


35
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.22   Newly scanned items have the Parcel’s arrival time and ID displayed.

Arrival time and ID now displayed


36
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.23   Adding a new Parcel with the Add JButton.

New Parcel not yet


added to JList

Panel information
is made uneditable
37
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.24   Removing a Parcel from the ArrayList.

Removing the current Parcel


from the ArrayList
• Use ArrayList’s remove method
•ArrayList updates its indices automatically
38
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.25   Adding a Parcel to parcelsArrayList.
39
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.26   Removing a Parcel from parcelsArrayList.
40
19.6 Constructing the Shipping Hub
Application (Cont.)
• To edit parcel information
– Click Edit JButton
• Enable all JTextFields, except for ID number and arrival
time
– Enter new information
– Click Update JButton to submit new information
41
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.27   Removing and inserting a Parcel to update data.

Updating the ArrayList with


the new Parcel’s information
42
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.28   Entering a new Parcel.
43
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.29   Modifying a Parcel after the Edit JButton is clicked.

City name is modified


44
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.30   Storing changes to a Parcel information
when the Update JButtonis clicked.

New city name is stored


in parcelsArrayList
45
19.6 Constructing the Shipping Hub
Application (Cont.)
• Displaying a Parcel after a removal
– When element removed is the last element in the
ArrayList
• Display first Parcel in ArrayList
– When element removed is the only element in the
ArrayList
• Clear application’s components
– Otherwise, display the next Parcel
46
19.6 Constructing the Shipping Hub
Application (Cont.)

Figure 19.31   Setting position to 0 (to display the first Parcel)

when the last Parcel in parcelsArrayList is removed.

Set position to display


first Parcel when last
Parcel is removed

Clear application’s components


when there are no more Parcels
47
19.6 Constructing the Shipping Hub
Application (Cont.)

Figure 19.32   Display next or first Parcel after a Parcel is removed.

Call method Parcel to


display next Parcel
48
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.33   Set position to the index of the
previous Parcel when < Back JButton is clicked.

Decrement position if
the current Parcel is
not the first Parcel

Otherwise, set position


to the final element
49
19.6 Constructing the Shipping Hub
Application (Cont.)

Figure 19.34   Calling the loadParcel


method
to display the previous Parcel’s information.
50
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.35   Set position to next Parcel when Next > JButton is clicked.

Increment position if
the current Parcel is
not the last Parcel

Otherwise, set position


to the first element
51
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.36   Calling the loadParcel method to display the next Parcel’s information.
52
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.37   Displaying the Parcel’s data in your application’s components.

Retrieving Parcel at index


position in parcelsArrayList

Display the data stored


in the Parcel object
53
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.38   Displaying the next Parcel using the Next > JButton.

• Save the changes to your code


• Compile and run in the Command Prompt
54
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.39   Displaying the previous Parcel using the < Back JButton.
55

19.7 Using Iterators

• Iterator is used to traverse through a collection


– Uses a control variable
– iterator method of ArrayList returns an Iterator
object
– Iterator methods:
• hasNext – returns true if there are any more elements
• next – returns a reference to the next object
56

19.7 Using Iterators (Cont.)

Figure 19.40   Declaring a variable to contain the state selected.

Obtain the state selected in


parcelStateJComboBox
57

19.7 Using Iterators (Cont.)

Figure 19.41   Declaring an Iterator.

Declaring the Iterator for the


while repetition statement
58

19.7 Using Iterators (Cont.)

Figure 19.42   Clearing parcelStateArrayList.

Removing all elements from


parcelStateArrayList

• clear method removes all Parcels from the ArrayList


59

19.7 Using Iterators (Cont.)

Figure 19.43   Iterator used in a while statement.

Using iterator in the while


condition
60

19.7 Using Iterators (Cont.)

Figure 19.44   Creating a reference using parcelIterator.next.

Retrieving the next Parcel in


the parcelIterator
61

19.7 Using Iterators (Cont.)

Figure 19.45   Adding all shipments going to selected state.

Adding the Parcel’s ID number


to parcelStateArrayList

• If state codes match, the Parcel is added to the ArrayList


62

19.7 Using Iterators (Cont.)

Figure 19.46   Displaying all Parcels going to the selected state.

Displaying the parcelID in


the parcelStateJList

• toArray method an array of Objects


63

19.7 Using Iterators (Cont.)

Figure 19.47   Viewing all Parcels being sent to the same state.

• Save the changes to your code


• Compile and run in the Command Prompt
64

19.7 Using Iterators (Cont.)

Figure 19.48   Adding an item to parcelStateJList.

Selecting the state in parcelStateJComboBox that


was selected by the user in stateJComboBox
65

19.7 Using Iterators (Cont.)

Figure 19.49   Updating items in parcelStateJList.

Selecting the state in parcelStateJComboBox that


was selected by the user in stateJComboBox
66

19.7 Using Iterators (Cont.)

Figure 19.50   Determining if an item needs to be removed from parcelStateJList.

Determine if the state selected in parcelStateJComboBox is


also the state of the Parcel being removed

• If the Parcel is from the same state, then it must be removed


from parcelStateArrayList
67

19.7 Using Iterators (Cont.)


Figure 19.51   Removing an element from parcelStateJList.

Removing the Parcel ID from


the pracelStateArrayList
Assigning the remaining Parcel IDs to

• ArrayList method indexOf takes an object returns the index


the pracelStateArrayList
in which it is stored
68

19.7 Using Iterators (Cont.)

Figure 19.52   Viewing all Parcels destined for Florida.

Scrollbar appears for more


than four Parcel IDs
69

19.7 Using Iterators (Cont.)

Figure 19.53   Resulting JList after a Parcel has been removed.

Parcel 3 is removed
70
1 // Tutorial 19: ShippingHub.java Outline
2 // This application tracks Parcels that pass through a shipping hub.
3 import java.awt.*;
4 import java.awt.event.*; ShippingHub.java
5 import java.util.*; (1 of 28)
6 import javax.swing.*;
7 import javax.swing.border.TitledBorder;
8
9 public class ShippingHub extends JFrame
Import the package
10 { containing class ArrayList
11 // JLabel and JTextField to display time of arrival
12 private JLabel arrivedAtJLabel;
13 private JTextField arrivedAtJTextField;
14
15 // JPanel to contain Parcel information
16 private JPanel parcelInformationJPanel;
17
18 // JLabel and JTextField to display Parcel identification number
19 private JLabel parcelIDJLabel;
20 private JTextField parcelIDJTextField;
21
22 // JLabel and JTextField for name
23 private JLabel nameJLabel;
24 private JTextField nameJTextField;
25

2004 Prentice Hall, Inc.


All rights reserved.
71
26 // JLabel and JTextField for address
27 private JLabel addressJLabel;
Outline
28 private JTextField addressJTextField;
29
ShippingHub.java
30 // JLabel and JTextField for city
(2 of 28)
31 private JLabel cityJLabel;
32 private JTextField cityJTextField;
33
34 // JLabel and JTextField for state
35 private JLabel stateJLabel;
36 private JComboBox stateJComboBox;
37
38 // JLabel and JTextField for zip code
39 private JLabel zipJLabel;
40 private JTextField zipJTextField;
41
42 // JPanel for Parcel number by state
43 private JPanel parcelStateJPanel;
44
45 // JComboBox, JList and JScrollPane for Parcel number
46 private JComboBox parcelStateJComboBox;
47 private JList parcelStateJList; parcelstateJList and
48 private JScrollPane parcelStateJScrollPane; parcelStateJScrollPane used to
49 display a scrollable list of Parcels
bound for the same state

2004 Prentice Hall, Inc.


All rights reserved.
72
50 // JButtons to manipulate Parcels
51 private JButton scanNewJButton;
Outline
52 private JButton addJButton;
53 private JButton removeJButton;
ShippingHub.java
54 private JButton editJButton;
(3 of 28)
55 private JButton updateJButton;
56 private JButton backJButton;
57 private JButton nextJButton;
58
59 // array contains options for parcelStateJComboBox
60 private String[] states = { "AL", "FL", "GA", "KY", "MS", "NC",
61 "SC", "TN", "VA", "WV" };
62
63 // Parcel object contains data for newly entered Parcels
64 private Parcel newParcel;
65
66 // ArrayList contains Parcel objects entered by user ArrayList contains the
67 private ArrayList parcelsArrayList = new ArrayList(); Parcels entered by user
68
69 // ArrayList used to modify and display the Parcel objects
70 // for a specific state
71 private ArrayList parcelStateArrayList = new ArrayList();
72
73 private int parcelID = 0; // ID for new Parcels
74 ArrayList contains Parcel
IDs destined for a specific state

2004 Prentice Hall, Inc.


All rights reserved.
73
75 // position used to track location when the user is
76 // browsing through the list of Parcels
Outline
77 private int position = 0;
78
ShippingHub.java
79 // no-argument constructor
(4 of 28)
80 public ShippingHub()
81 {
82 createUserInterface();
83 }
84
85 // create and position GUI components; register event handlers
86 private void createUserInterface()
87 {
88 // get content pane for attaching GUI components
89 Container contentPane = getContentPane();
90
91 // enable explicit positioning of GUI components
92 contentPane.setLayout( null );
93
94 // set up arrivedAtJLabel
95 arrivedAtJLabel = new JLabel();
96 arrivedAtJLabel.setBounds( 19, 14, 74, 24 );
97 arrivedAtJLabel.setText( "Arrived at:" );
98 contentPane.add( arrivedAtJLabel );
99

2004 Prentice Hall, Inc.


All rights reserved.
74
100 // set up arrivedAtJTextField
101 arrivedAtJTextField = new JTextField();
Outline
102 arrivedAtJTextField.setBounds( 89, 14, 197, 21 );
103 arrivedAtJTextField.setEditable( false );
ShippingHub.java
104 contentPane.add( arrivedAtJTextField );
(5 of 28)
105
106 // set up parcelInformationJPanel
107 parcelInformationJPanel = new JPanel();
108 parcelInformationJPanel.setBounds( 9, 51, 490, 178 );
109 parcelInformationJPanel.setBorder(
110 new TitledBorder( "Parcel Information" ) );
111 parcelInformationJPanel.setLayout( null );
112 contentPane.add( parcelInformationJPanel );
113
114 // set up parcelIDJLabel
115 parcelIDJLabel = new JLabel();
116 parcelIDJLabel.setBounds( 15, 27, 84, 24 );
117 parcelIDJLabel.setText( "Parcel ID:" );
118 parcelInformationJPanel.add( parcelIDJLabel );
119
120 // set up parcelIDJTextField
121 parcelIDJTextField = new JTextField();
122 parcelIDJTextField.setBounds( 80, 27, 386, 21 );
123 parcelIDJTextField.setEditable( false );
124 parcelInformationJPanel.add( parcelIDJTextField );

2004 Prentice Hall, Inc.


All rights reserved.
75
125
126 // set up nameJLabel
Outline
127 nameJLabel = new JLabel();
128 nameJLabel.setBounds( 15, 65, 66, 25 );
ShippingHub.java
129 nameJLabel.setText( "Name:" );
(6 of 28)
130 parcelInformationJPanel.add( nameJLabel );
131
132 // set up nameJTextField
133 nameJTextField = new JTextField();
134 nameJTextField.setBounds( 80, 65, 386, 21 );
135 nameJTextField.setEditable( false );
136 parcelInformationJPanel.add( nameJTextField );
137
138 // set up addressJLabel
139 addressJLabel = new JLabel();
140 addressJLabel.setBounds( 15, 103, 66, 25 );
141 addressJLabel.setText( "Address:" );
142 parcelInformationJPanel.add( addressJLabel );
143
144 // set up addressJTextField
145 addressJTextField = new JTextField();
146 addressJTextField.setBounds( 80, 103, 386, 21 );
147 addressJTextField.setEditable( false );
148 parcelInformationJPanel.add( addressJTextField );
149

2004 Prentice Hall, Inc.


All rights reserved.
76
150 // set up cityJLabel
151 cityJLabel = new JLabel();
Outline
152 cityJLabel.setBounds( 15, 141, 37, 24 );
153 cityJLabel.setText( "City:" );
ShippingHub.java
154 parcelInformationJPanel.add( cityJLabel );
(7 of 28)
155
156 // set up cityJTextField
157 cityJTextField = new JTextField();
158 cityJTextField.setBounds( 80, 141, 117, 21 );
159 cityJTextField.setEditable( false );
160 parcelInformationJPanel.add( cityJTextField );
161
162 // set up stateJLabel
163 stateJLabel = new JLabel();
164 stateJLabel.setBounds( 215, 141, 47, 24 );
165 stateJLabel.setText( "State:" );
166 parcelInformationJPanel.add( stateJLabel );
167
168 // set up stateJComboBox
169 stateJComboBox = new JComboBox( states );
170 stateJComboBox.setBounds( 260, 141, 70, 21 );
171 stateJComboBox.setEnabled( false );
172 parcelInformationJPanel.add( stateJComboBox );
173

2004 Prentice Hall, Inc.


All rights reserved.
77
174 // set up zipJLabel
175 zipJLabel = new JLabel();
Outline
176 zipJLabel.setBounds( 355, 141, 28, 24 );
177 zipJLabel.setText( "Zip:" );
ShippingHub.java
178 parcelInformationJPanel.add( zipJLabel );
(8 of 28)
179
180 // set up zipJTextField
181 zipJTextField = new JTextField();
182 zipJTextField.setBounds( 390, 141, 76, 21 );
183 zipJTextField.setEditable( false );
184 parcelInformationJPanel.add( zipJTextField );
185
186 // set up parcelStateJPanel
187 parcelStateJPanel = new JPanel();
188 parcelStateJPanel.setBounds( 508, 51, 136, 178 );
189 parcelStateJPanel.setBorder(
190 new TitledBorder( "Parcels by State" ) );
191 parcelStateJPanel.setLayout( null );
192 contentPane.add( parcelStateJPanel );
193
194 // set up parcelStateJComboBox
195 parcelStateJComboBox = new JComboBox( states );
196 parcelStateJComboBox.setBounds( 19, 29, 98, 21 );
197 parcelStateJPanel.add( parcelStateJComboBox );

2004 Prentice Hall, Inc.


All rights reserved.
78
198 parcelStateJComboBox.addActionListener(
199
Outline
200 new ActionListener() // anonymous inner class
201 {
ShippingHub.java
202 // event handler called when parcelStateJComboBox
(9 of 28)
203 // is selected
204 public void actionPerformed( ActionEvent event )
205 {
206 parcelStateJComboBoxActionPerformed( event );
207 }
208
209 } // end anonymous inner class
210 Define the parcelStateJList
211 ); // end call to addActionListener as a currently empty JList
212
213 // set up parcelStateJList
214 parcelStateJList = new JList(); 
215
216 // set up parcelStateJScrollPane
217 parcelStateJScrollPane = new JScrollPane( parcelStateJList );
218 parcelStateJScrollPane.setBounds( 19, 65, 98, 82 );
219 parcelStateJPanel.add( parcelStateJScrollPane );
220

Add the parcelStateJList to a


JScrollPane so that scrollbars
will be added as necessary

2004 Prentice Hall, Inc.


All rights reserved.
79
221 // set up scanNewJButton
222 scanNewJButton = new JButton();
Outline
223 scanNewJButton.setBounds( 9, 248, 95, 26 );
224 scanNewJButton.setText( "Scan New" );
ShippingHub.java
225 scanNewJButton.setMnemonic( KeyEvent.VK_S );
(10 of 28)
226 contentPane.add( scanNewJButton );
227 scanNewJButton.addActionListener(
Set the mnemonic of
228
229 new ActionListener() // anonymous inner class
scanNewJButton to ‘S’
230 {
231 // event handler called when scanNewJButton is pressed
232 public void actionPerformed( ActionEvent event )
233 {
234 scanNewJButtonActionPerformed( event );
235 }
236
237 } // end anonymous inner class
238
239 ); // end call to addActionListener
240
241 // set up addJButton
242 addJButton = new JButton();
243 addJButton.setBounds( 109, 248, 85, 26 );
Set the mnemonic of
244 addJButton.setText( "Add" );
addJButton to ‘A’
245 addJButton.setMnemonic( KeyEvent.VK_A );

2004 Prentice Hall, Inc.


All rights reserved.
80
246 addJButton.setEnabled( false );
247 contentPane.add( addJButton );
Outline
248 addJButton.addActionListener(
249
ShippingHub.java
250 new ActionListener() // anonymous inner class
(11 of 28)
251 {
252 // event handler called when addJButton is pressed
253 public void actionPerformed( ActionEvent event )
254 {
255 addJButtonActionPerformed( event );
256 }
257
258 } // end anonymous inner class
259
260 ); // end call to addActionListener
261
262 // set up removeJButton
263 removeJButton = new JButton();
264 removeJButton.setBounds( 199, 248, 85, 26 );
265 removeJButton.setText( "Remove" );
266 removeJButton.setMnemonic( KeyEvent.VK_R ); Set the mnemonic of
267 removeJButton.setEnabled( false ); removeJButton to ‘R’
268 contentPane.add( removeJButton );
269 removeJButton.addActionListener(
270

2004 Prentice Hall, Inc.


All rights reserved.
81
271 new ActionListener() // anonymous inner class
272 {
Outline
273 // event handler called when removeJButton is pressed
274 public void actionPerformed( ActionEvent event )
ShippingHub.java
275 {
(12 of 28)
276 removeJButtonActionPerformed( event );
277 }
278
279 } // end anonymous inner class
280
281 ); // end call to addActionListener
282
283 // set up editJButton
284 editJButton = new JButton();
285 editJButton.setBounds( 289, 248, 85, 26 );
286 editJButton.setText( "Edit" );
287 editJButton.setMnemonic( KeyEvent.VK_E );  Set the mnemonic of
288 editJButton.setEnabled( false ); editJButton to ‘E’
289 contentPane.add( editJButton );
290 editJButton.addActionListener(
291

2004 Prentice Hall, Inc.


All rights reserved.
82
292 new ActionListener() // anonymous inner class
293 {
Outline
294 // event handler called when editJButton is pressed
295 public void actionPerformed( ActionEvent event )
ShippingHub.java
296 {
(13 of 28)
297 editJButtonActionPerformed( event );
298 }
299
300 } // end anonymous inner class
301
302 ); // end call to addActionListener
303
304 // set up updateJButton
305 updateJButton = new JButton();
306 updateJButton.setBounds( 379, 248, 85, 26 );
307 updateJButton.setText( "Update" );
308 updateJButton.setMnemonic( KeyEvent.VK_U ); Set the mnemonic of
309 updateJButton.setEnabled( false ); updateJButton to ‘U’
310 contentPane.add( updateJButton );
311 updateJButton.addActionListener(
312

2004 Prentice Hall, Inc.


All rights reserved.
83
313 new ActionListener() // anonymous inner class
314 {
Outline
315 // event handler called when updateJButton is pressed
316 public void actionPerformed( ActionEvent event )
ShippingHub.java
317 {
(14 of 28)
318 updateJButtonActionPerformed( event );
319 }
320
321 } // end anonymous inner class
322
323 ); // end call to addActionListener
324
325 // set up backJButton
326 backJButton = new JButton();
327 backJButton.setBounds( 469, 248, 85, 26 );
328 backJButton.setText( "< Back" );
329 backJButton.setMnemonic( KeyEvent.VK_B ); Set the mnemonic of
330 backJButton.setEnabled( false ); backJButton to ‘B’
331 contentPane.add( backJButton );
332 backJButton.addActionListener(
333

2004 Prentice Hall, Inc.


All rights reserved.
84
334 new ActionListener() // anonymous inner class
335 {
Outline
336 // event handler called when backJButton is pressed
337 public void actionPerformed( ActionEvent event )
ShippingHub.java
338 {
(15 of 28)
339 backJButtonActionPerformed( event );
340 }
341
342 } // end anonymous inner class
343
344 ); // end call to addActionListener
345
346 // set up nextJButton
347 nextJButton = new JButton();
348 nextJButton.setBounds( 559, 248, 85, 26 );
349 nextJButton.setText( "Next >" );
350 nextJButton.setMnemonic( KeyEvent.VK_N ); Set the mnemonic of
351 nextJButton.setEnabled( false ); nextJButton to ‘N’
352 contentPane.add( nextJButton );

2004 Prentice Hall, Inc.


All rights reserved.
85
353 nextJButton.addActionListener(
354
Outline
355 new ActionListener() // anonymous inner class
356 {
ShippingHub.java
357 // event handler called when nextJButton is pressed
(16 of 28)
358 public void actionPerformed( ActionEvent event )
359 {
360 nextJButtonActionPerformed( event );
361 }
362
363 } // end anonymous inner class
364
365 ); // end call to addActionListener
366
367 // set properties of application's window
368 setTitle( "Shipping Hub" ); // set title bar string
369 setSize( 663, 313 ); // set window size
370 setVisible( true ); // display window
371
372 } // end method createUserInterface
373

2004 Prentice Hall, Inc.


All rights reserved.
86
374 // prepare to scan a new Parcel
375 private void scanNewJButtonActionPerformed( ActionEvent event )
Outline
376 {
377 // clear JTextFields
ShippingHub.java
378 clearComponents();
(17 of 28)
379
380 // set arrival time
381 Date now = new Date(); Create a new Date object and set
382 arrivedAtJTextField.setText( now.toString() ); the arrival time for the Parcel
383
384 // give Parcel unique ID number Create a unique ID number
385 parcelID++;
for the new Parcel
386 parcelIDJTextField.setText( String.valueOf( parcelID ) );
387
388 // create new Parcel object
Create a Parcel object to
389 newParcel = new Parcel( parcelID,
contain the user’s data
390 arrivedAtJTextField.getText() );
391
392 // disable appropriate components
393 setJButtons( false );
394
395 // enable or make editable appropriate components
396 addJButton.setEnabled( true );
397 parcelInformationJPanelEditable( true );
398

2004 Prentice Hall, Inc.


All rights reserved.
87
399 // grab focus
400 nameJTextField.requestFocusInWindow();
Outline
401
402 } // end method scanNewJButtonActionPerformed
ShippingHub.java
403
(18 of 28)
404 // add a new Parcel
405 private void addJButtonActionPerformed( ActionEvent event )
406 {
407 // set information for new Parcel
408 setParcelData();
409
410 // add new Parcel to parcelsArrayList
Add the Parcel to the
411 parcelsArrayList.add( newParcel ); ArrayList and set
412 position = parcelsArrayList.size() - 1; position to the last element
413 in the ArrayList
414 // disable or make uneditable appropriate components
415 addJButton.setEnabled( false );
416 parcelInformationJPanelEditable( false );
417
418 // enable appropriate components
419 setJButtons( true );
420 Change the selected item,
421 // change selected item in parcelStateJComboBox causing the
422 parcelStateJComboBox.setSelectedIndex( parcelStateJComboBox’s
423 stateJComboBox.getSelectedIndex() ); actionPerformed event
424 handler to execute
425 } // end method addJButtonActionPerformed

2004 Prentice Hall, Inc.


All rights reserved.
88
426
427 // remove a Parcel
Outline
428 private void removeJButtonActionPerformed( ActionEvent event )
429 {
ShippingHub.java
430 // retrieve the state of the current Parcel
(19 of 28)
431 String stateSelected = newParcel.getState();
432
433 // if same state is selected, remove ID number from
434 // parcelStateJList
435 if ( stateSelected.equals(
436 parcelStateJComboBox.getSelectedItem() ) )
437 {
If the state in the
438 // index of current Parcel parcelStateJComboBox is
439 int index = parcelStateArrayList.indexOf( the same as the state of the
440 String.valueOf( newParcel.getParcelID() ) ); Parcel to be removed, remove
441 parcelStateArrayList.remove( index ); the Parcel ID from the
442 parcelStateArrayList
443 // reset JList data
444 parcelStateJList.setListData(
445 parcelStateArrayList.toArray() ); The setListData method
446 displays an array’s
447 } // end if elements in a JList
448
449 // remove current Parcel from ArrayList
450 parcelsArrayList.remove( position ); Remove the current Parcel
from the parcelsArrayList

2004 Prentice Hall, Inc.


All rights reserved.
89
451
452 // load next Parcel in list if there is one
Outline
453 if ( parcelsArrayList.size() > 0 )
454 {
ShippingHub.java
455 if ( position >= parcelsArrayList.size() )
(20 of 28)
456 {
457 position = 0; // go to beginning
458 } Determine the position of
459 the next Parcel to be
460 loadParcel();
displayed, and call
461 }
loadParcel to display it
462 else
463 {
464 // if no other Parcels remain
If the removed Parcel was
465 clearComponents();
466 }
the only Parcel, clear the
467 application’s components
468 setJButtons( true ); // enabled appropriate JButtons
469
470 // set focus to scanNewJButton
471 scanNewJButton.requestFocusInWindow();
472
473 } // end method removeJButtonActionPerformed
474

2004 Prentice Hall, Inc.


All rights reserved.
90
475 // allow user to edit Parcel information
476 private void editJButtonActionPerformed( ActionEvent event )
Outline
477 {
478 // disable appropriate components
ShippingHub.java
479 setJButtons( false );
(21 of 28)
480
481 // make user able to update Parcel information
482 updateJButton.setEnabled( true );
483 parcelInformationJPanelEditable( true );
484
485 } // end method editJButtonActionPerformed
486
487 // move to next Parcel
488 The
private void updateJButtonActionPerformed( ActionEvent event ) setParcelData
489 { method sets the Parcel’s
490 setParcelData(); // update information properties to the values
491
entered by the user
492 // enable or make editable appropriate components
493 setJButtons( true );
494
495 // disable or make uneditable appropriate components
496 updateJButton.setEnabled( false );
497 parcelInformationJPanelEditable( false );
498

2004 Prentice Hall, Inc.


All rights reserved.
91
499 // change selected item in parcelStateJComboBox
500 parcelStateJComboBox.setSelectedIndex(
Outline
501 stateJComboBox.getSelectedIndex() ); Change the selected item,
502 causing the
ShippingHub.java
503 } // end method updateJButtonActionPerformed parcelStaeJComboBox’s
(22 of 28)
504 actionPerformed event
505 // move to previous Parcel handler to execute
506 private void backJButtonActionPerformed( ActionEvent event )
507 {
508 if ( position > 0 )
509 {
When the user clicks the
510 position--; // move position back by 1 < Back JButton, decrement
511 } the position. If the position
512 else // go to last element in list was zero, set the position to
513 { the last object in the
514 position = parcelsArrayList.size() - 1; ArrayList
515 }
516
517 // set and load Parcel
518 loadParcel(); Display the previous Parcel
519
520 } // end method backJButtonActionPerformed
521

2004 Prentice Hall, Inc.


All rights reserved.
92
522 // move to next Parcel
523 private void nextJButtonActionPerformed( ActionEvent event )
Outline
524 {
525 if ( position < parcelsArrayList.size() - 1 )
ShippingHub.java
526 {
(23 of 28)
527 position++; // move position forward by 1
When the user clicks the
528 }
529 else
Next > JButton, increment
530 { the position. If the position
531 position = 0; // go to first element in list was the index of the last
532 } object in the ArrayList, set
533 position to zero.
534 // load information of Parcel
535 loadParcel(); Display the next Parcel
536
537 } // end method nextJButtonActionPerformed
538
539 // change the list of Parcels in the parcelStateJList
540 private void parcelStateJComboBoxActionPerformed(
541 ActionEvent event )
542 {
543 // create string to compare states
544 String state =
545 ( String ) parcelStateJComboBox.getSelectedItem();
546

Retrieve the state code selected in


parcelStateJComboBox
2004 Prentice Hall, Inc.
All rights reserved.
93
547 // create iterator
548 Iterator parcelIterator = parcelsArrayList.iterator();
Outline
549
550 // clear parcelStateArrayList
ShippingHub.java
Create a new Iterator object
551 parcelStateArrayList.clear();
(24 of 28)
552
553 // create parcelStateArrayList with ID numbers of Parcels
554 // to be displayed Remove all items from the
555 while ( parcelIterator.hasNext() ) parcelStateArrayList
556 {
557 // create temporary reference to Parcel object

558 Parcel currentParcel = ( Parcel ) parcelIterator.next();


559
Iterate through every
560 // add parcel ID to ArrayList Retrieve element
the next in the ArrayList
object in
561 if the) parcelsArrayList
( state.equals( currentParcel.getState()
562 {
563 parcelStateArrayList.add( String.valueOf(
564 currentParcel.getParcelID() ) );
Add the Parcel ID to the
565 }
parcelStateArrayList
566
567 } // end while
568
569 // display ArrayList in parcelStateJList
570 parcelStateJList.setListData(
Display the parcel ID numbers in
571 parcelStateArrayList.toArray() ); the parcelStateArrayList
572
573 } // end method parcelStateJComboBoxActionPerformed
2004 Prentice Hall, Inc.
All rights reserved.
94
574
575 // set all information about the Parcel
Outline
576 private void setParcelData()
577 {
ShippingHub.java
578 newParcel.setName( nameJTextField.getText() );
(25 of 28)
579 newParcel.setAddress( addressJTextField.getText() );
580 newParcel.setCity( cityJTextField.getText() );
581 newParcel.setState( states[
582 stateJComboBox.getSelectedIndex() ] );
583 newParcel.setZip( Integer.parseInt(
584 zipJTextField.getText() ) );
585
586 } // end method setParcelData
587
588 // display all information about the Parcel Retrieve object at index
589 private void loadParcel() position in
590 { parcelsArrayList
591 // retrieve package from list
592 newParcel = ( Parcel ) parcelsArrayList.get( position );
593
594 // display package data
595 arrivedAtJTextField.setText( newParcel.getArrivalTime() );
596 parcelIDJTextField.setText(
597 String.valueOf( newParcel.getParcelID() ) );
598 nameJTextField.setText( newParcel.getName() );
599 addressJTextField.setText( newParcel.getAddress() );

2004 Prentice Hall, Inc.


All rights reserved.
95
600 cityJTextField.setText( newParcel.getCity() );
601 stateJComboBox.setSelectedItem( newParcel.getState() );
Outline
602 zipJTextField.setText( String.valueOf( newParcel.getZip() ) );
603
ShippingHub.java
604 } // end method loadParcel
(26 of 28)
605
606 // clear all information about the Parcel
607 private void clearComponents()
608 {
609 nameJTextField.setText( "" );
610 addressJTextField.setText( "" );
611 cityJTextField.setText( "" );
612 zipJTextField.setText( "" );
613 arrivedAtJTextField.setText( "" );
614 parcelIDJTextField.setText( "" );
615
616 } // end method clearComponents
617
618 // enabled/disable JButtons
619 private void setJButtons( boolean state )
620 {
621 backJButton.setEnabled( state );
622 scanNewJButton.setEnabled( state );
623 removeJButton.setEnabled( state );
624 editJButton.setEnabled( state );
625 nextJButton.setEnabled( state );

2004 Prentice Hall, Inc.


All rights reserved.
96
626
627 // disable navigation if not multiple packages
Outline
628 if ( parcelsArrayList.size() < 2 )
629 {
ShippingHub.java
630 nextJButton.setEnabled( false );
(27 of 28)
631 backJButton.setEnabled( false );
632 }
633
634 // if no items, disable Remove, Edit and Update JButtons
635 if ( parcelsArrayList.size() == 0 )
636 {
637 editJButton.setEnabled( false );
638 updateJButton.setEnabled( false );
639 removeJButton.setEnabled( false );
640 }
641
642 } // end method setJButtons
643

2004 Prentice Hall, Inc.


All rights reserved.
97
644 // make editable or uneditable components
645 // in parcelInformationJPanel
Outline
646 private void parcelInformationJPanelEditable( boolean editable )
647 {
ShippingHub.java
648 nameJTextField.setEditable( editable );
(28 of 28)
649 addressJTextField.setEditable( editable );
650 cityJTextField.setEditable( editable );
651 stateJComboBox.setEnabled( editable );
652 zipJTextField.setEditable( editable );
653
654 } // end method parcelInformationJPanelEditable
655
656 // main method
657 public static void main( String[] args )
658 {
659 ShippingHub application = new ShippingHub();
660 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
661
662 } // end method main
663
664 } // end class ShippingHub

2004 Prentice Hall, Inc.


All rights reserved.

Das könnte Ihnen auch gefallen