RMI, and Java GUIs
Comments zEveryone should be filling out and turning in group evaluations. These are largely for your protection. It can be used to point out where there are problems in group mechanics. zI had planned on talking about sequence diagrams today, but I’m going to push that back a week and discuss some Java topics instead.
Remote Method Invocation zJava makes virtually all network communication easier than it is in C/C++. This is particularly true when using RMI. zWith RMI you can treat objects on other computers as if they were local on your own computer and call methods on them accordingly. Of course, there is a lag when you do this, but you don’t have to explicitly worry about the socketing and whatnot.
Remote Interfaces zThe entity you get for an object remotely is actually not the object itself, but an interface that object implements. The interface must extend java.rmi.Remote. zThe implementation class should also extend java.rmi.server.UnicastRemoteObject. zNote that this implies that the implementation can have more functionality than the remote interface does. zAll remote methods can throw java.rmi.RemoteException.
Passing zDifferent types of objects are passed differently yRemote objects - any object that extends java.rmi.Remote is passed as a remote object. You get a skeleton that does network communication. ySerializable objects - If an object is not Remote it must be serializable and then it is passed by value. yPrimitives - No pass by reference of primitives
Registering and Lookup zOnce you have a remote object, it can pass you others, but getting the first one takes a different approach. zAn object can register itself with the rmi registry using the rebind method of java.rmi.Naming. (You have to start a local registry with rmiregistry first.) zObjects can get a remote reference to a registered object using the lookup method of java.rmi.Naming.
Compiling zOnce you have written your code you first compile it with a normal Java compiler. After that you have to do another step to create stub and skeleton classes that do most of the work behind RMI. You to this is the RMI compiler, rmic. Just run rmic specifying the name of the implementation class.
Java GUIs zOne of the greatest things about Java, from an application building standpoint, is how easy it is to make a GUI in it. zSince 5 out of 8 of the groups HAVE to do this to implement anything we will take a few minutes to discuss it. zJava has two graphics libraries, AWT and Swing. They work very similarly.
AWT zThe AWT library uses inheritance extensively. Components of the GUI are represented by the component class. Container is a subclass of Component that can have other components added to it. Each of these has other subclasses representing buttons, windows, text fields, labels, etc.
Layouts zTo make Java GUIs portable, they use layout managers to position and size various components. You can place things at specific locations on the screen, but that isn’t very portable. zEvery container has a layout manager it uses to place the components in it. By nesting containers and layout managers you can get great flexibility.
Some Layout Managers zHere are some of the layout managers in AWT. yFlowLayout - places one component after another like text on a page. yGridLayout - places components on a regular grid. yBorderLayout - Objects can be placed a north, south, east, west, or center. yGridBagLayout - A powerful layout manager for placing objects in an irregular grid.
Events zNo GUI is complete unless it can react to the user. User interactions with a GUI are called events and we want to be able to create code to “handle” events. zIn Java (since version 1.1) we do this be attaching event listeners to GUI components. When an event happens for an object all the appropriate listeners for that object are called.
Events and Listeners zThere are a fair number of event types and listeners. zThe listeners include ActionListener, MouseListener, KeyListener, and FocusListener. Each listener type has certain methods that it must define. zAction events are some of the most common and happen when you do things like click buttons.
Swing zSwing is actually based on AWT, but provides a look and feel that is machine independent. It has pretty much all the same objects but the names typically start is J (i.e. JButton and JFrame). zSwing also has other classes AWT didn’t. These include JTable, JTree, JColorChooser, and JToolBar. zAWT or Swing for project?