tailieunhanh - Professional Java JDK 6 Edition 2007 phần 9

Tham khảo tài liệu 'professional java jdk 6 edition 2007 phần 9', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Part II A Broad Understanding of Java APIs Tools and Techniques false initially disable the cancel menu item since there is no active request false new MenuItem - new MenuItem Configure Zipcode .addActionListener new ActionListener . retrieve user input for zipcode field new MenuItem Exit .addActionListener new ActionListener public void actionPerformed ActionEvent evt O new TrayIcon wImage Weather Watcher menu . The actionPerformed method of the WeatherWatcher class is where weather Web Services requests are started. Because is called the request is non-blocking and starts up in a background thread. When the request terminates either successfully or fails with an exception the anonymous AsyncHandler GetWeatherResponse s handleResponse method is called called when user clicks Get Weather context menu item initiates asynchronous web service call public void actionPerformed ActionEvent evt false true future zipcode new AsyncHandler GetWeatherResponse note that this reponse method is on another thread so we have to get back on the GUI thread to do any updates to our GUI components public void handleResponse Response GetWeatherResponse resp try final Weather w if if there was an exception during the web service processing it will be thrown here GetWeatherResponse gwr w else 592 Chapter 11 Communicating between Java Components and Other Platforms user cancelled the request re-enable menus and return try new Runnable public void run false true catch Exception ex return . All execution in handleResponse occurs on a different thread. This is extremely important it means whenever you want to update your GUI .