Blog / Build Code for the CourseListValueChanged() Method to Get Course Details – Develop Java Web Services to Access Databases

Build Code for the CourseListValueChanged() Method to Get Course Details – Develop Java Web Services to Access Databases

Build Code for the CourseListValueChanged() Method to Get Course Details – Develop Java Web Services to Access Databases


9.15.4   Build Code for the CourseListValueChanged() Method to Get Course Details

The function of this method is that when users click a course _ id from the Course ID List box, the detailed course information, including the course title, credit, classroom, schedule and enrollment for the selected course _ id, will be retrieved and displayed in six text fields in this CourseFrame Form window.

FIGURE 9.88   The run result of calling the Web operation QueryCourseID().

Perform the following operations to build the code for this method to perform that function:

1) Open our client project, WinClientCourse _ Select, if it has not been opened and open our main GUI, CourseFrame.java, by double-clicking on it.
2) Click on the Design button at the top of the window to open the GUI window, and right-click on our Course ID List Listbox and select Events > ListSelection > valueChanged item to open this method or event handler.

3) Enter the code shown in Figure 9.89 into this event handler.

Let’s have a closer look at this piece of new added code to see how it works.

A. The JTextField[] is a component defined in the javax.swing package; thus, it should be imported first to enable us to use that component to generate our field array later. The advantage of using a JTextField[] that contains all our six TextFields is to simplify our assignment coding process and make these assignments shorter in code.
B. An ArrayList instance, al, is created first, and it is used to collect the queried course details stored in an ArrayList object that will be returned from the execution of the Web service operation QueryCourse().

C. A JTextField array, cField[], is created and initialized with six text fields in the CourseFrame Form. The purpose of this array is to store queried course details and display them in these six TextFields.
D. Since the JList component belongs to the javax.swing package, not the java.awt package, clicking on an entry in the CourseList box causes the itemStateChanged() method to fire twice. The first time is when the mouse button is depressed, and the second time is when it is released. Therefore, the selected course _ id will appear twice when it is chosen. To prevent this from occurring, the getValueIsAdjusting() method is used to make sure that no item has been adjusted to be displayed twice. Then the selected course _ id is assigned to a local String variable, courseid, by calling the getSe-lectedValue() method of the CourseList Box class.

FIGURE 9.89   The completed code for the CourseListValueChanged() event handler.

Angela Gaffney

  • 0

Leave a Reply

Your email address will not be published. Required fields are marked *


                Copyright © 2024 Saucierlight.com. All Rights Reserved.