Java provides methods to access your application’s components, as well as property values for those components. In this exercise, compo- nentsArrayList is provided and contains a reference to each component in the application. You will use a while statement to iterate through each component. As each component is encountered, add the component’s identifier (that is, the component’s name) to another ArrayList, called outputArrayList. Finally, you will display the component names in a Tutorial 19 Shipping Hub Application 445 JList and change the component’s background color to magenta (Fig. 19.58). [Note: We will explain how to change background colors
a) Copying the template to your working directory. Copy the C:Examples Tutorial19ExercisesComponentsCollection directory to your C:SimplyJava directory.
b) Opening the template file. Open the ComponentsCollection.java file in your text editor.
c) Clearing outputArrayList. ArrayList outputArrayList will be used to specify the output that will be displayed in List of components: JList. Find the sub- mitJButtonActionPerformed method, which begins at line 146. Inside the sub- mitJButtonActionPerformed method, add code to remove all previous elements that were stored in outputArrayList.
d) Creating the Iterator. After clearing the outputArrayList, declare an Iterator (componentIterator) for componentsArrayList. ArrayList componentsArrayL- ist already contains references to each component in the application. These refer- ences are of type Component, which can be used to access information about any component, such as the name of the component.
e) Creating the whil
```
1 // Exercise 19.13 ComponentsCollection.java
2 // This application lists all components used in the application.
3 import java.awt.event.*;
4 import java.awt.*;
5 import java.util.*;
6 import javax.swing.*;
7
8 public class ComponentsCollection extends JFrame
9 {
10 // JLabel and JTextField for reading name from user
11 private JLabel nameJLabel;
12 private JTextField nameJTextField;
13
14 // JLabel and JComboBox for displaying series name
15 private JLabel bookJLabel;
16 private JComboBox bookJComboBox;
17
18 // two JLabels for displaying book cover
19 private JLabel pictureJLabel;
20 private JLabel bookPictureJLabel;
21
22 // JLabel and JList for displaying list of components
23 private JLabel listComponentsJLabel;
24 private JList listComponentsJList;
25
26 // JButton to allow user interaction
27 private JButton submitJButton;
28
29 // instance variables
30 private String[] bookList = { "Simply Series",
31 "How To Program Series", "Developer Series" };
32 private ArrayList componentsArrayList = new ArrayList();
33 private Component[] components = new Component[ 9 ];
34 private ArrayList outputArrayList = new ArrayList();
35
36 // no-argument constructor
37 public ComponentsCollection()
38 {
39 createUserInterface();
40 }
41
42 // create and position GUI components; register event handlers
43 private void createUserInterface()
44 {
45 // get content pane for attaching GUI components
46 Container contentPane = getContentPane();
47
48 // enable explicit positioning of GUI components
49 contentPane.setLayout( null );
50
51 // set up nameJLabel
52 nameJLabel = new JLabel();
53 nameJLabel.setBounds( 16, 15, 48, 21 );
54 nameJLabel.setText( "Name:" );
55 nameJLabel.setName( "nameJLabel" );
56 nameJLabel.setOpaque( true );
57 contentPane.add( nameJLabel );
58
59 // set up nameJTextField
60 nameJTextField = new JTextField();
61 nameJTextField.setBounds( 64, 16, 172, 20 );
62 nameJTextField.setName( "nameJTextField" );
63 contentPane.add( nameJTextField );
64
65 // set up bookJLabel
66 bookJLabel = new JLabel();
67 bookJLabel.setBounds( 16, 48, 48, 21 );
68 bookJLabel.setText( "Book:" );
69 bookJLabel.setName( "bookJLabel" );
70 bookJLabel.setOpaque( true );
71 contentPane.add( bookJLabel );
72
73 // set up bookJComboBox
74 bookJComboBox = new JComboBox( bookList );
75 bookJComboBox.setBounds( 64, 48, 172, 21 );
76 bookJComboBox.setName( "bookJComboBox" );
77 contentPane.add( bookJComboBox );
78
79 // set up pictureJLabel
80 pictureJLabel = new JLabel();
81 pictureJLabel.setBounds( 16, 80, 48, 21 );
82 pictureJLabel.setText( "Picture:" );
83 pictureJLabel.setName( "pictureJLabel" );
84 pictureJLabel.setOpaque( true );
85 contentPane.add( pictureJLabel );
86
87 // set up bookPictureJLabel
88 bookPictureJLabel = new JLabel();
89 bookPictureJLabel.setBounds( 64, 80, 91, 119 );
90 bookPictureJLabel.setIcon( new ImageIcon(
91 "vbnetFEP1_thumb.jpg" ) );
92 bookPictureJLabel.setName( "bookPictureJLabel" );
93 contentPane.add( bookPictureJLabel );
94
95 // set up listComponentsJLabel
96 listComponentsJLabel = new JLabel();
97 listComponentsJLabel.setBounds( 252, 16, 112, 21 );
98 listComponentsJLabel.setText( "List of components:" );
99 listComponentsJLabel.setName( "listComponentsJLabel" );
100 listComponentsJLabel.setOpaque( true );
101 contentPane.add( listComponentsJLabel );
102
103 // set up listComponentsJList
104 listComponentsJList = new JList();
105 listComponentsJList.setBounds( 252, 40, 130, 168 );
106 listComponentsJList.setName( "listComponentsJList" );
107 contentPane.add( listComponentsJList );
108
109 // set up submitJButton and register its event handler
110 submitJButton = new JButton();
111 submitJButton.setBounds( 296, 224, 76, 23 );
112 submitJButton.setText( "Submit" );
113 submitJButton.setName( "submitJButton" );
114 contentPane.add( submitJButton );
115 submitJButton.addActionListener(
116
117 new ActionListener() // anonymous inner class
118 {
119 // event handler called when submitJButton is pressed
120 public void actionPerformed( ActionEvent event )
121 {
122 submitJButtonActionPerformed( event );
123 }
124
125 } // end anonymous inner class
126
127 ); // end addActionListener
128
129 // put all components in an array
130 components = getContentPane().getComponents();
131
132 // add all components to componentsArrayList
133 for ( int i = 0; i < 9; i++ )
134 {
135 componentsArrayList.add( components[ i ] );
136 }
137
138 // set properties of application's window
139 setTitle( "Components Collection" ); // set title bar string
140 setSize( 406, 288 ); // set window size
141 setVisible( true ); // display window
142
143 } // end method createUserInterface
144
145 // method to display a list of components
146 private void submitJButtonActionPerformed( ActionEvent event )
147 {
148 // clear ArrayList
149 outputArrayList.clear()
150 ;
151
152 // set iterator
153 Iterator componentIterator = componentsArrayList.iterator();
154
155 // for every component in application
156 while ( componentIterator.hasNext() )
157 {
158 // set reference variable
159 Component currentComponent = ( Component )
160 componentIterator.next();
161
162 // set background color to magenta
163 currentComponent.setBackground( Color.MAGENTA );
164
165 // and add name to componentsArrayList
166 outputArrayList.add( currentComponent.getName() );
167
168 } // end while loop
169
170 // display componentsArrayList in JList
171 listComponentsJList.setListData( outputArrayList.toArray() );
172 } // end submitJButtonActionPerformed
173
174 // main method
175 public static void main( String args[] )
176 {
177 ComponentsCollection application = new ComponentsCollection();
178 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
179
180 } // end method main
181
182 } // end class ComponentsCollection
```
You might also like to view...
What is 70 percent of 90?
What will be an ideal response?
A movable, resizable container where text or graphics can be placed is called a
A) text box. B) chart area. C) plot area. D) bevel.
Explain the purpose of aggregation.
What will be an ideal response?
All activity in OneNote takes place in the notebook.
Answer the following statement true (T) or false (F)