Enhance the Inventory application that you developed in Tutorial 4 to prevent the user from entering input that is not a number. Use keyboard events to allow the user to press the number keys (0 to 9), the left and right arrow keys, the Backspace key and the Enter key. If any other key is pressed, display a JOption- Pane instructing the user to enter a number (Fig. 22.32).



a) Copying the template to your working directory. Copy the C:Examples Tutorial22ExercisesInventoryEnhanced directory to your C:SimplyJava directory.

b) Opening the template file. Open the Inventory.java file in your text editor.

c) Adding a switch statement. In line 132 (in the cartonsJTextFieldKeyPressed method), begin a switch statement that determines whether a number key, a left or right arrow key or the Backspace key was pressed. Use KeyEvent constants VK_0, VK_1, VK_2, VK_3, VK_4, VK_5, VK_6, VK_7, VK_8, VK_9, VK_LEFT, VK_RIGHT, VK_BACK_SPACE and VK_ENTER. If one of these keys was pressed, use the break state- ment to continue execution.

d) Adding the default statement. At the end of the switch statement, add a default case to determine whether a key other than a valid one for this application was pressed. If an invalid key was pressed, display a JOptionPane that instructs the user to enter a number. Also, reset the text of cartonsJTextField to "0".


```
1 // Inventory.java
2 // Calculates the number of items in a shipment based on the number
3 // of cartons received and the number of items per carton.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 public class Inventory extends JFrame
9 {
10 // JLabel and JTextField for cartons per shipment
11 private JLabel cartonsJLabel;
12 private JTextField cartonsJTextField;
13
14 // JLabel and JTextField for items per carton
15 private JLabel itemsJLabel;
16 private JTextField itemsJTextField;
17
18 // JLabel and JTextField for total items per shipment
19 private JLabel totalJLabel;
20 private JTextField totalJTextField;
21
22 // JButton to initiate calculation of total items per shipment
23 private JButton calculateJButton;
24
25 // no-argument constructor
26 public Inventory()
27 {
28 createUserInterface();
29 }
30
31 // create and position GUI components; register event handlers
32 private void createUserInterface()
33 {
34 // get content pane and set layout to null
35 Container contentPane = getContentPane();
36 contentPane.setLayout( null );
37
38 // set up cartonsJLabel
39 cartonsJLabel = new JLabel();
40 cartonsJLabel.setText( "Cartons per shipment:" );
41 cartonsJLabel.setBounds( 16, 16, 130, 21 );
42 contentPane.add( cartonsJLabel );
43
44 // set up cartonsJTextField
45 cartonsJTextField = new JTextField();
46 cartonsJTextField.setText( "0" );
47 cartonsJTextField.setBounds( 148, 16, 40, 21 );
48 cartonsJTextField.setHorizontalAlignment( JTextField.RIGHT );
49 contentPane.add( cartonsJTextField );
50 cartonsJTextField.addKeyListener(
51
52 new KeyAdapter() // anonymous inner class
53 {
54 // event handler called when cartonsJTextField is edited
55 public void keyPressed( KeyEvent event )
56 {
57 cartonsJTextFieldKeyPressed( event );
58 }
59
60 } // end anonymous inner class
61
62 ); // end call to addKeyListener
63
64 // set up itemsJLabel
65 itemsJLabel = new JLabel();
66 itemsJLabel.setText( "Items per carton:" );
67 itemsJLabel.setBounds( 16, 48, 104, 21 );
68 contentPane.add( itemsJLabel );
69
70 // set up itemsJTextField
71 itemsJTextField = new JTextField();
72 itemsJTextField.setText( "0" );
73 itemsJTextField.setBounds( 148, 48, 40, 21 );
74 itemsJTextField.setHorizontalAlignment( JTextField.RIGHT );
75 contentPane.add( itemsJTextField );
76 itemsJTextField.addKeyListener(
77
78 new KeyAdapter() // anonymous inner class
79 {
80 // event handler called when itemsJTextField is edited
81 public void keyPressed( KeyEvent event )
82 {
83 itemsJTextFieldKeyPressed( event );
84 }
85
86 } // end anonymous inner class
87
88 ); // end call to addKeyListener
89
90 // set up totalJLabel
91 totalJLabel = new JLabel();
92 totalJLabel.setText( "Total:" );
93 totalJLabel.setBounds( 204, 16, 40, 21 );
94 contentPane.add( totalJLabel );
95
96 // set up totalJTextField
97 totalJTextField = new JTextField();
98 totalJTextField.setBounds( 244, 16, 86, 21 );
99 totalJTextField.setHorizontalAlignment( JTextField.RIGHT );
100 totalJTextField.setEditable( false );
101 contentPane.add( totalJTextField );
102
103 // set up calculateJButton
104 calculateJButton = new JButton();
105 calculateJButton.setText( "Calculate Total" );
106 calculateJButton.setBounds( 204, 48, 126, 24 );
107 contentPane.add( calculateJButton );
108 calculateJButton.addActionListener(
109
110 new ActionListener() // anonymous inner class
111 {
112 // event handler called when calculateJButton is clicked
113 public void actionPerformed( ActionEvent event )
114 {
115 calculateJButtonActionPerformed( event );
116 }
117
118 } // end anonymous inner class
119
120 ); // end call to addActionListener
121
122 // set properties of application's window
123 setTitle( "Inventory" ); // set title bar string
124 setSize( 354, 112 ); // set window size
125 setVisible( true ); // display window
126
127 } // end method createUserInterface
128
129 // ensure only numbers are entered in cartonsJTextField
130 private void cartonsJTextFieldKeyPressed( KeyEvent event )
131 {
132 switch ( event.getKeyCode() )
133 {
166 case KeyEvent.VK_0:
167 case KeyEvent.VK_1:
168 case KeyEvent.VK_2:
169 case KeyEvent.VK_3:
170 case KeyEvent.VK_4:
171 case KeyEvent.VK_5:
172 case KeyEvent.VK_6:
173 case KeyEvent.VK_7:
174 case KeyEvent.VK_8:
175 case KeyEvent.VK_9:
176 case KeyEvent.VK_LEFT:
177 case KeyEvent.VK_RIGHT:
178 case KeyEvent.VK_BACK_SPACE:
179 case KeyEvent.VK_ENTER:
180 break;
181
182 default:
183 JOptionPane.showMessageDialog( this,
184 "Enter numbers only",
185 JOptionPane.WARNING_MESSAGE );
186 itemsJTextField.setText( "0" );
187 break;
188
189 } // end switch
190
191 } // end method itemsJTextFieldKeyPressed
192
193 // method
194 private void calculateJButtonActionPerformed( ActionEvent event )
195 {
196 // import data from JTextFields
197 int cartons = Integer.parseInt( cartonsJTextField.getText() );
198 int items = Integer.parseInt( itemsJTextField.getText() );
199 //
200 int calculate total = the total
201 items * cartons;
202
203 //
204 totalJTextField.setText( String.valueOf( total ) );
205
206 } // end method calculateJButtonActionPerformed
207
208 // main method
209 public static void main( String args[] )
210 {
211 Inventory application = new Inventory();
212 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
213
214 } // end method main
215
216 } // end class Inventory
```

Computer Science & Information Technology

You might also like to view...

In Java, a block is delimited by:

a. ( ) b. /* */ c. “ “ d. { }

Computer Science & Information Technology

When searching on Google, this is the same as using the AND keyword.

a. + b. OR c. blank space d. -

Computer Science & Information Technology

Match the following terms to their meanings:

I. Modal II. Macro III. Pop-up IV. Caption V. Dialog A. Thick border style property setting that can include a title bar with a Close button B. Text entered on this property displays on the title bar of the form when in Form view C. Until this window is closed, the focus cannot be changed to another database object D. Double-clicking the name of this causes it to execute E. Setting this property setting to "No" can display the Minimize and Maximize buttons

Computer Science & Information Technology

?The computational thinking tool of abstraction strives to eliminate all minor and insignificant details in order to focus on the fundamental aspects of the problem.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology