Java Applets / Flash
Java Applet vs. Flash political problems with Microsoft highly portable more difficult development not a problem less so excellent visual development tool
Applet / Flash good for: situations involving complex user input where XHTML input elements are not adequate animated output
Applet / Flash Pitfalls some users disable plug-ins/applets not available on all platforms input/output can not be handled in a standard way (assistive technology for people with disabilities)
Applet Security applets cannot: read/write files on the client file system open network connections (other than to the machine that the applet was loaded from) launch applications on the client system read certain properties of the VM
Embedding an Applet in a Page <object classid="java:path to class file" width="width in pixels" height="height in pixels"> <param name="name1" value="value1" /> <param name="name2" value="value2" />... Your browser doesn't support the object tag. </object>
<object classid="java:applet1.class" width="200" height="100"> <param name="message" value="hello!" /> Your browser doesn't support the object tag. </object>
import java.applet.applet; import java.awt.*; import java.awt.event.*; public class applet1 extends Applet { public void paint(graphics g) { g.drawstring( System.getProperty( "java.version" ), 20, 20 ); g.drawstring( System.getProperty( "java.vendor" ), 20, 40 ); } } g.drawstring( getparameter( "message" ), 20, 60 );
Applet Control Methods invoked by the containing browser init() - called once after the applet object is constructed start() - called each time applet is activated (becomes visible, etc.) stop() - called each time applet is deactivated (becomes invisible, etc.) destroy() - called once before applet is garbage collected
Controlling the Browser the Applet class has a getappletcontext method which returns an object allowing the applet to control its environment the AppletContext class has three methods for controlling the browser: showdocument( URL url ) showdocument( URL url, String target ) showstatus( String status )
import java.applet.applet; import java.awt.*; import java.awt.event.*; public class applet1 extends Applet {... URL newurl( http://www.abc.com ); AppletContext ac = getappletcontext(); } ac.showstatus( Clever message ); ac.showdocument( newurl, _blank ); ac.showdocument( newurl );...
AWT Abstract Window Toolkit platform independent GUI toolkit written in Java event driven
Component class base class for all visible elements paint() method overridden by child classes to give component specific rendering repaint() method used to trigger the painting of the component add***listner() method to control the distribution of events
Listeners low level addkeylistener addmouselistener addmousemostionlistener high level addactionlistener
JavaBeans: Indexed Properties for properties that can have multiple values, use the following pattern: public String[] getfoo(); public void setfoo( String[] value ); public String getfoo( int index ); public void setfoo( int index, String value );
JavaBeans: Indexed Properties there are no JSP tags for accessing individual elements of an indexed property use JSP expressions instead: <jsp:usebean id= somebean class= FooBean scope= page />... <%= somebean.getfoo( 7 ) %>
AWT: Simple Components Component Button Label TextComponent Checkbox Canvas TextField TextArea
Buttons simple push button Button b = new Button( Push Me! ); b.addactionlistener( myapplet ); b.setactioncommand( push );
public class applet1 extends Applet implements ActionListener {... void actionperformed( ActionEvent e ) { switch ( e.getactioncommand() ) { case push :... case pull :... }... }
Checkbox Checkbox cb = new Checkbox( Fries with that? ); cb.additemlistener( myapplet );
public class applet1 extends Applet implements ItemListener {... void itemstatechanged( ItemEvent e ) { switch ( e.getstatechange() ) { case SELECTED:... case DESELECTED:... }... }
AWT: Containers Component Container Panel Window Applet Frame
AWT: Containers Panel subwindow Window top level window; free to be moved, resized, etc adding compenents to a container: myapplet.add( b ); myapplet.add( cb );...
AWT: Containers: Layouts used to control how childen are laid out inside a container choices: FlowLayout (default), BorderLayout, GridLayout, GridBagLayout setting the layout: myapplet.setlayout( new BorderLayout() ); myapplet.setlayout( new GridLayout( rows, cols ) );
BorderLayout only one component per position when adding components, must also specify position: BorderLayout.NORTH BorderLayout.SOUTH BorderLayout.EAST BorderLayout.WEST BorderLayout.CENTER myapplet.add( b, BorderLayout.EAST );
AWT: Menus MenuComponent MenuItem MenuBar Menu
Menus: Example Menu m = new Menu( File ); MenuItem i1 = new MenuItem( New ); i1.addactionlistener( myapplet ); i1.setactioncommand( new ); MenuItem i2 = new MenuItem( Open ); MenuItem i3 = new MenuItem( Save ); MenuItem i1 = new MenuItem( Save As... ); m.add( i1 ); m.add( i2 ); m.add( i3 ); m.add( i4 );
Cascading menus Menu m2 = new Menu( foo ); MenuItem i10 = new MenuItem( xyz ); MenuItem i11 = new MenuItem( abc ); m2.add( i10 ); m2.add( i11 ); m2.add( m ); // menu from previous slide