+ All Categories

Download - GUI Arraylist

Transcript
Page 1: GUI Arraylist

1

Outline19.1 Test-Driving the Shipping Hub Application19.2 Parcel Class19.3 JList Component19.4 Using Mnemonics (Keyboard Shortcuts)19.5 Collections19.6 Constructing the Shipping Hub Application19.7 Using Iterators19.8 Wrap-Up

Tutorial 19 – Shipping Hub Application

Introducing Collections, ArrayList and Iterators

Page 2: GUI Arraylist

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.

Page 3: GUI Arraylist

3

19.1 Test-Driving the Shipping Hub Application

Application Requirements A shipping company receives parcels at its shipping hub. Parcels will be represented as Parcel objects, where class Parcel is provided for you. The company then ships these Parcels to a distribution center in one of the following states: Alabama, Florida, Georgia, Kentucky, Mississippi, North Carolina, South Carolina, Tennessee, Virginia or West Virginia. The company needs an application to track the Parcels that pass through its shipping hub. When the user clicks the application’s Scan New JButton, the application generates an ID and arrival time for the new Parcel. Once a Parcel has been scanned, the user should be able to enter the recipient’s name and shipping address for the Parcel. JButtons should be provided for the user to remove or modify the information of Parcels that have already been scanned. The user should be able to navigate through the list of scanned Parcels by using the < Back or Next > JButtons. Finally, users should be able to view a list of all Parcels destined for a particular state.

Page 4: GUI Arraylist

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

Page 5: GUI Arraylist

5

19.1 Test-Driving the Shipping Hub Application (Cont.)

Figure 19.1 Running the completed Shipping Hub application.

JPanel contains fields for user to view or edit the Parcel JComboBoxes list

available states

Page 6: GUI Arraylist

6

19.1 Test-Driving the Shipping Hub Application (Cont.)

Figure 19.2 Scanning a new Parcel.

User can now enter Parcel information

Page 7: GUI Arraylist

7

19.1 Test-Driving the Shipping Hub Application (Cont.)

Figure 19.3 Entering a new Parcel’s information.

New Parcel listed in JList

Page 8: GUI Arraylist

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

Page 9: GUI Arraylist

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

Page 10: GUI Arraylist

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

Page 11: GUI Arraylist

11

19.1 Test-Driving the Shipping Hub Application (Cont.)

Figure 19.7 Viewing all Parcels going to South Carolina.

All Parcels being sent to South Carolina

Page 12: GUI Arraylist

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.

Page 13: GUI Arraylist

13

19.2 Parcel Class (Cont.)Method Description getName/setName Provides access to instance variable name (a

String). getAddress/ setAddress

Provides access to instance variable address (a String).

getCity/setCity Provides access to instance variable city (a String).

getState/-setState

Provides access to instance variable state (a String). States are represented as two-letter abbreviations.

getZip/setZip Provides access to instance variable zip (an int). getParcelID Provides access to instance variable parcelID (an

int). getArrivalTime Provides access to instance variable arrivalTime

(a String). Parcel Constructor that is used to create a new Parcel object

with two arguments parcelID (an int) and arrivalTime (a String).

Figure 19.8 Methods listing for class Parcel.

Page 14: GUI Arraylist

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.

Page 15: GUI Arraylist

15

19.3 JList Component (Cont.)

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

Page 16: GUI Arraylist

16

19.3 JList Component (Cont.)

Figure 19.10 Creating a new JList object.

Initializing parcelStateJList with a new JList

Page 17: GUI Arraylist

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

Page 18: GUI Arraylist

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

Page 19: GUI Arraylist

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

Page 20: GUI Arraylist

20

19.4 Using Mnemonics (Keyboard Shortcuts)

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

Mnemonic underlined

Page 21: GUI Arraylist

21

19.4 Using Mnemonics (Keyboard Shortcuts)

Figure 19.14 JButtons with mnemonics.

