Home >Documents >GUI Arraylist

GUI Arraylist

Date post:16-Nov-2014
Category:
View:113 times
Download:0 times
Share this document with a friend
Transcript:

1

Tutorial 19 Shipping Hub ApplicationIntroducing Collections, ArrayList and IteratorsOutline 19.1 19.2 19.3 19.4 19.5 19.6 19.7 19.8 Test-Driving the Shipping Hub Application Parcel Class JList Component Using Mnemonics (Keyboard Shortcuts) Collections Constructing the Shipping Hub Application Using Iterators 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.

19.1

Test-Driving the Shipping Hub Application

3

Application RequirementsA 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 applications 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 recipients 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.

19.1

Test-Driving the Shipping Hub Application (Cont.)

4

Running the completed application Open a Command Prompt Change to ShippingHub directory Type java ShippingHub

19.1

Test-Driving the Shipping Hub Application (Cont.)

5

Figure 19.1 Running the completed Shipping Hub application.

JPanel contains fields for user to view or edit the Parcel

JComboBoxes list

available states

19.1

Test-Driving the Shipping Hub Application (Cont.)Figure 19.2 Scanning a new Parcel.

6

User can now enter Parcel information

19.1

Test-Driving the Shipping Hub Application (Cont.)

7

Figure 19.3 Entering a new Parcels information.

New Parcel listed in JList

19.1

Test-Driving the Shipping Hub Application (Cont.)

8

Figure 19.4 Parcel information is editable when Edit JButton is clicked.

Fields are made editable

Update JButton is enabled

19.1

Test-Driving the Shipping Hub Application (Cont.)

9

Figure 19.5 New information is stored when Update JButton is clicked.

Updated zip code

19.1

Test-Driving the Shipping Hub Application (Cont.)

10

Figure 19.6 Next Parcel is displayed when Remove JButton is clicked.

When a Parcel is removed, the next Parcel is displayed

19.1

Test-Driving the Shipping Hub Application (Cont.)

11

Figure 19.7 Viewing all Parcels going to South Carolina.

All Parcels being sent to South Carolina

12

19.2

Parcel Class

Store each Parcels 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 recipients 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). Provides access to instance variable address (a getAddress/ setAddress String). getCity/setCity Provides access to instance variable city (a String). Provides access to instance variable state (a getState/setState String). States are represented as two-letter abbreviations. Provides access to instance variable zip (an int). getZip/setZip Provides access to instance variable parcelID (an getParcelID int). Provides access to instance variable arrivalTime getArrivalTime (a String). Constructor that is used to create a new Parcel object Parcel 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 Figure 19.11 Declaring a new JScrollPane containing the JList. the 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 components 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 theJButton.

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 Predefined classes

Collections

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.

19.6

Constructing the Shipping Hub Application

23

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 users 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

19.6

Constructing the Shipping Hub Application (Cont.)C ponent/C om lass/O bject , arrivedAtJLabel , parcelIDJLabel , nameJLabel , addressJLabel , cityJLabel , stateJLabel , zipJLabel parcelInformationJPanel parcelStateJPanel parcelsArrayList parcelStateArray List E vent/M ethod

24

A ction L blth a pc ti n c mo e ts a e e p l a o o p nn i s

,

Ce tep r e r a ac l s Ar y Ls toc n i ra i t o tan P r e bi gs n ac l e s n et Ce tep r e S te Ar y i t to r a ac l ta r a Ls c n i th P r e bi gs n toa o tan e ac l e s n et s ei i s te pcfc ta D p yth arv lti ea dth I i l s a e ri a m n eD n me o th nw ac l u br f e e P r e Ce teaP r eo j c f rth n w ra ac l b t o e e e e tr ny Rtreei p t v l e f o th u e e i v n u aus r m e s r

al l

arrivedAtJTextField parcelIDJTextField newParcel , nameJTextField addressJTextField , cityJTextField , zipJTextField stateJComboBox

,

U ser clicks S can N ew JButton U clicks ser A dd JButton

,

Figure 19.15 ACE table for the Shipping Hub application. (Part 1 of 4.)

19.6

Constructing the Shipping Hub Application (Cont.)newParcel , parcelsArrayList parcelsArrayList parcelStateJList parcelStateJList parcelsArrayList newParcel , parcelsArrayList nameJTextField , addressJTextField cityJTextField , zipJTextField , stateJComboBox User clicks Remove JButton

25

Sto in u v lu s inth n w re p t a e e e P r e o je t ac l b c A dth n w a e toth p rc ls d e e P rc l e a e A ra ist r yL A dth P r e IDtoth d e ac l e p r e te ist a c lSta JL R m v th P rc l IDfro th e oe e a e m e p r e te ist a c lSta JL R m v th P rc l fro th e oe e a e m e pr e A aL a c ls rr y ist R trie ev lu s fro th n x e v a e m e et Pre ac l D la th n x P r e isp y e e t a c l

,

Figure 19.15 ACE table for the Shipping Hub application. (Part 2 of 4.)

19.6

Constructing the Shipping Hub Application (Cont.)nameJTextField addressJTextField cityJTextField , zipJTextField stateJComboBox nameJTextField addressJTextField cityJTextField , zipJTextField stateJComboBox , newParcel parcelsArrayList parcelStateJList , newParcel parcelsArrayList , nameJTextField addressJTextField , cityJTextField , zipJTextField stateJComboBox , , , , , , , U clicks ser E it JButton d

26

E a l th i p t c mo e ts n b en u o p n n e

Rtreei p t v l e f o th e i v n u aus r m e ue sr

U clicks ser U date p J Button

S r th nwn u v l e i th toe e e i p t aus n e P r eo j c ac l b t e U d te ep r e ta JLs p a th ac l te i t S b s do th u e c a gs a e n e s rs h n e Rtree ausf o th pei u e i v vl e r m e r

Popular Tags:

Click here to load reader

Embed Size (px)
Recommended