Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application."— Presentation transcript:

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application 19.2 Parcel Class 19.3 JList Component 19.4Using Mnemonics (Keyboard Shortcuts) 19.5Collections 19.6Constructing the Shipping Hub Application 19.7Using Iterators 19.8 Wrap-Up Tutorial 19 – Shipping Hub Application Introducing Collections, ArrayList and Iterators

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 19.1 Test-Driving the Shipping Hub Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 JComboBox es list available states

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 19.1 Test-Driving the Shipping Hub Application (Cont.) Figure 19.2 Scanning a new Parcel. User can now enter Parcel information

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 19.1 Test-Driving the Shipping Hub Application (Cont.) Figure 19.7 Viewing all Parcel s going to South Carolina. All Parcel s being sent to South Carolina

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 19.2 Parcel Class (Cont.)

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 19.3 JList Component (Cont.) Figure 19.9 Declaring new references to a JList and a JScrollPane.

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 19.3 JList Component (Cont.) Figure 19.10 Creating a new JList object. Initializing parcelStateJList with a new JList

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 19.3 JList Component (Cont.) Figure 19.11 Declaring a new JScrollPane containing the JList. JScrollPane allows user to scroll through items if there are more items in the list than the number of visible rows. Adding the parcelStateJList to the parcelStateJScrollPane

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 19.4 Using Mnemonics (Keyboard Shortcuts) Figure 19.12 Creating a mnemonic. Pressing Alt+S will have the same effect as if the user clicks the JButton. Setting the mnemonic of scanNewJButton to S

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 19.4 Using Mnemonics (Keyboard Shortcuts) Figure 19.13 JButton with mnemonic property set to 'S'. Mnemonic underlined

21 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 19.4 Using Mnemonics (Keyboard Shortcuts) Figure 19.14 JButton s with mnemonics.

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.15 ACE table for the Shipping Hub application. (Part 1 of 4.)

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.15 ACE table for the Shipping Hub application. (Part 2 of 4.)

26 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.15 ACE table for the Shipping Hub application. (Part 3 of 4.)

27 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.15 ACE table for the Shipping Hub application. (Part 4 of 4.)

28 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.16 Importing class ArrayList. The ArrayList class is located in java.util Importing the java.util package

29 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.17 Creating the ArrayList. Creating the ArrayList

30 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 JTextField s

33 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.25 Adding a Parcel to parcelsArrayList.

39 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.26 Removing a Parcel from parcelsArrayList.

40 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 19.6 Constructing the Shipping Hub Application (Cont.) To edit parcel information –Click Edit JButton Enable all JTextField s, except for ID number and arrival time –Enter new information –Click Update JButton to submit new information

41 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.28 Entering a new Parcel.

43 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.30 Storing changes to a Parcel information when the Update JButton is clicked. New city name is stored in parcelsArrayList

45 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 45 19.6 Constructing the Shipping Hub Application (Cont.) Displaying a Parcel after a removal –When element removed is the last element in the A rrayList Display first Parcel in ArrayList –When element removed is the only element in the A rrayList Clear application’s components –Otherwise, display the next Parcel

46 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 Parcel s

47 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 49 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.34 Calling the loadParcel method to display the previous Parcel ’s information.

50 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 51 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.36 Calling the loadParcel method to display the next Parcel ’s information.

52 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 52 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.37 Displaying the Parcel ’s data in your application’s components. Display the data stored in the Parcel object Retrieving Parcel at index position in parcelsArrayList

53 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 54 19.6 Constructing the Shipping Hub Application (Cont.) Figure 19.39 Displaying the previous Parcel using the < Back JButton.

55 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 56 19.7 Using Iterators (Cont.) Figure 19.40 Declaring a variable to contain the state selected. Obtain the state selected in parcelStateJComboBox

57 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 57 19.7 Using Iterators (Cont.) Figure 19.41 Declaring an Iterator. Declaring the Iterator for the while repetition statement

58 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 58 19.7 Using Iterators (Cont.) Figure 19.42 Clearing parcelStateArrayList. clear method removes all Parcel s from the ArrayList Removing all elements from parcelStateArrayList

59 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 59 19.7 Using Iterators (Cont.) Figure 19.43 Iterator used in a while statement. Using iterator in the while condition

60 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 60 19.7 Using Iterators (Cont.) Figure 19.44 Creating a reference using parcelIterator.next. Retrieving the next Parcel in the parcelIterator

61 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 61 19.7 Using Iterators (Cont.) Figure 19.45 Adding all shipments going to selected state. If state codes match, the Parcel is added to the ArrayList Adding the Parcel ’s ID number to parcelStateArrayList

62 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 62 19.7 Using Iterators (Cont.) Figure 19.46 Displaying all Parcel s going to the selected state. toArray method an array of Object s Displaying the parcelID in the parcelStateJList