Page 22: GUI Arraylist

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.

Page 23: GUI Arraylist

23

19.6 Constructing the Shipping Hub Application

When the Edit JButton is clickedEnable the input components (nameJTextField, addressJTextField,

cityJTextField, zipJTextField and stateJComboBox)

When the Update JButton is clickedStore the new name, address, city, state and zip code values in the Parcel objectUpdate the parcelStateJList based on the user’s changes

When the < Back JButton is clickedDisplay 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 clickedDisplay 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 JComboBoxIterate through each Parcel in the parcelsArrayListAdd IDs of Parcels destined for the selected state to the parcelStateArrayListDisplay the Parcel iDs from the parcelStateArrayList in the parcelStateJList

Page 24: GUI Arraylist

24

19.6 Constructing the Shipping Hub Application (Cont.)

Action Component/Class/Object Event/Method Label the application’s components arrivedAtJLabel,

parcelIDJLabel, nameJLabel, addressJLabel, cityJLabel, stateJLabel, zipJLabel, parcelInformationJPanel, parcelStateJPanel

Create parcelsArrayList to contain all Parcels being sent

parcelsArrayList

Create parcelStateArrayList to contain the Parcels being sent to a specific state

parcelStateArrayList

Display the arrival time and the ID number of the new Parcel

arrivedAtJTextField, parcelIDJTextField

User clicks Scan New JButton

Create a Parcel object for the new entry

newParcel

Retrieve input values from the user nameJTextField, addressJTextField, cityJTextField, zipJTextField, stateJComboBox

User clicks Add JButton

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

Page 25: GUI Arraylist

25

19.6 Constructing the Shipping Hub Application (Cont.)

Store input values in the new Parcel object

newParcel, parcelsArrayList

Add the new Parcel to the parcels-ArrayList

parcelsArrayList

Add the Parcel ID to the parcelStateJList

parcelStateJList

Remove the Parcel ID from the parcelStateJList

parcelStateJList User clicks Remove JButton

Remove the Parcel from the parcelsArrayList

parcelsArrayList

Retrieve values from the next Parcel

newParcel, parcelsArrayList

Display the next Parcel nameJTextField, addressJTextField, cityJTextField, zipJTextField, stateJComboBox

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

Page 26: GUI Arraylist

26

19.6 Constructing the Shipping Hub Application (Cont.)

Enable the input components nameJTextField, addressJTextField, cityJTextField, zipJTextField, stateJComboBox

User clicks Edit JButton

Retrieve input values from the user

nameJTextField, addressJTextField, cityJTextField, zipJTextField, stateJComboBox

User clicks Update JButton

Store the new input values in the Parcel object

newParcel, parcelsArrayList

Update the parcelStateJList based on the user’s changes

parcelStateJList

Retrieve values from the previous Parcel

newParcel, parcelsArrayList

User clicks < Back JButton

Display the previous Parcel in the parcelsArrayList

nameJTextField, addressJTextField, cityJTextField, zipJTextField, stateJComboBox

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

Page 27: GUI Arraylist

27

19.6 Constructing the Shipping Hub Application (Cont.)

Retrieve values from the next Parcel

newParcel, parcelsArrayList

User clicks Next > JButton

Display the next Parcel in the parcelsArrayList

nameJTextField, addressJTextField, cityJTextField, zipJTextField, stateJComboBox

Iterate through each Parcel in the parcelsArrayList

parcelsArrayList User selects a state in -JComboBox

Add IDs of Parcels destined for the selected state to the parcel-StateArrayList

parcelStateArray-List

Display the Parcel iDs from the parcelStateArrayList in the parcelStateJList

parcelStateJList

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

Page 28: GUI Arraylist

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

Page 29: GUI Arraylist

29

19.6 Constructing the Shipping Hub Application (Cont.)

Figure 19.17 Creating the ArrayList.

Creating the ArrayList

Page 30: GUI Arraylist

30

