Enhance the Shipping Hub application to allow the user to move a maximum of five parcels from the ware- house to a truck for shipping. When the user selects an item in the parcelStateJList and clicks the Ship JButton, the item will be removed from this JList and added to the Parcels to Ship JList (parcelShipJList, shown in Fig. 19.59). After five Parcel IDs have been added to this JList, no more may be added. The five Parcel limit has already been coded for you. You will only be adding the functionality to modify the JLists.
a) Copying the template to your working directory. Copy the C:Examples Tutorial19ExercisesEnhancedShippingHub directory to your C:SimplyJava directory.
b) Opening the template file. Open the ShippingHub.java file in your text editor.
c) Retrieving the selected Parcel’s ID. Find the shipJButtonActionPerformed method, which begins at 440. The first statement inside the shipJButtonActionPer- formed is an if statement. This if statement will execute when the user has clicked shipJButton and there are still less than five items in toBeShippedArrayList. This ArrayList will be used to contain the Parcel IDs that will be added to the Parcels to Ship JList. At line 445, declare variable int currentNumber to hold the ID of the Parcel the user has selected in parcelStateJList. Remember to use the get-
SelectedValue method and convert the return value to the proper type.
d) Adding the selected ID to toBeShippedArrayList. Add the String version of the ID selected to toBeShippedArrayL
```
1 // 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
26 // JLabel and JTextField for address
27 private JLabel addressJLabel;
28 private JTextField addressJTextField;
29
30 // JLabel and JTextField for city
31 private JLabel cityJLabel;
32 private JTextField cityJTextField;
33
34 // JLabel and JTextField for state
35 private JLabel stateJLabel;
36 private JComboBox stateJComboBox;
37
38 // JLabel and JTextField for zip code
39 private JLabel zipJLabel;
40 private JTextField zipJTextField;
41
42 // JPanel for Parcel number by state
43 private JPanel parcelStateJPanel;
44
45 // JComboBox, JList and JScrollPane for Parcel number
46 private JComboBox parcelStateJComboBox;
47 private JList parcelStateJList;
48 private JScrollPane parcelStateJScrollPane;
49
50 // JButton to ship Parcel
51 private JButton shipJButton;
52
53 // JPanel for Parcels to ship
54 private JPanel parcelShipJPanel;
55
56 // JList and JScrollPane for Parcels to ship
57 private JList parcelShipJList;
58 private JScrollPane parcelShipJScrollPane;
59
60 // JButtons to manipulate Parcels
61 private JButton scanNewJButton;
62 private JButton addJButton;
63 private JButton removeJButton;
64 private JButton editJButton;
65 private JButton updateJButton;
66 private JButton backJButton;
67 private JButton nextJButton;
68
69 // array contains options for stateJComboBox
70 private String[] states = { "AL", "FL", "GA", "KY", "MS", "NC",
71 "SC", "TN", "VA", "WV" };
72
73 // Parcel object contains data for newly entered Parcels
74 private Parcel newParcel;
75
76 // ArrayList contains Parcels entered by user
77 private ArrayList parcelsArrayList = new ArrayList();
78
79 // ArrayList used to modify and display the Parcel objects
80 // for a specific state
81 private ArrayList parcelStateArrayList = new ArrayList();
82
83 // ArrayList contains Parcels being sent to truck
84 private ArrayList toBeShippedArrayList = new ArrayList();
85
86 private int parcelID = 0; // ID for new Parcels
87
88 // position used to track location when the user is
89 // browsing through the list of Parcels
90 private int position = 0;
91
92 // no-argument constructor
93 public ShippingHub()
94 {
95 createUserInterface();
96 }
97
98 // create and position GUI components; register event handlers
99 private void createUserInterface()
100 {
101 // get content pane for attaching GUI components
102 Container contentPane = getContentPane();
103
104 // enable explicit positioning of GUI components
105 contentPane.setLayout( null );
106
107 // set up arrivedAtJLabel
108 arrivedAtJLabel = new JLabel();
109 arrivedAtJLabel.setBounds( 19, 14, 74, 24 );
110 arrivedAtJLabel.setText( "Arrived at:" );
111 contentPane.add( arrivedAtJLabel );
112
113 // set up arrivedAtJTextField
114 arrivedAtJTextField = new JTextField();
115 arrivedAtJTextField.setBounds( 89, 14, 197, 21 );
116 arrivedAtJTextField.setEditable( false );
117 contentPane.add( arrivedAtJTextField );
118
119 // set up parcelInformationJPanel
120 parcelInformationJPanel = new JPanel();
121 parcelInformationJPanel.setBounds( 9, 51, 490, 178 );
122 parcelInformationJPanel.setBorder(
123 new TitledBorder( "Parcel Information" ) );
124 parcelInformationJPanel.setLayout( null );
125 contentPane.add( parcelInformationJPanel );
126
127 // set up parcelIDJLabel
128 parcelIDJLabel = new JLabel();
129 parcelIDJLabel.setBounds( 15, 27, 84, 24 );
130 parcelIDJLabel.setText( "Parcel ID:" );
131 parcelInformationJPanel.add( parcelIDJLabel );
132
133 // set up parcelIDJTextField
134 parcelIDJTextField = new JTextField();
135 parcelIDJTextField.setBounds( 80, 27, 386, 21 );
136 parcelIDJTextField.setEditable( false );
137 parcelInformationJPanel.add( parcelIDJTextField );
138
139 // set up nameJLabel
140 nameJLabel = new JLabel();
141 nameJLabel.setBounds( 15, 65, 66, 25 );
142 nameJLabel.setText( "Name:" );
143 parcelInformationJPanel.add( nameJLabel );
144
145 // set up nameJTextField
146 nameJTextField = new JTextField();
147 nameJTextField.setBounds( 80, 65, 386, 21 );
148 nameJTextField.setEditable( false );
149 parcelInformationJPanel.add( nameJTextField );
150
151 // set up addressJLabel
152 addressJLabel = new JLabel();
153 addressJLabel.setBounds( 15, 103, 66, 25 );
154 addressJLabel.setText( "Address:" );
155 parcelInformationJPanel.add( addressJLabel );
156
157 // set up addressJTextField
158 addressJTextField = new JTextField();
159 addressJTextField.setBounds( 80, 103, 386, 21 );
160 addressJTextField.setEditable( false );
161 parcelInformationJPanel.add( addressJTextField );
162
163 // set up cityJLabel
164 cityJLabel = new JLabel();
165 cityJLabel.setBounds( 15, 141, 37, 24 );
166 cityJLabel.setText( "City:" );
167 parcelInformationJPanel.add( cityJLabel );
168
169 // set up cityJTextField
170 cityJTextField = new JTextField();
171 cityJTextField.setBounds( 80, 141, 117, 21 );
172 cityJTextField.setEditable( false );
173 parcelInformationJPanel.add( cityJTextField );
174
175 // set up stateJLabel
176 stateJLabel = new JLabel();
177 stateJLabel.setBounds( 215, 141, 47, 24 );
178 stateJLabel.setText( "State:" );
179 parcelInformationJPanel.add( stateJLabel );
180
181 // set up stateJComboBox
182 stateJComboBox = new JComboBox( states );
183 stateJComboBox.setBounds( 260, 141, 70, 21 );
184 stateJComboBox.setEnabled( false );
185 parcelInformationJPanel.add( stateJComboBox );
186
187 // set up zipJLabel
188 zipJLabel = new JLabel();
189 zipJLabel.setBounds( 355, 141, 28, 24 );
190 zipJLabel.setText( "Zip:" );
191 parcelInformationJPanel.add( zipJLabel );
192
193 // set up zipJTextField
194 zipJTextField = new JTextField();
195 zipJTextField.setBounds( 390, 141, 76, 21 );
196 zipJTextField.setEditable( false );
197 parcelInformationJPanel.add( zipJTextField );
198
199 // set up parcelStateJPanel
200 parcelStateJPanel = new JPanel();
201 parcelStateJPanel.setBounds( 508, 51, 136, 178 );
202 parcelStateJPanel.setBorder(
203 new TitledBorder( "Parcels by State" ) );
204 parcelStateJPanel.setLayout( null );
205 contentPane.add( parcelStateJPanel );
206
207 // set up parcelStateJComboBox
208 parcelStateJComboBox = new JComboBox( states );
209 parcelStateJComboBox.setBounds( 19, 24, 98, 21 );
210 parcelStateJPanel.add( parcelStateJComboBox );
211 parcelStateJComboBox.addActionListener(
212
213 new ActionListener() // anonymous inner class
214 {
215 // event handler called when parcelStateJComboBox
216 // is selected
217 public void actionPerformed( ActionEvent event )
218 {
219 parcelStateJComboBoxActionPerformed( event );
220 }
221
222 } // end anonymous inner class
223
224 ); // end call to addActionListener
225
226 // set up parcelStateJList
227 parcelStateJList = new JList();
228 parcelStateJList.addMouseListener(
229
230 new MouseAdapter() // anonymous inner class
231 {
232 // event handler called when mouse is double clicked
233 public void mouseClicked( MouseEvent event )
234 {
235 if ( event.getClickCount() == 2 )
236 {
237 parcelStateJListMouseDoubleClicked( event );
238 }
239
240 } // end method mouseClicked
241
242 } // end anonymous inner class
243
244 ); // end call to addMouseListener
245
246 // set up parcelStateJScrollPane
247 parcelStateJScrollPane = new JScrollPane( parcelStateJList );
248 parcelStateJScrollPane.setBounds( 19, 55, 98, 77 );
249 parcelStateJPanel.add( parcelStateJScrollPane );
250
251 // set up shipJButton
252 shipJButton = new JButton();
253 shipJButton.setBounds( 19, 142, 98, 26 );
254 shipJButton.setText( "Ship" );
255 parcelStateJPanel.add( shipJButton );
256 shipJButton.addActionListener(
257
258 new ActionListener() // anonymous inner class
259 {
260 // eve