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

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Advertisements

Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
Chapter 8: String Manipulation
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 27 - Phone Book Application Introducing Multimedia.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 32.1 Reviewing the Bookstore Application 32.2.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 18.1 Test-Driving the Microwave Oven Application.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Shipping Hub Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 2.1 Test-Driving the Welcome Application 2.2.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 11.1 Test-Driving the Security Panel Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 11.1 Test-Driving the Interest Calculator Application 11.2 Essentials of Counter-Controlled Repetition.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Presentation transcript:

© Copyright 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

© Copyright 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.

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Shipping Hub Application (Cont.) Running the completed application –Open a Command Prompt Change to ShippingHub directory Type java ShippingHub

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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Shipping Hub Application (Cont.) Figure 19.3 Entering a new Parcel ’s information. New Parcel listed in JList

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Shipping Hub Application (Cont.) Figure 19.5 New information is stored when Update JButton is clicked. Updated zip code

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

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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Parcel Class (Cont.)

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved JList Component (Cont.) Figure 19.9 Declaring new references to a JList and a JScrollPane.

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved JList Component (Cont.) Figure 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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Mnemonics (Keyboard Shortcuts) Figure 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

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

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

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

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

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

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

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

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

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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure 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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Displaying the Parcel ’s number and arrival time. Displaying the arrival time and Parcel ’s ID number in the JTextField s

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Creating a Parcel object. Create a new Parcel object with an ID and arrival time

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Adding a Parcel to the ArrayList. Adding a Parcel object to the ArrayList Use ArrayList ’s add method.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Newly scanned items have the Parcel ’s arrival time and ID displayed. Arrival time and ID now displayed

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Adding a new Parcel with the Add JButton. New Parcel not yet added to JList Panel information is made uneditable

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Removing a Parcel from the ArrayList. Removing the current Parcel from the ArrayList Use ArrayList ’s remove method ArrayList updates its indices automatically

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Adding a Parcel to parcelsArrayList.

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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Removing and inserting a Parcel to update data. Updating the ArrayList with the new Parcel ’s information

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Entering a new Parcel.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Modifying a Parcel after the Edit JButton is clicked. City name is modified

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Storing changes to a Parcel information when the Update JButton is clicked. New city name is stored in parcelsArrayList

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Display next or first Parcel after a Parcel is removed. Call method Parcel to display next Parcel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Calling the loadParcel method to display the previous Parcel ’s information.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Calling the loadParcel method to display the next Parcel ’s information.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Displaying the next Parcel using the Next > JButton. Save the changes to your code Compile and run in the Command Prompt

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Shipping Hub Application (Cont.) Figure Displaying the previous Parcel using the < Back JButton.

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Iterators (Cont.) Figure Declaring a variable to contain the state selected. Obtain the state selected in parcelStateJComboBox

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

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

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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Iterators (Cont.) Figure 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

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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Iterators (Cont.) Figure Adding an item to parcelStateJList. Selecting the state in parcelStateJComboBox that was selected by the user in stateJComboBox

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Iterators (Cont.) Figure Updating items in parcelStateJList. Selecting the state in parcelStateJComboBox that was selected by the user in stateJComboBox

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Iterators (Cont.) Figure 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Iterators (Cont.) Figure 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

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

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

 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; // JPanel to contain Parcel information 16 private JPanel parcelInformationJPanel; // JLabel and JTextField to display Parcel identification number 19 private JLabel parcelIDJLabel; 20 private JTextField parcelIDJTextField; // JLabel and JTextField for name 23 private JLabel nameJLabel; 24 private JTextField nameJTextField; 25 Import the package containing class ArrayList

 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; // JLabel and JTextField for city 31 private JLabel cityJLabel; 32 private JTextField cityJTextField; // JLabel and JTextField for state 35 private JLabel stateJLabel; 36 private JComboBox stateJComboBox; // JLabel and JTextField for zip code 39 private JLabel zipJLabel; 40 private JTextField zipJTextField; // JPanel for Parcel number by state 43 private JPanel parcelStateJPanel; // 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

 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; // array contains options for parcelStateJComboBox 60 private String[] states = { "AL", "FL", "GA", "KY", "MS", "NC", 61 "SC", "TN", "VA", "WV" }; // Parcel object contains data for newly entered Parcels 64 private Parcel newParcel; // ArrayList contains Parcel objects entered by user 67 private ArrayList parcelsArrayList = new ArrayList(); // ArrayList used to modify and display the Parcel objects 70 // for a specific state 71 private ArrayList parcelStateArrayList = new ArrayList(); 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

 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; // no-argument constructor 80 public ShippingHub() 81 { 82 createUserInterface(); 83 } // 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(); // enable explicit positioning of GUI components 92 contentPane.setLayout( null ); // 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. 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 ); // 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 ); // set up parcelIDJLabel 115 parcelIDJLabel = new JLabel(); 116 parcelIDJLabel.setBounds( 15, 27, 84, 24 ); 117 parcelIDJLabel.setText( "Parcel ID:" ); 118 parcelInformationJPanel.add( parcelIDJLabel ); // 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. Outline 75 ShippingHub.java (6 of 28) // set up nameJLabel 127 nameJLabel = new JLabel(); 128 nameJLabel.setBounds( 15, 65, 66, 25 ); 129 nameJLabel.setText( "Name:" ); 130 parcelInformationJPanel.add( nameJLabel ); // set up nameJTextField 133 nameJTextField = new JTextField(); 134 nameJTextField.setBounds( 80, 65, 386, 21 ); 135 nameJTextField.setEditable( false ); 136 parcelInformationJPanel.add( nameJTextField ); // set up addressJLabel 139 addressJLabel = new JLabel(); 140 addressJLabel.setBounds( 15, 103, 66, 25 ); 141 addressJLabel.setText( "Address:" ); 142 parcelInformationJPanel.add( addressJLabel ); // 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. 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 ); // set up cityJTextField 157 cityJTextField = new JTextField(); 158 cityJTextField.setBounds( 80, 141, 117, 21 ); 159 cityJTextField.setEditable( false ); 160 parcelInformationJPanel.add( cityJTextField ); // set up stateJLabel 163 stateJLabel = new JLabel(); 164 stateJLabel.setBounds( 215, 141, 47, 24 ); 165 stateJLabel.setText( "State:" ); 166 parcelInformationJPanel.add( stateJLabel ); // 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. 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 ); // set up zipJTextField 181 zipJTextField = new JTextField(); 182 zipJTextField.setBounds( 390, 141, 76, 21 ); 183 zipJTextField.setEditable( false ); 184 parcelInformationJPanel.add( zipJTextField ); // 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 ); // 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. Outline 78 ShippingHub.java (9 of 28) 198 parcelStateJComboBox.addActionListener( 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 } } // end anonymous inner class ); // end call to addActionListener // set up parcelStateJList 214 parcelStateJList = new JList(); // 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

 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( 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 } } // end anonymous inner class ); // end call to addActionListener // 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’

 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( 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 } } // end anonymous inner class ); // end call to addActionListener // 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’

 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 } } // end anonymous inner class ); // end call to addActionListener // 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’

 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 } } // end anonymous inner class ); // end call to addActionListener // 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’

 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 } } // end anonymous inner class ); // end call to addActionListener // 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’

 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 } } // end anonymous inner class ); // end call to addActionListener // 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’

 2004 Prentice Hall, Inc. All rights reserved. Outline 85 ShippingHub.java (16 of 28) 353 nextJButton.addActionListener( 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 } } // end anonymous inner class ); // end call to addActionListener // 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 } // end method createUserInterface 373

 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(); // set arrival time 381 Date now = new Date(); 382 arrivedAtJTextField.setText( now.toString() ); // give Parcel unique ID number 385 parcelID++; 386 parcelIDJTextField.setText( String.valueOf( parcelID ) ); // create new Parcel object 389 newParcel = new Parcel( parcelID, 390 arrivedAtJTextField.getText() ); // disable appropriate components 393 setJButtons( false ); // 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

 2004 Prentice Hall, Inc. All rights reserved. Outline 87 ShippingHub.java (18 of 28) 399 // grab focus 400 nameJTextField.requestFocusInWindow(); } // end method scanNewJButtonActionPerformed // add a new Parcel 405 private void addJButtonActionPerformed( ActionEvent event ) 406 { 407 // set information for new Parcel 408 setParcelData(); // add new Parcel to parcelsArrayList 411 parcelsArrayList.add( newParcel ); 412 position = parcelsArrayList.size() - 1; // disable or make uneditable appropriate components 415 addJButton.setEnabled( false ); 416 parcelInformationJPanelEditable( false ); // enable appropriate components 419 setJButtons( true ); // change selected item in parcelStateJComboBox 422 parcelStateJComboBox.setSelectedIndex( 423 stateJComboBox.getSelectedIndex() ); } // 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

 2004 Prentice Hall, Inc. All rights reserved. Outline 88 ShippingHub.java (19 of 28) // remove a Parcel 428 private void removeJButtonActionPerformed( ActionEvent event ) 429 { 430 // retrieve the state of the current Parcel 431 String stateSelected = newParcel.getState(); // 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 ); // reset JList data 444 parcelStateJList.setListData( 445 parcelStateArrayList.toArray() ); } // end if // 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

 2004 Prentice Hall, Inc. All rights reserved. Outline 89 ShippingHub.java (20 of 28) // 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 } loadParcel(); 461 } 462 else 463 { 464 // if no other Parcels remain 465 clearComponents(); 466 } setJButtons( true ); // enabled appropriate JButtons // set focus to scanNewJButton 471 scanNewJButton.requestFocusInWindow(); } // 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

 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 ); // make user able to update Parcel information 482 updateJButton.setEnabled( true ); 483 parcelInformationJPanelEditable( true ); } // end method editJButtonActionPerformed // move to next Parcel 488 private void updateJButtonActionPerformed( ActionEvent event ) 489 { 490 setParcelData(); // update information // enable or make editable appropriate components 493 setJButtons( true ); // 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

 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() ); } // end method updateJButtonActionPerformed // move to previous Parcel 506 private void backJButtonActionPerformed( ActionEvent event ) 507 { 508 if ( position > 0 ) 509 { 510 position--; // move position back by } 512 else // go to last element in list 513 { 514 position = parcelsArrayList.size() - 1; 515 } // set and load Parcel 518 loadParcel(); } // 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

 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 } 529 else 530 { 531 position = 0; // go to first element in list 532 } // load information of Parcel 535 loadParcel(); } // end method nextJButtonActionPerformed // 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

 2004 Prentice Hall, Inc. All rights reserved. Outline 93 ShippingHub.java (24 of 28) 547 // create iterator 548 Iterator parcelIterator = parcelsArrayList.iterator(); // clear parcelStateArrayList 551 parcelStateArrayList.clear(); // 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(); // add parcel ID to ArrayList 561 if ( state.equals( currentParcel.getState() ) 562 { 563 parcelStateArrayList.add( String.valueOf( 564 currentParcel.getParcelID() ) ); 565 } } // end while // display ArrayList in parcelStateJList 570 parcelStateJList.setListData( 571 parcelStateArrayList.toArray() ); } // 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

 2004 Prentice Hall, Inc. All rights reserved. Outline 94 ShippingHub.java (25 of 28) // 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() ) ); } // end method setParcelData // display all information about the Parcel 589 private void loadParcel() 590 { 591 // retrieve package from list 592 newParcel = ( Parcel ) parcelsArrayList.get( position ); // 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

 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() ) ); } // end method loadParcel // 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( "" ); } // end method clearComponents // 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. Outline 96 ShippingHub.java (27 of 28) // disable navigation if not multiple packages 628 if ( parcelsArrayList.size() < 2 ) 629 { 630 nextJButton.setEnabled( false ); 631 backJButton.setEnabled( false ); 632 } // 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 } } // end method setJButtons 643

 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 ); } // end method parcelInformationJPanelEditable // main method 657 public static void main( String[] args ) 658 { 659 ShippingHub application = new ShippingHub(); 660 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class ShippingHub