19.6 Constructing the Shipping Hub Application (Cont.)

Figure 19.18 Entering information for a new Parcel.

Parcel’s arrival time and ID not yet displayed

• Save the changes to your code• Compile and run in the Command Prompt

Page 31: GUI Arraylist

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

Page 32: GUI Arraylist

32

19.6 Constructing the Shipping Hub Application (Cont.)

Figure 19.19 Displaying the Parcel’s number and arrival time.

Displaying the arrival time and Parcel’s ID number in the JTextFields

Page 33: GUI Arraylist

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

Page 34: GUI Arraylist

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.

Page 35: GUI Arraylist

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

Page 36: GUI Arraylist

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

Page 37: GUI Arraylist

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

Page 38: GUI Arraylist

38

19.6 Constructing the Shipping Hub Application (Cont.)

Figure 19.25 Adding a Parcel to parcelsArrayList.

Page 39: GUI Arraylist

39

19.6 Constructing the Shipping Hub Application (Cont.)

Figure 19.26 Removing a Parcel from parcelsArrayList.

Page 40: GUI Arraylist

40

19.6 Constructing the Shipping Hub Application (Cont.)

• To edit parcel information– Click Edit JButton

• Enable all JTextFields, except for ID number and arrival time

– Enter new information

– Click Update JButton to submit new information

Page 41: GUI Arraylist

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

Page 42: GUI Arraylist

42

19.6 Constructing the Shipping Hub Application (Cont.)

Figure 19.28 Entering a new Parcel.

Page 43: GUI Arraylist

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

Page 44: GUI Arraylist

44

19.6 Constructing the Shipping Hub Application (Cont.)

Figure 19.30 Storing changes to a Parcel information when the Update JButtonis clicked.

New city name is stored in parcelsArrayList

Page 45: GUI Arraylist

45

19.6 Constructing the Shipping Hub Application (Cont.)

• Displaying a Parcel after a removal– When element removed is the last element in the

ArrayList• Display first Parcel in ArrayList

– When element removed is the only element in the ArrayList

• Clear application’s components

– Otherwise, display the next Parcel

Page 46: GUI Arraylist

46

19.6 Constructing the Shipping Hub Application (Cont.)

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

when the last Parcel in parcelsArrayList is removed.

Set position to display first Parcel when last Parcel is removed

Clear application’s components when there are no more Parcels

Page 47: GUI Arraylist

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

Page 48: GUI Arraylist

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

Page 49: GUI Arraylist

49

19.6 Constructing the Shipping Hub Application (Cont.)

Figure 19.34 Calling the loadParcel method to display the previous Parcel’s information.

Page 50: GUI Arraylist

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

Page 51: GUI Arraylist

51

19.6 Constructing the Shipping Hub Application (Cont.)

Figure 19.36 Calling the loadParcel method to display the next Parcel’s information.

Page 52: GUI Arraylist

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

Page 53: GUI Arraylist

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

Page 54: GUI Arraylist

54

19.6 Constructing the Shipping Hub Application (Cont.)

Figure 19.39 Displaying the previous Parcel using the < Back JButton.

Page 55: GUI Arraylist

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

Page 56: GUI Arraylist

56

19.7 Using Iterators (Cont.)

Figure 19.40 Declaring a variable to contain the state selected.

Obtain the state selected in parcelStateJComboBox

Page 57: GUI Arraylist

57

19.7 Using Iterators (Cont.)

Figure 19.41 Declaring an Iterator.

Declaring the Iterator for the while repetition statement

Page 58: GUI Arraylist

58

19.7 Using Iterators (Cont.)

Figure 19.42 Clearing parcelStateArrayList.

• clear method removes all Parcels from the ArrayList

Removing all elements from parcelStateArrayList

Page 59: GUI Arraylist

59

19.7 Using Iterators (Cont.)

Figure 19.43 Iterator used in a while statement.

Using iterator in the while condition

Page 60: GUI Arraylist

60

