The AWT was modified in version 1.1 of the JDK to provide major quality improvements while introducing the beginnings of a richer infrastructure for larger-scale GUI development.
2. OVERVIEW
• AWT Enhancements
• Popup menus
• Scroll pane
• Clipboard
• The AppletContext object
• Audio and Images
• Inter-applet communication
3. DESKTOP COLORS
• The new SystemColor class helps Java detect the
color scheme set for the desktop
• Programmers can uses these colors by accessing the
final members of the class
• activeCaption : Caption background color
• contolText : Control’s text color
• desktop : Desktop’s background color
• menuText : Menu’s text color
• window : Window’s background color
4. POPUP MENUS
• Dynamically popped menus as specified position within
the component
• Defined the PopupMenu class of Java
• Invoke the show method to popup the menu
• void show(Component,int,int)
• If the popup menu object is used in place of a menu
item, the show() cannot be invoked
5. POPUP MENU - AN
EXAMPLE
public void mouseReleased(
MouseEvent evt) {
if (evt.isPopupTrigger()) {
popup.show(evt.getSource(),
evt.getX(), evt.getY());
}
else {
System.out.println("no popup");
}
}
7. SCROLLING CONTAINERS
• Container implementing automatic scrolling for a single
child component
• Needed in situations where the viewport is smaller than
the window
• Clipping the child’s contents appropriately depending
on the scroll position
8. THE SCROLLPANE CLASS
• Display policy can be set
• as needed
• always
• never
• Placement is controlled by platform-specifc properties
• Implements the Adjustable interface, which can
be used for fine tuning
9. SCROLLPANE - AN
EXAMPLE
ScrollPane sp = new ScrollPane();
sp.getHAdjustable().setUnitIncrement(2);
sp.getVAdjustable().setUnitIncrement(2);
sp.add(new ImageComponent(
getToolkit().getImage(filename)));
add(sp, BorderLayout.CENTER);
setSize(200, 150);
11. DATA TRANSFER
• Clipboard and Drag-drop are the two major models of
data transfer
• Data transfer occurs using “flavors” defined by the
Transfer APIs
• The flavors to be used for data transfer encapsulate the
various MIME types
• The java.awt.datatransfer package caters to the needs
of these operations
12. CLIPBOARD
• Clipboard is a globally shared memory
• The toolkit has an instance of a clipboard, that uses the
system clipboard
• Class willing to write to a clipboard need to implement
the ClipboardOwner interface
• Data flavors need to be compatible for transfer of data
using clipboard
13. WRITING TO A
CLIPBOARD
class ClipboardSource extends Frame
implements ClipboardOwner {
TextArea ta = new TextArea();
void setContents() {
String s = ta.getSelectedText();
StringSelection contents = new
StringSelection(s);
getToolkit().getSystemClipboard(
).setContents(contents, this);
}
}
15. READING FROM
CLIPBOARD
Transferable t = getToolkit().
getSystemClipboard().getContents(
this);
if(t.isDataFlavorSupported(
DataFlavor.stringFlavor)) {
String str = t.getTransferData(
DataFlavor.stringFlavor);
}
16. DRAG AND DROP
• Uses the same data transfer mechanism
• Discussed later as a part of JFC
17. APPLET CONTEXT
• Corresponds to the applet’s execution environment
• In the normal execution circumstances, this object is
the browser
• Defined by the AppletContext interface
• Use the getAppletContext() method to access
the context object
18. USING AUDIO
• Defined by the AudioClip interface
• A simple abstraction for playing audio clips
• Multiple clips can be played simltaneously
• Created using the getAudioClip() method of the
applet context
• Can play the audio clip using
• play()
• loop()
19. IMAGING
• Defined by the Image class
• BufferedImage can be used to achieve buffering
during image rendering
• Use getImage() of applet context to create a
Image object
• Use the drawImage() method in the Graphics class
to render images
20. ADVANCED FEATURES
• Defined in the java.awt.image package
• ImageProducer : a source of image data
• ImageConsumer : a user of image data
• ImageObserver : asynchronous notification on the image
• Defines color models to be used
• Defines some image filters
21. TRACKING THE MEDIA
• Useful for synchronous media preparation
• Defined in the MediaTracker class
• Applicable to both audio and images, but only images
supported currently
tracker = new MediaTracker(this);
for (int i = 0; i < 5; i++)
tracker.addImage(anim[i], 1);
for (int i = 0; i < 5; i++)
tracker.waitForID(i);
22. LOADING WEB PAGES
• Java applets can load other web pages
• Use the showDocument() method of the applet
context
• Can also be used to address framesets
• _self : same frame
• _parent : applet’s parent frame
• _top : top-level frame of applet
• _new : a new top-level window
• name : specified frame name
23. INTER-APPLET
COMMUNICATION
• Applets within the same applet context can
communicate with each other
• Obtain the object reference of the other applets using
• void getApplet(String);
• Enumeration getApplets();
• Communicate by invoking public methods
24. SCRIPTING APPLETS
• Help in communicating with applets from an external
entity
<SCRIPT LANGUAGE=VBScript>
Sub Update_onClick()
Marquee.setMessage(Message.Value)
End Sub
</SCRIPT>
<INPUT TYPE="Text" NAME="Message"
VALUE="">
<INPUT TYPE="Button" NAME="Update"
VALUE="Update">
25. REVIEW
• AWT offer capabilities like context menus, clipboard
support etc.
• Applet contexts help in
• rendering images and playing audio
• loading web pages
• inter-applet communication