// This code is based on the final version of the FileDisplay applet, // introduced in the lectures. // It adds exception handling (displaying a message in a dialog box // when an exception occurs). It also adds some code to ensure operations // scheduled by the start() method run in the AWT event-dispatching thread. import java.util.* ; import java.io.* ; import java.awt.event.* ; import javax.swing.* ; import java.rmi.* ; // While debugging, you will probably want to print messages from your applet. // You can try using something like this: /* JOptionPane.showMessageDialog(this, "my message", "Applet Debug Message", JOptionPane.WARNING_MESSAGE) ; */ // The first argument is a reference to a parent AWT component, // e.g. the JApplet or JPanel. class FilePanel extends JPanel { JList list ; public FilePanel() { list = new JList() ; // empty list JScrollPane scrollPane = new JScrollPane(list) ; add(scrollPane) ; } public void setLines(Vector lines) { list.setListData(lines) ; } } public class FileDisplay extends JApplet { FilePanel panel ; FileSource server ; public void init() { panel = new FilePanel() ; getContentPane().add(panel) ; try { server = (FileSource) Naming.lookup("rmi://sirah.csit.fsu.edu:" + getParameter("port") + "/fileservice") ; } catch(Exception e) { JOptionPane.showMessageDialog(this, "Error connecting to FileServer program: " + e.getMessage(), "FileDisplay Applet Error", JOptionPane.ERROR_MESSAGE) ; } } public void start() { // Use `invokeLater' and an anonymous class to ensure code // is executed in the AWT event-dispatching thread. // (In principle this is required.) // (If you also override the stop() method, it is advisable // to follow this approach there, as well.) SwingUtilities.invokeLater(new Runnable() { public void run() { // *** Start of code to run in event-dispatching thread *** String file = getParameter("file") ; try { Vector lines = server.readFile(file) ; panel.setLines(lines) ; repaint() ; } catch(Exception e) { JOptionPane.showMessageDialog(FileDisplay.this, "Failed to read file: " + e.getMessage(), "FileDisplay Applet Error", JOptionPane.ERROR_MESSAGE) ; } // *** End of code to run in event-dispatching thread. *** } }) ; } }