19.7 Using Iterators (Cont.)

Figure 19.44 Creating a reference using parcelIterator.next.

Retrieving the next Parcel in the parcelIterator

Page 61: GUI Arraylist

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

Page 62: GUI Arraylist

62

19.7 Using Iterators (Cont.)

Figure 19.46 Displaying all Parcels going to the selected state.

• toArray method an array of Objects

Displaying the parcelID in the parcelStateJList

Page 63: GUI Arraylist

63

19.7 Using Iterators (Cont.)

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

• Save the changes to your code• Compile and run in the Command Prompt

Page 64: GUI Arraylist

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

Page 65: GUI Arraylist

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

Page 66: GUI Arraylist

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

Page 67: GUI Arraylist

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 pracelStateArrayListAssigning the remaining Parcel IDs to

the pracelStateArrayList

Page 68: GUI Arraylist

68

19.7 Using Iterators (Cont.)

Figure 19.52 Viewing all Parcels destined for Florida.

Scrollbar appears for more than four Parcel IDs

Page 69: GUI Arraylist

69

19.7 Using Iterators (Cont.)

Figure 19.53 Resulting JList after a Parcel has been removed.

Parcel 3 is removed

Page 70: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline70

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

Page 71: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline71

ShippingHub.java(2 of 28)

26 // JLabel and JTextField for address27 private JLabel addressJLabel;28 private JTextField addressJTextField;29

30 // JLabel and JTextField for city31 private JLabel cityJLabel;32 private JTextField cityJTextField;33

34 // JLabel and JTextField for state35 private JLabel stateJLabel;36 private JComboBox stateJComboBox;37

38 // JLabel and JTextField for zip code39 private JLabel zipJLabel;40 private JTextField zipJTextField;41

42 // JPanel for Parcel number by state43 private JPanel parcelStateJPanel;44

45 // JComboBox, JList and JScrollPane for Parcel number46 private JComboBox parcelStateJComboBox;47 private JList parcelStateJList; 48 private JScrollPane parcelStateJScrollPane;49

parcelstateJList and parcelStateJScrollPane used to display a scrollable list of Parcels bound for the same state

Page 72: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline72

ShippingHub.java(3 of 28)

50 // JButtons to manipulate Parcels51 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 parcelStateJComboBox60 private String[] states = { "AL", "FL", "GA", "KY", "MS", "NC",61 "SC", "TN", "VA", "WV" };62

63 // Parcel object contains data for newly entered Parcels64 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 objects70 // for a specific state 71 private ArrayList parcelStateArrayList = new ArrayList(); 72

73 private int parcelID = 0; // ID for new Parcels74

ArrayList contains the Parcels entered by user

ArrayList contains Parcel IDs destined for a specific state

Page 73: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline73

ShippingHub.java(4 of 28)

75 // position used to track location when the user is76 // browsing through the list of Parcels77 private int position = 0;78 79 // no-argument constructor80 public ShippingHub()81 {82 createUserInterface();83 }84 85 // create and position GUI components; register event handlers86 private void createUserInterface()87 {88 // get content pane for attaching GUI components89 Container contentPane = getContentPane();90

91 // enable explicit positioning of GUI components 92 contentPane.setLayout( null );93

94 // set up arrivedAtJLabel95 arrivedAtJLabel = new JLabel();96 arrivedAtJLabel.setBounds( 19, 14, 74, 24 );97 arrivedAtJLabel.setText( "Arrived at:" );98 contentPane.add( arrivedAtJLabel );99

Page 74: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline74

ShippingHub.java(5 of 28)

