Professional Documents
Culture Documents
GUI Arraylist
GUI Arraylist
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
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.
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.
Initializing parcelStateJList
with a new JList
17
•Pressing Alt+S will have the same effect as if the user clicks the
JButton.
20
Mnemonic underlined
21
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.
Panel information
is made uneditable
37
19.6 Constructing the Shipping Hub
Application (Cont.)
Figure 19.24 Removing a Parcel from the ArrayList.
Decrement position if
the current Parcel is
not the first Parcel
Increment position if
the current Parcel is
not the last Parcel
Figure 19.47 Viewing all Parcels being sent to the same state.
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