Đang chuẩn bị liên kết để tải về tài liệu:
Professional Java JDK 6 Edition 2007 phần 9
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
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 cancelMI.setEnabled false initially disable the cancel menu item since there is no active request cancelMI.setEnabled false menu.add new MenuItem - menu.add new MenuItem Configure Zipcode .addActionListener new ActionListener . retrieve user input for zipcode field menu.add new MenuItem Exit .addActionListener new ActionListener public void actionPerformed ActionEvent evt System.exit O this.icon new TrayIcon wImage Weather Watcher menu sysTray.add this.icon . The actionPerformed method of the WeatherWatcher class is where weather Web Services requests are started. Because WeatherGetter.getWeatherAsync 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 this.getWeatherMI.setEnabled false cancelMI.setEnabled true future weatherGetter.getWeatherAsync 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 resp.isCancelled resp.isDone if there was an exception during the web service processing it will be thrown here GetWeatherResponse gwr resp.get w gwr.getWeather else 592 Chapter 11 Communicating between Java Components and Other Platforms user cancelled the request re-enable menus and return try SwingUtilities.invokeAndWait new Runnable public void run cancelMI.setEnabled false getWeatherMI.setEnabled true catch Exception ex ex.printStackTrace return . All execution in handleResponse occurs on a different thread. This is extremely important it means whenever you want to update your GUI .