100 // set up arrivedAtJTextField101 arrivedAtJTextField = new JTextField();102 arrivedAtJTextField.setBounds( 89, 14, 197, 21 );103 arrivedAtJTextField.setEditable( false );104 contentPane.add( arrivedAtJTextField );105 106 // set up parcelInformationJPanel107 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 parcelIDJLabel115 parcelIDJLabel = new JLabel();116 parcelIDJLabel.setBounds( 15, 27, 84, 24 );117 parcelIDJLabel.setText( "Parcel ID:" );118 parcelInformationJPanel.add( parcelIDJLabel );119 120 // set up parcelIDJTextField121 parcelIDJTextField = new JTextField();122 parcelIDJTextField.setBounds( 80, 27, 386, 21 );123 parcelIDJTextField.setEditable( false );124 parcelInformationJPanel.add( parcelIDJTextField );

Page 75: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline75

ShippingHub.java(6 of 28)

125

126 // set up nameJLabel127 nameJLabel = new JLabel();128 nameJLabel.setBounds( 15, 65, 66, 25 );129 nameJLabel.setText( "Name:" );130 parcelInformationJPanel.add( nameJLabel );131

132 // set up nameJTextField133 nameJTextField = new JTextField();134 nameJTextField.setBounds( 80, 65, 386, 21 );135 nameJTextField.setEditable( false );136 parcelInformationJPanel.add( nameJTextField );137 138 // set up addressJLabel139 addressJLabel = new JLabel();140 addressJLabel.setBounds( 15, 103, 66, 25 );141 addressJLabel.setText( "Address:" );142 parcelInformationJPanel.add( addressJLabel );143

144 // set up addressJTextField145 addressJTextField = new JTextField();146 addressJTextField.setBounds( 80, 103, 386, 21 );147 addressJTextField.setEditable( false );148 parcelInformationJPanel.add( addressJTextField );149

Page 76: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline76

ShippingHub.java(7 of 28)

150 // set up cityJLabel151 cityJLabel = new JLabel();152 cityJLabel.setBounds( 15, 141, 37, 24 );153 cityJLabel.setText( "City:" );154 parcelInformationJPanel.add( cityJLabel );155 156 // set up cityJTextField157 cityJTextField = new JTextField();158 cityJTextField.setBounds( 80, 141, 117, 21 );159 cityJTextField.setEditable( false );160 parcelInformationJPanel.add( cityJTextField );161 162 // set up stateJLabel163 stateJLabel = new JLabel();164 stateJLabel.setBounds( 215, 141, 47, 24 );165 stateJLabel.setText( "State:" );166 parcelInformationJPanel.add( stateJLabel );167 168 // set up stateJComboBox169 stateJComboBox = new JComboBox( states );170 stateJComboBox.setBounds( 260, 141, 70, 21 );171 stateJComboBox.setEnabled( false );172 parcelInformationJPanel.add( stateJComboBox );173

Page 77: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline77

ShippingHub.java(8 of 28)

174 // set up zipJLabel175 zipJLabel = new JLabel();176 zipJLabel.setBounds( 355, 141, 28, 24 );177 zipJLabel.setText( "Zip:" );178 parcelInformationJPanel.add( zipJLabel );179 180 // set up zipJTextField181 zipJTextField = new JTextField();182 zipJTextField.setBounds( 390, 141, 76, 21 );183 zipJTextField.setEditable( false );184 parcelInformationJPanel.add( zipJTextField );185

186 // set up parcelStateJPanel187 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 parcelStateJComboBox195 parcelStateJComboBox = new JComboBox( states );196 parcelStateJComboBox.setBounds( 19, 29, 98, 21 );197 parcelStateJPanel.add( parcelStateJComboBox );

Page 78: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline78

ShippingHub.java(9 of 28)

