Modify the Shipping Hub application created in this tutorial so that when the user double clicks a Parcel’s ID in parcelStateJList, that Parcel’s information will be displayed in a JOptionPane (Fig. 19.57). An empty method called parcelStateJListMouseDoubleClicked is provided for you. This method will exe- cute when an item in parcelStateJList is double clicked.You will add this exercise’s func- tionality to this method. At this point, you do not need to understand how to handle events related to the mouse—this will be covered in Tutorial 21.
a) Copying the template to your working directory. Copy the C:Examples Tutorial19ExercisesModifiedShippingHub 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 parcelStateJListMouseDouble- Clicked method, which begins at line 593. Inside the parcelStateJListMouseDou- bleClicked method, declare variable int number to hold the number of the Parcel the user has selected to display. To do this, you need to call the getSelectedValue method of class JList. This method returns the item currently selected in the JList as an Object. You will need to convert this Object to a String, then to an Integer, before finally storing the value in number.
d) Creating the Iterator. To cycle through the Parcels in parcelsArrayList, you need to create an Iterator. Create an Iterator (parcelIterator) to iterate through parcelsArrayList.
e) Creating the while statement. Add a whi
```
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 // JButtons to manipulate Parcels
51 private JButton scanNewJButton;
52 private JButton addJButton;
53 private JButton removeJButton;
54 private JButton editJButton;
55 private JButton updateJButton;
56 private JButton backJButton;
57 private JButton nextJButton;
58
59 // array contains options for stateJComboBox
60 private String[] states = { "AL", "FL", "GA", "KY", "MS", "NC",
61 "SC", "TN", "VA", "WV" };
62
63 // Parcel object contains data for newly entered Parcels
64 private Parcel newParcel;
65
66 // ArrayList contains Parcels entered by user
67 private ArrayList parcelsArrayList = new ArrayList();
68
69 // ArrayList used to modify and display the Parcel objects
70 // for a specific state
71 private ArrayList parcelStateArrayList = new ArrayList();
72
73 private int parcelID = 0; // ID for new Parcels
74
75 // position used to track location when the user is
76 // browsing through the list of Parcels
77 private int position = 0;
78
79 // no-argument constructor
80 public ShippingHub()
81 {
82 createUserInterface();
83 }
84
85 // create and position GUI components; register event handlers
86 private void createUserInterface()
87 {
88 // get content pane for attaching GUI components
89 Container contentPane = getContentPane();
90
91 // enable explicit positioning of GUI components
92 contentPane.setLayout( null );
93
94 // set up arrivedAtJLabel
95 arrivedAtJLabel = new JLabel();
96 arrivedAtJLabel.setBounds( 19, 14, 74, 24 );
97 arrivedAtJLabel.setText( "Arrived at:" );
98 contentPane.add( arrivedAtJLabel );
99
100 // set up arrivedAtJTextField
101 arrivedAtJTextField = new JTextField();
102 arrivedAtJTextField.setBounds( 89, 14, 197, 21 );
103 arrivedAtJTextField.setEditable( false );
104 contentPane.add( arrivedAtJTextField );
105
106 // set up parcelInformationJPanel
107 parcelInformationJPanel = new JPanel();
108 parcelInformationJPanel.setBounds( 9, 51, 490, 178 );
109 parcelInformationJPanel.setBorder(
110 new TitledBorder( "Parcel Information" ) );
111 parcelInformationJPanel.setLayout( null );
112 contentPane.add( parcelInformationJPanel );
113
114 // set up parcelIDJLabel
115 parcelIDJLabel = new JLabel();
116 parcelIDJLabel.setBounds( 15, 27, 84, 24 );
117 parcelIDJLabel.setText( "Parcel ID:" );
118 parcelInformationJPanel.add( parcelIDJLabel );
119
120 // set up parcelIDJTextField
121 parcelIDJTextField = new JTextField();
122 parcelIDJTextField.setBounds( 80, 27, 386, 21 );
123 parcelIDJTextField.setEditable( false );
124 parcelInformationJPanel.add( parcelIDJTextField );
125
126 // set up nameJLabel
127 nameJLabel = new JLabel();
128 nameJLabel.setBounds( 15, 65, 66, 25 );
129 nameJLabel.setText( "Name:" );
130 parcelInformationJPanel.add( nameJLabel );
131
132 // set up nameJTextField
133 nameJTextField = new JTextField();
134 nameJTextField.setBounds( 80, 65, 386, 21 );
135 nameJTextField.setEditable( false );
136 parcelInformationJPanel.add( nameJTextField );
137
138 // set up addressJLabel
139 addressJLabel = new JLabel();
140 addressJLabel.setBounds( 15, 103, 66, 25 );
141 addressJLabel.setText( "Address:" );
142 parcelInformationJPanel.add( addressJLabel );
143
144 // set up addressJTextField
145 addressJTextField = new JTextField();
146 addressJTextField.setBounds( 80, 103, 386, 21 );
147 addressJTextField.setEditable( false );
148 parcelInformationJPanel.add( addressJTextField );
149
150 // set up cityJLabel
151 cityJLabel = new JLabel();
152 cityJLabel.setBounds( 15, 141, 37, 24 );
153 cityJLabel.setText( "City:" );
154 parcelInformationJPanel.add( cityJLabel );
155
156 // set up cityJTextField
157 cityJTextField = new JTextField();
158 cityJTextField.setBounds( 80, 141, 117, 21 );
159 cityJTextField.setEditable( false );
160 parcelInformationJPanel.add( cityJTextField );
161
162 // set up stateJLabel
163 stateJLabel = new JLabel();
164 stateJLabel.setBounds( 215, 141, 47, 24 );
165 stateJLabel.setText( "State:" );
166 parcelInformationJPanel.add( stateJLabel );
167
168 // set up stateJComboBox
169 stateJComboBox = new JComboBox( states );
170 stateJComboBox.setBounds( 260, 141, 70, 21 );
171 stateJComboBox.setEnabled( false );
172 parcelInformationJPanel.add( stateJComboBox );
173
174 // set up zipJLabel
175 zipJLabel = new JLabel();
176 zipJLabel.setBounds( 355, 141, 28, 24 );
177 zipJLabel.setText( "Zip:" );
178 parcelInformationJPanel.add( zipJLabel );
179
180 // set up zipJTextField
181 zipJTextField = new JTextField();
182 zipJTextField.setBounds( 390, 141, 76, 21 );
183 zipJTextField.setEditable( false );
184 parcelInformationJPanel.add( zipJTextField );
185
186 // set up parcelStateJPanel
187 parcelStateJPanel = new JPanel();
188 parcelStateJPanel.setBounds( 508, 51, 136, 178 );
189 parcelStateJPanel.setBorder(
190 new TitledBorder( "Parcels by State" ) );
191 parcelStateJPanel.setLayout( null );
192 contentPane.add( parcelStateJPanel );
193
194 // set up parcelStateJComboBox
195 parcelStateJComboBox = new JComboBox( states );
196 parcelStateJComboBox.setBounds( 19, 29, 98, 21 );
197 parcelStateJPanel.add( parcelStateJComboBox );
198 parcelStateJComboBox.addActionListener(
199
200 new ActionListener() // anonymous inner class
201 {
202 // event handler called when parcelStateJComboBox
203 // is selected
204 public void actionPerformed( ActionEvent event )
205 {
206 parcelStateJComboBoxActionPerformed( event );
207 }
208
209 } // end anonymous inner class
210
211 ); // end call to addActionListener
212
213 // set up parcelStateJList
214 parcelStateJList = new JList();
215 parcelStateJList.addMouseListener(
216
217 new MouseAdapter() // anonymous inner class
218 {
219 // event handler called when mouse is double clicked
220 public void mouseClicked( MouseEvent event )
221 {
222 if ( event.getClickCount() == 2 )
223 {
224 parcelStateJListMouseDoubleClicked( event );
225 }
226
227 } // end method mouseClicked
228
229 } // end anonymous inner class
230
231 ); // end call to addMouseListener
232
233 // set up parcelStateJScrollPane
234 parcelStateJScrollPane = new JScrollPane( parcelStateJList );
235 parcelStateJScrollPane.setBounds( 19, 65, 98, 82 );
236 parcelStateJPanel.add( parcelStateJScrollPane );
237
238 // set up scanNewJButton
239 scanNewJButton = new JButton();
240 scanNewJButton.setBounds( 9, 248, 95, 26 );
241 scanNewJButton.setText( "Scan New" );
242 scanNewJButton.setMnemonic( KeyEvent.VK_S );
243 contentPane.add( scanNewJButton );
244 scanNewJButton.addActionListener(
245
246 new ActionListener() // anonymous inner class
247 {
248 // event handler called when scanNewJButton is pressed
249 public void actionPerformed( ActionEvent event )
250 {
251 scanNewJButtonActionPerformed( event );
252 }
253
254 } // end anonymous inner class
255
256 ); // end call to addActionLi
You might also like to view...
____________________ is the space between each letter.
Fill in the blank(s) with the appropriate word(s).
?Many organizations use a system of internal cross-charges to account for the cost of _____ assigned to a project.
Fill in the blank(s) with the appropriate word(s).
Match the following terms to their meanings:
I. SkyDrive II. Office 365 III. cloud integration technology IV. WordPress V. Send Invitations A. Microsoft Office products available online B. to share data online C. a button in Online Presentation D. virtual storage location E. a blogging service
Saving the closing lines of a letter is a poor application of creating a Quick Part for later use.
Answer the following statement true (T) or false (F)