63 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 63 19.7 Using Iterators (Cont.) Figure 19.47 Viewing all Parcel s being sent to the same state. Save the changes to your code Compile and run in the Command Prompt

64 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 66 19.7 Using Iterators (Cont.) Figure 19.50 Determining if an item needs to be removed from parcelStateJList. If the Parcel is from the same state, then it must be removed from parcelStateArrayList Determine if the state selected in parcelStateJComboBox is also the state of the Parcel being removed

67 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 67 19.7 Using Iterators (Cont.) Figure 19.51 Removing an element from parcelStateJList. ArrayList method indexOf takes an object returns the index in which it is stored Removing the Parcel ID from the pracelStateArrayList Assigning the remaining Parcel IDs to the pracelStateArrayList

68 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 68 19.7 Using Iterators (Cont.) Figure 19.52 Viewing all Parcel s destined for Florida. Scrollbar appears for more than four Parcel IDs

69 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 69 19.7 Using Iterators (Cont.) Figure 19.53 Resulting JList after a Parcel has been removed. Parcel 3 is removed

70  2004 Prentice Hall, Inc. All rights reserved. Outline 70 ShippingHub.java (1 of 28) 1 // Tutorial 19: ShippingHub.java 2 // This application tracks Parcels that pass through a shipping hub. 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.*; 6 import javax.swing.*; 7 import javax.swing.border.TitledBorder; 8 9 public class ShippingHub extends JFrame 10 { 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 Import the package containing class ArrayList

71  2004 Prentice Hall, Inc. All rights reserved. Outline 71 ShippingHub.java (2 of 28) 26 // JLabel and JTextField for address 27 private JLabel addressJLabel; 28 private JTextField addressJTextField; 29 30 // JLabel and JTextField for city 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; 48 private JScrollPane parcelStateJScrollPane; 49 parcelstateJList and parcelStateJScrollPane used to display a scrollable list of Parcel s bound for the same state

72  2004 Prentice Hall, Inc. All rights reserved. Outline 72 ShippingHub.java (3 of 28) 50 // JButtons to manipulate Parcels 51 private JButton scanNewJButton; 52 private JButton addJButton; 53 private JButton removeJButton; 54 private JButton editJButton; 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 67 private ArrayList parcelsArrayList = new ArrayList(); 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 the Parcel s entered by user ArrayList contains Parcel IDs destined for a specific state

73  2004 Prentice Hall, Inc. All rights reserved. Outline 73 ShippingHub.java (4 of 28) 75 // position used to track location when the user is 76 // browsing through the list of Parcels 77 private int position = 0; 78 79 // no-argument constructor 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

74  2004 Prentice Hall, Inc. All rights reserved. Outline 74 ShippingHub.java (5 of 28) 100 // set up arrivedAtJTextField 101 arrivedAtJTextField = new JTextField(); 102 arrivedAtJTextField.setBounds( 89, 14, 197, 21 ); 103 arrivedAtJTextField.setEditable( false ); 104 contentPane.add( arrivedAtJTextField ); 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 );

75  2004 Prentice Hall, Inc. All rights reserved. Outline 75 ShippingHub.java (6 of 28) 125 126 // set up nameJLabel 127 nameJLabel = new JLabel(); 128 nameJLabel.setBounds( 15, 65, 66, 25 ); 129 nameJLabel.setText( "Name:" ); 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

76  2004 Prentice Hall, Inc. All rights reserved. Outline 76 ShippingHub.java (7 of 28) 150 // set up cityJLabel 151 cityJLabel = new JLabel(); 152 cityJLabel.setBounds( 15, 141, 37, 24 ); 153 cityJLabel.setText( "City:" ); 154 parcelInformationJPanel.add( cityJLabel ); 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

77  2004 Prentice Hall, Inc. All rights reserved. Outline 77 ShippingHub.java (8 of 28) 174 // set up zipJLabel 175 zipJLabel = new JLabel(); 176 zipJLabel.setBounds( 355, 141, 28, 24 ); 177 zipJLabel.setText( "Zip:" ); 178 parcelInformationJPanel.add( zipJLabel ); 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 );