198 parcelStateJComboBox.addActionListener(199 200 new ActionListener() // anonymous inner class201 {202 // event handler called when parcelStateJComboBox203 // is selected204 public void actionPerformed( ActionEvent event )205 {206 parcelStateJComboBoxActionPerformed( event );207 }208 209 } // end anonymous inner class210 211 ); // end call to addActionListener212 213 // set up parcelStateJList 214 parcelStateJList = new JList(); 215

216 // set up parcelStateJScrollPane217 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

Page 79: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline79

ShippingHub.java(10 of 28)

221 // set up scanNewJButton222 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 class230 {231 // event handler called when scanNewJButton is pressed232 public void actionPerformed( ActionEvent event )233 {234 scanNewJButtonActionPerformed( event );235 }236 237 } // end anonymous inner class238 239 ); // end call to addActionListener240

241 // set up addJButton242 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’

Page 80: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline80

ShippingHub.java(11 of 28)

246 addJButton.setEnabled( false );247 contentPane.add( addJButton );248 addJButton.addActionListener( 249 250 new ActionListener() // anonymous inner class251 {252 // event handler called when addJButton is pressed253 public void actionPerformed( ActionEvent event )254 {255 addJButtonActionPerformed( event );256 }257 258 } // end anonymous inner class259 260 ); // end call to addActionListener261

262 // set up removeJButton263 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’

Page 81: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline81

ShippingHub.java(12 of 28)

271 new ActionListener() // anonymous inner class272 {273 // event handler called when removeJButton is pressed274 public void actionPerformed( ActionEvent event )275 {276 removeJButtonActionPerformed( event );277 }278 279 } // end anonymous inner class280 281 ); // end call to addActionListener282

283 // set up editJButton284 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’

Page 82: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline82

ShippingHub.java(13 of 28)

292 new ActionListener() // anonymous inner class293 {294 // event handler called when editJButton is pressed295 public void actionPerformed( ActionEvent event )296 {297 editJButtonActionPerformed( event );298 }299 300 } // end anonymous inner class301 302 ); // end call to addActionListener303

304 // set up updateJButton305 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’

Page 83: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline83

ShippingHub.java(14 of 28)

313 new ActionListener() // anonymous inner class314 {315 // event handler called when updateJButton is pressed316 public void actionPerformed( ActionEvent event )317 {318 updateJButtonActionPerformed( event );319 }320 321 } // end anonymous inner class322 323 ); // end call to addActionListener324

325 // set up backJButton326 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’

Page 84: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline84

ShippingHub.java(15 of 28)

334 new ActionListener() // anonymous inner class335 {336 // event handler called when backJButton is pressed337 public void actionPerformed( ActionEvent event )338 {339 backJButtonActionPerformed( event );340 }341 342 } // end anonymous inner class343 344 ); // end call to addActionListener345

346 // set up nextJButton347 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’

Page 85: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline85

ShippingHub.java(16 of 28)

353 nextJButton.addActionListener( 354 355 new ActionListener() // anonymous inner class356 {357 // event handler called when nextJButton is pressed358 public void actionPerformed( ActionEvent event )359 {360 nextJButtonActionPerformed( event );361 }362 363 } // end anonymous inner class364 365 ); // end call to addActionListener366 367 // set properties of application's window368 setTitle( "Shipping Hub" ); // set title bar string369 setSize( 663, 313 ); // set window size370 setVisible( true ); // display window371 372 } // end method createUserInterface373

Page 86: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline86

ShippingHub.java(17 of 28)

374 // prepare to scan a new Parcel375 private void scanNewJButtonActionPerformed( ActionEvent event )376 {377 // clear JTextFields378 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 components393 setJButtons( false );394

395 // enable or make editable appropriate components396 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

Page 87: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline87

ShippingHub.java(18 of 28)

399 // grab focus400 nameJTextField.requestFocusInWindow();401 402 } // end method scanNewJButtonActionPerformed403 404 // add a new Parcel405 private void addJButtonActionPerformed( ActionEvent event )406 {407 // set information for new Parcel408 setParcelData();409 410 // add new Parcel to parcelsArrayList 411 parcelsArrayList.add( newParcel ); 412 position = parcelsArrayList.size() - 1;413 414 // disable or make uneditable appropriate components415 addJButton.setEnabled( false );416 parcelInformationJPanelEditable( false );417

418 // enable appropriate components419 setJButtons( true );420 421 // change selected item in parcelStateJComboBox422 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

Page 88: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline88

ShippingHub.java(19 of 28)

426 427 // remove a Parcel428 private void removeJButtonActionPerformed( ActionEvent event )429 {430 // retrieve the state of the current Parcel431 String stateSelected = newParcel.getState();432

433 // if same state is selected, remove ID number from434 // 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 if448 449 // remove current Parcel from ArrayList450 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

Page 89: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline89

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 remain465 clearComponents(); 466 } 467

468 setJButtons( true ); // enabled appropriate JButtons469

470 // set focus to scanNewJButton471 scanNewJButton.requestFocusInWindow();472 473 } // end method removeJButtonActionPerformed474

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

Page 90: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline90

ShippingHub.java(21 of 28)

475 // allow user to edit Parcel information476 private void editJButtonActionPerformed( ActionEvent event )477 {478 // disable appropriate components479 setJButtons( false );480

481 // make user able to update Parcel information482 updateJButton.setEnabled( true );483 parcelInformationJPanelEditable( true );484 485 } // end method editJButtonActionPerformed486

487 // move to next Parcel488 private void updateJButtonActionPerformed( ActionEvent event )489 {490 setParcelData(); // update information491

492 // enable or make editable appropriate components493 setJButtons( true );494

495 // disable or make uneditable appropriate components496 updateJButton.setEnabled( false );497 parcelInformationJPanelEditable( false );498

The setParcelData method sets the Parcel’s properties to the values entered by the user

Page 91: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline91

ShippingHub.java(22 of 28)

499 // change selected item in parcelStateJComboBox500 parcelStateJComboBox.setSelectedIndex( 501 stateJComboBox.getSelectedIndex() ); 502 503 } // end method updateJButtonActionPerformed504

505 // move to previous Parcel506 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 Parcel518 loadParcel(); 519

520 } // end method backJButtonActionPerformed521

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

Page 92: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline92

ShippingHub.java(23 of 28)

522 // move to next Parcel523 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 list532 } 533 534 // load information of Parcel535 loadParcel(); 536 537 } // end method nextJButtonActionPerformed538

539 // change the list of Parcels in the parcelStateJList540 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

Page 93: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline93

ShippingHub.java(24 of 28)

547 // create iterator 548 Iterator parcelIterator = parcelsArrayList.iterator();549 550 // clear parcelStateArrayList551 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 while568 569 // display ArrayList in parcelStateJList570 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 ArrayListRetrieve the next object in

the parcelsArrayList

Add the Parcel ID to the parcelStateArrayList

Display the parcel ID numbers in the parcelStateArrayList

Page 94: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline94

ShippingHub.java(25 of 28)

574 575 // set all information about the Parcel576 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 setParcelData587 588 // display all information about the Parcel589 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

Page 95: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline95

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 loadParcel605 606 // clear all information about the Parcel607 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 clearComponents617

618 // enabled/disable JButtons619 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 );

Page 96: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline96

ShippingHub.java(27 of 28)

626

627 // disable navigation if not multiple packages628 if ( parcelsArrayList.size() < 2 )629 {630 nextJButton.setEnabled( false );631 backJButton.setEnabled( false );632 }633

634 // if no items, disable Remove, Edit and Update JButtons635 if ( parcelsArrayList.size() == 0 )636 {637 editJButton.setEnabled( false );638 updateJButton.setEnabled( false );639 removeJButton.setEnabled( false );640 }641

642 } // end method setJButtons643

Page 97: GUI Arraylist

2004 Prentice Hall, Inc.All rights reserved.

Outline97

ShippingHub.java(28 of 28)

644 // make editable or uneditable components645 // in parcelInformationJPanel646 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 parcelInformationJPanelEditable655 656 // main method657 public static void main( String[] args )658 {659 ShippingHub application = new ShippingHub();660 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );661

662 } // end method main663 664 } // end class ShippingHub


Top Related