Create an application that allows a user to make a list of office supplies to buy as shown in Fig. 8.25. The user should enter the supply in a JText- Field, then click the Buy JButton to add it to the JTextArea. The Clear JButton removes all the items from the JTextArea.
```
a) Copying the template to your working directory. Copy the directory C:Examples Tutorial08ExercisesOfficeSupplies to your C:SimplyJava directory.
b) Opening the template file. Open the OfficeSupplies.java file in your text editor. c) Adding code to the event handler for the Buy JButton. Add a statement to the
buyJButtonActionPerformed event handler (which begins in line 105) that obtains
the user input from itemJTextField, then appends the user input to outputJText- Area. Add another statement that clears the itemJTextField.
d) Adding code to the event handler for the Clear JButton. Add a statement to the clearJButtonActionPerformed event handler (immediately after the buyJButton- ActionPerformed event handler) that uses JTextArea method setText to clear outputJTextArea.
e) Saving the application. Save your modified source code file.
f) Opening the Command Prompt window and changing directories. Open the Com- mand Prompt window by selecting Star
```
1 // OfficeSupplies.java
2 // Creates a list of office supplies to buy.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class OfficeSupplies extends JFrame
8 {
9 // JLabel and JTextField for inputting items
10 private JLabel itemJLabel;
11 private JTextField itemJTextField;
12
13 // JTextArea and JScrollPane for displaying items to purchase
14 private JTextArea outputJTextArea;
15 private JScrollPane outputJScrollPane;
16
17 // JButton to add item to list of items being purchased
18 private JButton buyJButton;
19
20 // JButton to clear list of items being purchased
21 private JButton clearJButton;
22
23 // no-argument constructor
24 public OfficeSupplies()
25 {
26 createUserInterface();
27 }
28
29 // create and position GUI components; register event handlers
30 private void createUserInterface()
31 {
32 // get content pane for attaching GUI components
33 Container contentPane = getContentPane();
34
35 // enable explicit positioning of GUI components
36 contentPane.setLayout( null );
37
38 // set up itemJLabel
39 itemJLabel = new JLabel();
40 itemJLabel.setBounds( 16, 24, 35, 21 );
41 itemJLabel.setText( "Item:" );
42 contentPane.add( itemJLabel );
43
44 // set up itemJTextField
45 itemJTextField = new JTextField();
46 itemJTextField.setBounds( 55, 24, 137, 21 );
47 itemJTextField.setHorizontalAlignment( JTextField.LEFT );
48 contentPane.add( itemJTextField );
49
50 // set up outputJTextArea
51 outputJTextArea = new JTextArea();
52 outputJTextArea.setEditable( false );
53
54 // set up outputJScrollPane
55 outputJScrollPane = new JScrollPane( outputJTextArea );
56 outputJScrollPane.setBounds( 16, 64, 176, 95 );
57 contentPane.add( outputJScrollPane );
58
59 // set up buyJButton
60 buyJButton = new JButton();
61 buyJButton.setBounds( 16, 176, 80, 24 );
62 buyJButton.setText( "Buy" );
63 contentPane.add( buyJButton );
64 buyJButton.addActionListener(
65
66 new ActionListener() // anonymous inner class
67 {
68 // event handler called when buyJButton is pressed
69 public void actionPerformed ( ActionEvent event )
70 {
71 buyJButtonActionPerformed( event );
72 }
73
74 } // end anonymous inner class
75
76 ); // end call to addActionListener
77
78 // set up clearJButton
79 clearJButton = new JButton();
80 clearJButton.setBounds( 112, 176, 80, 24 );
81 clearJButton.setText( "Clear" );
82 contentPane.add( clearJButton );
83 clearJButton.addActionListener(
84
85 new ActionListener() // anonymous inner class
86 {
87 // event handler called when clearJButton is pressed
88 public void actionPerformed ( ActionEvent event )
89 {
90 clearJButtonActionPerformed( event );
91 }
92
93 } // end anonymous inner class
94
95 ); // end call to addActionListener
96
97 // set properties of application’s window
98 setTitle( "Office Supplies" ); // set title bar text
99 setSize( 218, 245 ); // set window's size
100 setVisible( true ); // display window
101
102 } // end method createUserInterface
103
104 // called when user clicks buyJButton
105 public void buyJButtonActionPerformed( ActionEvent event )
106 {
107 // add user input to outputJTextArea
108 outputJTextArea.append( itemJTextField.getText() + "\n" );
109
110 itemJTextField.setText( "" ); // clear itemJTextField
111
112 } // end method buyJButtonActionPerformed
113
114 // called when user clicks clearJButton
115 public void clearJButtonActionPerformed( ActionEvent event )
116 {
117 outputJTextArea.setText( "" ); // clear outputJTextArea
118
119 } // end method clearJButtonActionPerformed
120
121 // main method
122 public static void main( String args[] )
123 {
124 OfficeSupplies application = new OfficeSupplies();
125 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
126
127 } // end method main
128
129 } // end class OfficeSupplies
```
You might also like to view...
A(n) ____________ is similar to an array, but it can expand at run time.
a. List object b. sequential object c. Random object d. jagged array
For a zip code field, the data type may be changed from Number to Text with no problems
Indicate whether the statement is true or false
The ________ bar displays at the bottom of the screen where you type the address of the website you want to visit or a search term
A) tabs B) address C) favorites D) status
Technorati assigns each blog an authority value to describe how influential it is in the ____.
A. Internet B. Web C. World D. blogosphere