78  2004 Prentice Hall, Inc. All rights reserved. Outline 78 ShippingHub.java (9 of 28) 198 parcelStateJComboBox.addActionListener( 199 200 new ActionListener() // anonymous inner class 201 { 202 // event handler called when parcelStateJComboBox 203 // is selected 204 public void actionPerformed( ActionEvent event ) 205 { 206 parcelStateJComboBoxActionPerformed( event ); 207 } 208 209 } // end anonymous inner class 210 211 ); // end call to addActionListener 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 Define the parcelStateJList as a currently empty JList Add the parcelStateJList to a JScrollPane so that scrollbars will be added as necessary

79  2004 Prentice Hall, Inc. All rights reserved. Outline 79 ShippingHub.java (10 of 28) 221 // set up scanNewJButton 222 scanNewJButton = new JButton(); 223 scanNewJButton.setBounds( 9, 248, 95, 26 ); 224 scanNewJButton.setText( "Scan New" ); 225 scanNewJButton.setMnemonic( KeyEvent.VK_S ); 226 contentPane.add( scanNewJButton ); 227 scanNewJButton.addActionListener( 228 229 new ActionListener() // anonymous inner class 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 ); 244 addJButton.setText( "Add" ); 245 addJButton.setMnemonic( KeyEvent.VK_A ); Set the mnemonic of scanNewJButton to ‘S’ Set the mnemonic of addJButton to ‘A’

80  2004 Prentice Hall, Inc. All rights reserved. Outline 80 ShippingHub.java (11 of 28) 246 addJButton.setEnabled( false ); 247 contentPane.add( addJButton ); 248 addJButton.addActionListener( 249 250 new ActionListener() // anonymous inner class 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 ); 267 removeJButton.setEnabled( false ); 268 contentPane.add( removeJButton ); 269 removeJButton.addActionListener( 270 Set the mnemonic of removeJButton to ‘R’

81  2004 Prentice Hall, Inc. All rights reserved. Outline 81 ShippingHub.java (12 of 28) 271 new ActionListener() // anonymous inner class 272 { 273 // event handler called when removeJButton is pressed 274 public void actionPerformed( ActionEvent event ) 275 { 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 ); 288 editJButton.setEnabled( false ); 289 contentPane.add( editJButton ); 290 editJButton.addActionListener( 291 Set the mnemonic of editJButton to ‘E’

82  2004 Prentice Hall, Inc. All rights reserved. Outline 82 ShippingHub.java (13 of 28) 292 new ActionListener() // anonymous inner class 293 { 294 // event handler called when editJButton is pressed 295 public void actionPerformed( ActionEvent event ) 296 { 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 ); 309 updateJButton.setEnabled( false ); 310 contentPane.add( updateJButton ); 311 updateJButton.addActionListener( 312 Set the mnemonic of updateJButton to ‘U’

83  2004 Prentice Hall, Inc. All rights reserved. Outline 83 ShippingHub.java (14 of 28) 313 new ActionListener() // anonymous inner class 314 { 315 // event handler called when updateJButton is pressed 316 public void actionPerformed( ActionEvent event ) 317 { 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 ); 330 backJButton.setEnabled( false ); 331 contentPane.add( backJButton ); 332 backJButton.addActionListener( 333 Set the mnemonic of backJButton to ‘B’

84  2004 Prentice Hall, Inc. All rights reserved. Outline 84 ShippingHub.java (15 of 28) 334 new ActionListener() // anonymous inner class 335 { 336 // event handler called when backJButton is pressed 337 public void actionPerformed( ActionEvent event ) 338 { 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 ); 351 nextJButton.setEnabled( false ); 352 contentPane.add( nextJButton ); Set the mnemonic of nextJButton to ‘N’

85  2004 Prentice Hall, Inc. All rights reserved. Outline 85 ShippingHub.java (16 of 28) 353 nextJButton.addActionListener( 354 355 new ActionListener() // anonymous inner class 356 { 357 // event handler called when nextJButton is pressed 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

86  2004 Prentice Hall, Inc. All rights reserved. Outline 86 ShippingHub.java (17 of 28) 374 // prepare to scan a new Parcel 375 private void scanNewJButtonActionPerformed( ActionEvent event ) 376 { 377 // clear JTextFields 378 clearComponents(); 379 380 // set arrival time 381 Date now = new Date(); 382 arrivedAtJTextField.setText( now.toString() ); 383 384 // give Parcel unique ID number 385 parcelID++; 386 parcelIDJTextField.setText( String.valueOf( parcelID ) ); 387 388 // create new Parcel object 389 newParcel = new Parcel( parcelID, 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 Create a new Date object and set the arrival time for the Parcel Create a unique ID number for the new Parcel Create a Parcel object to contain the user’s data

87  2004 Prentice Hall, Inc. All rights reserved. Outline 87 ShippingHub.java (18 of 28) 399 // grab focus 400 nameJTextField.requestFocusInWindow(); 401 402 } // end method scanNewJButtonActionPerformed 403 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 411 parcelsArrayList.add( newParcel ); 412 position = parcelsArrayList.size() - 1; 413 414 // disable or make uneditable appropriate components 415 addJButton.setEnabled( false ); 416 parcelInformationJPanelEditable( false ); 417 418 // enable appropriate components 419 setJButtons( true ); 420 421 // change selected item in parcelStateJComboBox 422 parcelStateJComboBox.setSelectedIndex( 423 stateJComboBox.getSelectedIndex() ); 424 425 } // end method addJButtonActionPerformed Add the Parcel to the ArrayList and set position to the last element in the ArrayList Change the selected item, causing the parcelStateJComboBox’s actionPerformed event handler to execute

88  2004 Prentice Hall, Inc. All rights reserved. Outline 88 ShippingHub.java (19 of 28) 426 427 // remove a Parcel 428 private void removeJButtonActionPerformed( ActionEvent event ) 429 { 430 // retrieve the state of the current Parcel 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 { 438 // index of current Parcel 439 int index = parcelStateArrayList.indexOf( 440 String.valueOf( newParcel.getParcelID() ) ); 441 parcelStateArrayList.remove( index ); 442 443 // reset JList data 444 parcelStateJList.setListData( 445 parcelStateArrayList.toArray() ); 446 447 } // end if 448 449 // remove current Parcel from ArrayList 450 parcelsArrayList.remove( position ); If the state in the parcelStateJComboBox is the same as the state of the Parcel to be removed, remove the Parcel ID from the parcelStateArrayList The setListData method displays an array’s elements in a JList Remove the current Parcel from the parcelsArrayList

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

90  2004 Prentice Hall, Inc. All rights reserved. Outline 90 ShippingHub.java (21 of 28) 475 // allow user to edit Parcel information 476 private void editJButtonActionPerformed( ActionEvent event ) 477 { 478 // disable appropriate components 479 setJButtons( false ); 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 private void updateJButtonActionPerformed( ActionEvent event ) 489 { 490 setParcelData(); // update information 491 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 The setParcelData method sets the Parcel ’s properties to the values entered by the user

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

92  2004 Prentice Hall, Inc. All rights reserved. Outline 92 ShippingHub.java (23 of 28) 522 // move to next Parcel 523 private void nextJButtonActionPerformed( ActionEvent event ) 524 { 525 if ( position < parcelsArrayList.size() - 1 ) 526 { 527 position++; // move position forward by 1 528 } 529 else 530 { 531 position = 0; // go to first element in list 532 } 533 534 // load information of Parcel 535 loadParcel(); 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 When the user clicks the Next > JButton, increment the position. If the position was the index of the last object in the ArrayList, set position to zero. Display the next Parcel Retrieve the state code selected in parcelStateJComboBox

93  2004 Prentice Hall, Inc. All rights reserved. Outline 93 ShippingHub.java (24 of 28) 547 // create iterator 548 Iterator parcelIterator = parcelsArrayList.iterator(); 549 550 // clear parcelStateArrayList 551 parcelStateArrayList.clear(); 552 553 // create parcelStateArrayList with ID numbers of Parcels 554 // to be displayed 555 while ( parcelIterator.hasNext() ) 556 { 557 // create temporary reference to Parcel object 558 Parcel currentParcel = ( Parcel ) parcelIterator.next(); 559 560 // add parcel ID to ArrayList 561 if ( state.equals( currentParcel.getState() ) 562 { 563 parcelStateArrayList.add( String.valueOf( 564 currentParcel.getParcelID() ) ); 565 } 566 567 } // end while 568 569 // display ArrayList in parcelStateJList 570 parcelStateJList.setListData( 571 parcelStateArrayList.toArray() ); 572 573 } // end method parcelStateJComboBoxActionPerformed Create a new Iterator object Remove all items from the parcelStateArrayList Iterate through every element in the ArrayList Retrieve the next object in the parcelsArrayList Add the Parcel ID to the parcelStateArrayList Display the parcel ID numbers in the parcelStateArrayList

94  2004 Prentice Hall, Inc. All rights reserved. Outline 94 ShippingHub.java (25 of 28) 574 575 // set all information about the Parcel 576 private void setParcelData() 577 { 578 newParcel.setName( nameJTextField.getText() ); 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 589 private void loadParcel() 590 { 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() ); Retrieve object at index position in parcelsArrayList

95  2004 Prentice Hall, Inc. All rights reserved. Outline 95 ShippingHub.java (26 of 28) 600 cityJTextField.setText( newParcel.getCity() ); 601 stateJComboBox.setSelectedItem( newParcel.getState() ); 602 zipJTextField.setText( String.valueOf( newParcel.getZip() ) ); 603 604 } // end method loadParcel 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 );

96  2004 Prentice Hall, Inc. All rights reserved. Outline 96 ShippingHub.java (27 of 28) 626 627 // disable navigation if not multiple packages 628 if ( parcelsArrayList.size() < 2 ) 629 { 630 nextJButton.setEnabled( false ); 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

97  2004 Prentice Hall, Inc. All rights reserved. Outline 97 ShippingHub.java (28 of 28) 644 // make editable or uneditable components 645 // in parcelInformationJPanel 646 private void parcelInformationJPanelEditable( boolean editable ) 647 { 648 nameJTextField.setEditable( editable ); 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


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application."

Similar presentations


Ads by Google