Your SlideShare is downloading. ×
0
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
JAVA GUI
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

JAVA GUI

670

Published on

Published in: Technology, Education
1 Comment
0 Likes
Statistics
Notes
  • just not really good at object oriented java but would love so much to learn it....any help rendered will be appreciated
    Thanks.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total Views
670
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
48
Comments
1
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Ranjana vyas
  • Transcript

    • 1. OOM for SE ByProf. Dr. O. P. Vyas M.Tech.(CS), Ph.D. ( I.I.T. Kharagpur ) DAAD Fellow ( Germany ) AOTS Fellow ( Japan)
    • 2. Java : GUI & Applets s Java GUI 5Creating Window: using Frame s Using AWT & Swing s Event Handling in Java s Delegation model 5Event Source , Listener & Interface s Applets & Standalone applications s Writing Applet s Embedding in HTML s Life Cycle of Applet 04/26/12 2Database System Concepts 3.2 ©Silberschatz, Korth and Sudarshan
    • 3. OOM & GUI Typical GUI providing user friendly environment comprises of ;  Buttons,  Menus  Combo boxes &  Bars: Title Bar, Menu Bar & Scroll Bar. Although there are several techniques for creating GUI but let us explore GUI creations by Java .  In Java Graphical User Interface (GUI) 04/26/12 programming, we do not build GUI 3 components from scratch. Instead we©Silberschatz, Korth and SudarshanDatabase System Concepts 3.3 use
    • 4. GUI with Java  GUI : AWT & Swing 5 Creating Window s Components & Containers s Frame Classes s Layout Managers s Event Handling & Applets s Writing Applets s Sandbox Security 04/26/12 4Database System Concepts 3.4 ©Silberschatz, Korth and Sudarshan
    • 5. GUI with Java  GUI : AWT & Swing 5 Creating Window: GUI Coordinate systems 5 Import Classes 5 OOP characteristics on AWT & Swing Instantiate Frame Object s Components & Containers s Frame Classes s Layout Managers s Event Handling & Applets s Sandbox Security 04/26/12 5Database System Concepts 3.5 ©Silberschatz, Korth and Sudarshan
    • 6. User Interface s Sun had used a logical and object-oriented design for creating a GUI and handling its events. s There are two APIs for creating GUI applications in Java: Swing and AWT (Abstract Windowing Toolkit). s When Java was first released in 1995, it contained a GUI API : AWT. s AWT API contained classes like 5 Frame to represent a typical window, 5Button to represent buttons, 5 Menu to represent a window’s menu, and so on. 6Database System Concepts 3.6 ©Silberschatz, Korth and Sudarshan
    • 7. GUI: AWT & Swing AWT components are referred to as heavyweight components because their implementation relies heavily on the underlying operating system. s The look and feel of AWT components depend on the platform the program is running on. For example, 5 an AWT button will look like a Windows button when the program is run on a Windows platform. 5 The same button will look like a Macintosh button when the program is run on a Macintosh platform. s Swing is different from AWT in that Swing components are 100 percent Java, thereby not relying on the native operating system or platform. 7Database System Concepts 3.7 ©Silberschatz, Korth and Sudarshan
    • 8. AWT & Swing s Nowadays, most Java GUI programming is done by using Swing. s We will discuss the AWT , though, because it is an important part of GUI programming, and many of the AWT classes are used in Swing, including the layout managers, and event-handling classes and interfaces. s The names of the Swing classes all begin with a capital J, like JButton. s For the most part, an AWT program can be converted to a Swing program by adding a capital J to the class names used in the source code and recompiling the code. 04/26/12 8Database System Concepts 3.8 ©Silberschatz, Korth and Sudarshan
    • 9. Containers and Components s There are two basic elements of a GUI: containers and components. 5A container is for displaying components, and components must be displayed within a container. 5A Button is an example of a component, component whereas a Frame is an example of a container. 5To display a Button, you place it within a Frame and display the Frame. 04/26/12 9Database System Concepts 3.9 ©Silberschatz, Korth and Sudarshan
    • 10. Swing Components for GUI The Class Hierarchy depicts that from where Swing components inherit their common attributes and behavior. Class Component ( package.java.awt) is subclass of Object that declares many of the attributes and behaviors common to GUI Component Object Component Container JComponent The Swing API uses many of the AWT classes and interfaces. 10Database System Concepts 3.10 ©Silberschatz, Korth and Sudarshan
    • 11. Creating Window: using Frame s Every GUI app uses what is called a Frame that comes with the JDK. s A Frame is a window with borders, a title bar, and buttons for closing and maximizing or minimizing itself. s It also knows how to resize itself. s Every GUI app subclasses Frame in some way to add more functionalities to the window frame. s One common task is to tell the system to terminate all "threads" of the GUI app when the user clicks on the exit (close) button of the main GUI window. 04/26/12 11Database System Concepts 3.11 ©Silberschatz, Korth and Sudarshan
    • 12. Creating Window s The basic starting point of a GUI is the container because you need a container before you can start laying out your components. s The java.awt.Frame and javax.swing.JFrame classes are containers that represent a basic window with a title bar and common windowing capabilities such as resizing, minimizing, maximizing, and closing. s The Frame class is used for AWT programs and is the parent class of JFrame, which is used for Swing programs. 04/26/12 12Database System Concepts 3.12 ©Silberschatz, Korth and Sudarshan
    • 13. GUI Coordinates s All components and containers have a size and location, which is denoted in pixels. s A pixel is a relative unit of measurement based on the settings of the user’s screen. s The pixels create a coordinate system, with the upper-left corner of the screen as the origin (0,0). s Any point on the screen can be represented as an (x,y) value, where x is the number of pixels to the right of the origin, and y is the number of pixels down from the origin. 04/26/12 13Database System Concepts 3.13 ©Silberschatz, Korth and Sudarshan
    • 14. Using Frame s For example, the point (100,100) is 100 pixels over and 100 pixels down from the upper-left corner of the screen. s Suppose that a Frame is instantiated and given the bounds (100,100, 300, 400):  Frame f = new Frame();  f.setBounds(100, 100, 300, 400); s The upper-left corner of the Frame is the point (100,100) relative to the computer screen. s The width of this Frame is 300 and the height is 400, so the lower-right corner of the Frame is the point (400, 500) of the computer screen 04/26/12 14Database System Concepts 3.14 ©Silberschatz, Korth and Sudarshan
    • 15. GUI Coordinate system s There is another coordinate system of GUI components referred to as the relative coordinate system. s The relative coordinate system is based on the upper- left corner of the container that the component is residing in. s The upper-left corner of a container is an origin (0,0), and components are placed in a container relative to the container’s origin, not the screen’s origin. s For example, the following statements instantiate a Button, assign it bounds (20, 200, 60, 40). The Button is then added to the Frame object instantiated earlier: s Button ok = new Button(“OK”); s ok.setBounds(20, 200, 60, 40); s f.add(ok); //Add the Button to a Frame f.add(ok 04/26/12 15Database System Concepts 3.15 ©Silberschatz, Korth and Sudarshan
    • 16. Coordinates The upper-left corner of the OK button appears 20 pixels over and 200 pixels down from the upper-left corner of the Frame. The size of the Button is 60 pixels wide and 40 pixels high. s Assuming that Frame f has not been moved, this puts the Button 120 pixels over and 300 pixels down from the upper-left corner of the screen. This point changes if the Frame is moved. However, the relative location of the Button within the Frame does not move, even if the Frame moves. This is the desired result of GUI containers and components. s When we move a window, we expect all the components within the window to move along with it. Therefore, we rarely concern ourselves with the actual screen coordinates of a component. s The component’s relative coordinates are what are important to a programmer laying out components in a container. 04/26/12 16Database System Concepts 3.16 ©Silberschatz, Korth and Sudarshan
    • 17. java.awt.Frame Class s When working with Frame objects, there are basically three steps involved to get a Frame window to appear on the screen: 1. Instantiate the Frame object in memory. 2. Give the Frame object a size using setSize(), setBounds(), or pack(). 3. Make the Frame appear on the screen by invoking setVisible(true). 04/26/12 17Database System Concepts 3.17 ©Silberschatz, Korth and Sudarshan
    • 18. Instantiate the Frame Object s The java.awt.Frame class has four constructors: n public Frame(). Creates a new frame with no message in the title bar. n public Frame(String title). Creates a new frame with the given String appearing in the title bar. n public Frame(GraphicsConfiguration gc). Creates a frame with the specified GraphicsConfiguration of a screen device. n public Frame(String title, GraphicsConfiguration gc). Creates a frame with the specified title and GraphicsConfiguration. 04/26/12 18Database System Concepts 3.18 ©Silberschatz, Korth and Sudarshan
    • 19. Instantiate Frame Object s Each of the preceding constructors creates a new Frame object that is initially invisible and has a size of 0 pixels wide and 0 pixels high. high s The String passed in to a Frame constructor appears in the title bar, and the Graphics-Configuration represents where the image is to be displayed. s If you do not pass in a GraphicsConfiguration object, your Frame will use the default graphics destination, which in Windows is the computer screen. s The following statement demonstrates instantiating a new Frame object in memory: Frame f = new Frame(“My first window”); 04/26/12 19Database System Concepts 3.19 ©Silberschatz, Korth and Sudarshan
    • 20. Windows s This Frame is not displayed on the screen, and it has an initial size of 0 by 0. s We need to give our Frame a size before displaying it, which can be done by invoking one of the following five methods: s public void setSize(int width, int height). Sets the size of the Frame to the given width and height, in pixels. s public void setSize(java.awt.Dimension d). Sets the size of the Frame to the same width and height as the given Dimension object. s public void setBounds(int x, int y, int width, int height). Sets both the size and initial location of the window, where x represents the number of pixels over from the upper-left corner of the screen, and y represents the number of pixels down from the upper-left corner of the screen. s public void setBounds(java.awt.Rectangle r). Sets the bounds 04/26/12 of the Frame to that of the given Rectangle. 20Database System Concepts 3.20 ©Silberschatz, Korth and Sudarshan
    • 21. Create Window s After we have 5instantiated a Frame, 5given it a size, and 5laid out the components within it, 5we display the Frame on the screen by invoking the setVisible() method inherited from the Component class. s The signature of setVisible() is: public void setVisible(boolean show) s If the boolean passed in is true, the component is made visible. If the value is false, the component is hidden. 04/26/12 21Database System Concepts 3.21 ©Silberschatz, Korth and Sudarshan
    • 22. FrameDemo program s The following FrameDemo program creates a Frame object, sets its bounds, and displays it on the screen. s Study the program and try to determine its output, import java.awt.*; public class FrameDemo { public static void main(String [] args) { Frame f = new Frame(“My first window”); f.setBounds(100,100, 400, 300); f.setVisible(true); } } 04/26/12 22Database System Concepts 3.22 ©Silberschatz, Korth and Sudarshan
    • 23. Window We can move, resize, minimize, and maximize the Frame window. However, we can’t close the window because closing a window often implies ending the program. If the user needs to save a document or other settings before ending, your program needs a chance to do this. The closing involves handling the WindowEvent generated by a user attempting to close the window. 04/26/12 23Database System Concepts 3.23 ©Silberschatz, Korth and Sudarshan
    • 24. Creating Window with Swing s Let’s look at the steps involved in creating a JFrame. You start by instantiating a JFrame using one of the following constructors: s public JFrame(). Creates a new JFrame with no message in the title bar. s public JFrame(String title). Creates a new JFrame with the given String appearing in the title bar. s public JFrame(GraphicsConfiguration gc). Creates a JFrame with the specified GraphicsConfiguration of a screen device. s public JFrame(String title, GraphicsConfiguration gc). Creates a Jframe with the specified title and GraphicsConfiguration. 24Database System Concepts 3.24 ©Silberschatz, Korth and Sudarshan
    • 25. s The constructors are similar to those in the Frame class, and the parameters have the same uses. The following statement instantiates a JFrame with “My first JFrame” in the title bar: s JFrame f = new JFrame(“My first JFrame”); s As with Frame objects, this JFrame is initially not visible and has a size of 0 pixels by 0 pixels. s You invoke one of the setSize(), setBounds(), or pack() methods to give the JFrame a size and then invoke setVisible() to make it visible. 04/26/12 25Database System Concepts 3.25 ©Silberschatz, Korth and Sudarshan
    • 26. s The following JFrameDemo program demonstrates creating and displaying a JFrame object. import javax.swing.*; public class JFrameDemo { public static void main(String [] args) { JFrame f = new JFrame(“My first JFrame”); f.setSize(400, 300); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_ CLOSE); f.setVisible(true); } } 04/26/12 26Database System Concepts 3.26 ©Silberschatz, Korth and Sudarshan
    • 27. Using Swing Clicking the X in the title bar of a JFrame causes the window to be hidden by default, but this does not cause your program to stop executing. We need to press Ctrl+c at the command prompt to stop the JVM, even though our JFrame is no longer visible on the screen. 04/26/12 27Database System Concepts 3.27 ©Silberschatz, Korth and Sudarshan
    • 28. 04/26/12 28Database System Concepts 3.28 ©Silberschatz, Korth and Sudarshan
    • 29. Containers and Components s There are two basic elements of a GUI: containers and components. 5A container is for displaying components, and components must be displayed within a container. 5A Button is an example of a component, whereas a Frame is an example of a container. 5To display a Button, you place it within a Frame and display the Frame. 04/26/12 29Database System Concepts 3.29 ©Silberschatz, Korth and Sudarshan
    • 30. GUI s Component is an abstract class that is the parent class of the various GUI components of the AWT: Button, Checkbox, Choice, Label, List, and Text- Component. s Container is an abstract class that is the parent class of the containers of the AWT: Window, Panel, and ScrollPane. s Child objects of Component are placed within child objects of Container. s For example, a Button can be placed within a Panel, or a List can be placed 04/26/12 30 within a Frame. FrameDatabase System Concepts 3.30 ©Silberschatz, Korth and Sudarshan
    • 31. Adding Components to a Container s AComponent is added to a Container using one of the following add() methods found in the java.awt.Container class: s public Component add(Component c). Adds the Component to the Container and returns a reference to the newly added Component. s public Component add(Component c, int index). Adds the Component to the Container at the position specified by index s public Component add(Component c, Object constraints). Adds the Component to the Container using the specified constraints 04/26/12 31Database System Concepts 3.31 ©Silberschatz, Korth and Sudarshan
    • 32. Components s To demonstrate using the add() method, the following AddDemo program creates a Frame object and adds a Button. import java.awt.*; public class AddDemo { public static void main(String [] args) { Frame f = new Frame(“A simple window”); Button cancel = new Button(“Cancel”); f.add(cancel); //Add the Button to the Frame f.setSize(100,100); f.setVisible(true); } 04/26/12 32 }Database System Concepts 3.32 ©Silberschatz, Korth and Sudarshan
    • 33. Notice that the cancel Button is added to the Frame f. Whenever f is displayed, the cancel button is also displayed Notice that the Button consumes the entire interior of the Frame, no matter what size we make the Frame. 04/26/12 33Database System Concepts 3.33 ©Silberschatz, Korth and Sudarshan
    • 34. Using Jframe s Components are added to a JFrame differently from the way they are added to a Frame. When using a Frame, you invoke the add() method directly on the Frame object, adding the components directly to the Frame. s When using a JFrame, we still invoke the add() method, but not on the JFrame. Instead, we add the components to the content pane of the JFrame by invoking the add() method on the JFrame’s content pane. s We use the getContentPane() method in the JFrame class to obtain a reference to the content pane of a JFrame. For Ex, the following statements add a JButton to the content pane of a JFrame: JFrame f = new JFrame(); JButton b = new JButton(); Container contentPane = f.getContentPane(); contentPane.add(b); 04/26/12 34Database System Concepts 3.34 ©Silberschatz, Korth and Sudarshan
    • 35. s Notice that the return value of getContentPane() is Container. The add() method is invoked on the content pane, adding b by using the layout manager of the content pane. s It involves the concept of a Layout manager. s A container uses a layout manager to determine how components are laid out within the container. s The Frame class uses a BorderLayout manager by default, and the BorderLayout manager has placed the Button in the center of the Frame. 04/26/12 35Database System Concepts 3.35 ©Silberschatz, Korth and Sudarshan
    • 36. s Suppose GUI programming using Visual Basic, and all one could visually place the components exactly where I wanted them in the window using the Visual Basic IDE. Can you do that in Java? s A: You can if you have an IDE like Visual Café or Visual Age. These IDEs have GUI editors that let you place components exactly where you want them. You can also organize components by assigning a null layout manager to your container and specifying the exact location and size of each component added. s Q: So why would you ever use one of the layout managers? Why not just use the IDE or lay out the components exactly where you want them? s A: Two reasons: First, you might not have an IDE, and if you do, there is a certain complexity to figuring out how to use it. If you understand layout managers, this will help you comprehend the code that the IDE is generating for you. s Second, using a layout manager to lay out your components makes your GUI more portable. You might be surprised to see that a GUI that you created using an IDE looks great on Windows, but not so SudarshanDatabase System Concepts 3.36 ©Silberschatz, Korth and
    • 37. Java GUI : Ques. s Q: How does the layout manager know how you want your GUI to look? s A: You need to understand the way each type of layout manager behaves. For example, you need to know that the FlowLayout manager gives components their preferred size, and that BorderLayout places components in specific regions of the container. s By using the different layout managers and nesting containers, you have great control over the look of the GUI, while at the same time letting the layout managers determine the exact location and size of your GUI components.Database System Concepts 3.37 ©Silberschatz, Korth and Sudarshan
    • 38. Layout Managers s A container uses a layout manager to determine both the location and size of the components within the container. A container can be assigned one layout manager, which is done using the setLayout() method of the java.awt.Container class: s public void setLayout(LayoutManager m) s LayoutManager is an interface that all the layout managers’ classes must implement. s You can create your own layout manager by writing a class that implements the methods of the LayoutManager interface (no small task), or 04/26/12 38Database System Concepts 3.38 ©Silberschatz, Korth and Sudarshan
    • 39. s We can use one of the many layout managers of the AWT and Swing APIs, including the following: s java.awt.FlowLayout. Lays out components in a left-to- right flow, with each component given its preferred size. A Panel has FlowLayout by default. s java.awt.BorderLayout. Divides a container into five regions, allowing one component to be added to each region. A Frame and the content pane of a JFrame have BorderLayout by default. s java.awt.GridLayout. Divides a container into a grid of rows and columns, with one component added to each region of the grid and each component having the same size. s java.awt.GridBagLayout. Divides a container into regions similar to GridLayout, except that components do not need to be the same size. Components can span more than one row or column. 39Database System Concepts 3.39 ©Silberschatz, Korth and Sudarshan
    • 40. s java.awt.CardLayout. Each component added to the container is treated as a card, with only one card being visible at a time (similar to a deck of cards). s javax.swing.BoxLayout. Allows components to be laid out vertically or horizontally. BoxLayout is similar to GridBagLayout, but it is generally easier to use. s javax.swing.SpringLayout. Lays out components with a specified distance between the edges of each component. s javax.swing.OverlayLayout. Displays components over the top of each other, similarly to CardLayout. This is a useful layout manager for creating tabbed panes. 04/26/12 40Database System Concepts 3.40 ©Silberschatz, Korth and Sudarshan
    • 41. s Any container can use any layout manager. Notice that Frame objects and the content pane of JFrame objects have BorderLayout by default. s However, you can assign them any layout manager you need. Similarly, Panel objects have FlowLayout by default, but a Panel can be assigned any other layout manager. s There are more commonly used ones, including FlowLayout, BorderLayout, GridLayout, and BoxLayout 04/26/12 41Database System Concepts 3.41 ©Silberschatz, Korth and Sudarshan
    • 42. FlowLayout Manager s The java.awt.FlowLayout class represents a layout manager that aligns components in a left-to-right flow, such as words in a sentence. s FlowLayout has the following properties:  Components are given their preferred size.  ■■ The order in which the components are added determines their order in the container. The first component added appears to the left, and subsequent components flow in from the right.  ■■ If the container is not wide enough to display all of the components, the components wrap around to a new line.  ■■ You can control whether the components are centered, left- justified, or right-justified.  ■■ You can control the vertical and horizontal gap between components. 04/26/12 42Database System Concepts 3.42 ©Silberschatz, Korth and Sudarshan
    • 43. s To use FlowLayout in a Frame or JFrame, you need to invoke setLayout() on the container and pass in a new FlowLayout object. The FlowLayout class has three constructors: s public FlowLayout(). Creates a new FlowLayout that centers the components with a horizontal and vertical gap of five units (where the unit is pixels in most GUI operating systems). s public FlowLayout(int align). Creates a FlowLayout object with the specified alignment, which is one of the following values: FlowLayout .CENTER, FlowLayout.RIGHT, or FlowLayout.LEFT. The horizontal and vertical gap between components is five units. s public FlowLayout(int align, int hgap, int vgap). Creates a FlowLayout object with the specified alignment, horizontal gap, and vertical gap. 04/26/12 43Database System Concepts 3.43 ©Silberschatz, Korth and Sudarshan
    • 44. s For example, the following statement instantiates a new FlowLayout manager that justifies components to the right. The horizontal and vertical gap is not specified, so they will have the default value of 5. s Frame f = new Frame(); s f.setLayout(new FlowLayout(FlowLayout.RIGHT)); s The FlowLayoutDemo program creates a Frame and assigns it FlowLayout. Components are then added using the add() method. s Study the program and see if you can determine its output, which is shown. 04/26/12 44Database System Concepts 3.44 ©Silberschatz, Korth and Sudarshan
    • 45. import java.awt.*; public class FlowLayoutDemo { public static void main(String [] args) { Frame f = new Frame(“FlowLayout demo”); f.setLayout(new FlowLayout()); f.add(new Button(“Red”)); f.add(new Button(“Blue”)); f.add(new Button(“White”)); List list = new List(); for(int i = 0; i < args.length; i++) { list.add(args[i]); } f.add(list); f.add(new Checkbox(“Pick me”, true)); f.add(new Label(“Enter name here:”)); f.add(new TextField(20)); 04/26/12 f.pack(); 45Database System Concepts 3.45 ©Silberschatz, Korth and Sudarshan
    • 46. Flowlayout demo s The FlowLayoutDemo demonstrates using some of the AWT components. s Three Button components are added to the Frame first. Then, a List is created, filled with the command-line arguments, and added to the Frame. Next, a Checkbox, Label, and TextField are added. The pack() method sizes the Frame so all the components fit nicely, as you can see by the output 04/26/12 46Database System Concepts 3.46 ©Silberschatz, Korth and Sudarshan
    • 47. 04/26/12 47Database System Concepts 3.47 ©Silberschatz, Korth and Sudarshan
    • 48. Applets & Event Handling s Event Handling in Java s Delegation model 5 Event Source , Listener & Interface s Using AWT & Swing s Applets & Standalone applications s Using JAR files in Java s Writing Applet 5 Sandbox Security & HTML Intro. 5 HelloWorldApplet Class & Event Handling in Applet s Embedding in HTML s Life cycle of an Applet: Life Cycle methods 04/26/12 48 s Playing Audio with Java AppletDatabase System Concepts 3.48 ©Silberschatz, Korth and Sudarshan
    • 49. Swing Components for GUI The Class Hierarchy depicts that from where Swing components inherit their common attributes and behavior. Class Component ( package.java.awt) is subclass of Object that declares many of the attributes and behaviors common to GUI Component Object Component Container JComponent The Swing API uses many of the AWT classes and interfaces. 49Database System Concepts 3.49 ©Silberschatz, Korth and Sudarshan
    • 50. Event handling s Normally a user interacts with an application’s GUI to indicate the tasks that the application should perform. s When you write an e-mail in the email software, clicking the Send button tells the application to send the email to the specified email addresses. s When the user interacts with a GUI component the interaction- known as an event-drives the program to perform event a task. s Some common events (user-interactions) that might cause an application to perform a task include; 5 clicking a button, 5 typing in a text field, 5 selecting an item from a menu, menu 5 closing a window 5 moving the mouse. s The code that performs a task in response to an event is 50 called an event handler and the overall process ofDatabase System Concepts 3.50 ©Silberschatz, Korth and Sudarshan
    • 51. Event Handling & GUI s GUI programs discussed so far do not have any functionality beyond looking good. s We are required to understand how to create GUI components and how to handle their events. s Now we will discuss the delegation model, the architecture behind event handling in Java. s We will then look at the various components of the AWT and Swing APIs, discussing how to create them and how to handle their events. s Java GUI programming uses the Delegation Model for handling the events of components and containers. s The source of an event invokes a method on a registered listener of the event, with the two objects communicating via a common interface. 51Database System Concepts 3.51 ©Silberschatz, Korth and Sudarshan
    • 52. The Delegation Model s Events in Java are fired and handled using a design known as the delegation model. s With the delegation model, a source generates an event and a listener handles it, creating an object-oriented approach to handling events. (A class is written to handle the events of a component.) s There are 03 players in delegation model:- n The source of the event. In GUI programming, the event component is the source of the event. Events are Java objects that are instantiated by the component and passed as an argument to any listeners. n An event listener. A listener of an event registers itself listener with the source of the event. When an event occurs, the source of the event invokes a method on the listener. n An interface. The interface contains the methods that interface 04/26/12 the listener must implement and that the source of the52Database System Concepts invokes when the event occurs. event 3.52 ©Silberschatz, Korth and Sudarshan
    • 53. Event Handling & Applet s An applet is a Java program that runs in a Web browser. s An applet can be a fully functional Java application because it has the entire Java API at its disposal. s Writing an applet is similar to creating a graphical user interface (GUI) program, because an applet is a Container object. s Containers, Components, Layout managers, and event handling are a big part of developing applets. 04/26/12 53Database System Concepts 3.53 ©Silberschatz, Korth and Sudarshan
    • 54. Applets s An applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. s An applet is typically embedded inside a web page and runs in the context of a browser. s An applet must be a subclass of the java.applet.Applet class. s The Applet class provides the standard interface between the applet and the browser environment. s Swing provides a special subclass of the Applet class called javax.swing.JApplet. s The JApplet class should be used for all applets that use Swing components to construct their graphical user interfaces (GUIs). 54Database System Concepts 3.54 ©Silberschatz, Korth and Sudarshan
    • 55. Applets There are some important differences between an applet and a standalone Java application, including the following: s An applet is a Java class that extends the java.applet.Applet class. s A main() method is not invoked on an applet, and an applet class will (typically) not define main(). s Applets are designed to be embedded within an HTML page. s When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user’s machine. s A user must have a JVM on his or her machine. The JVM can be either a plug-in of the Web browser or a separate runtime environment. s The JVM on the user’s machine creates an instance of the applet class and invokes various methods during the applet’s lifetime. 55Database System Concepts 3.55 ©Silberschatz, Korth and Sudarshan
    • 56. Truly Platform independent s Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child security playing in a sandbox with various rules that must be followed. s Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file. s Because applets are a part of a Web page, however, they can be accessed by any Web browser using any operating system on any device, and therefore can be executed on many different platforms and devices. s I can run an applet using Windows XP and Internet Explorer, and you can run the same applet on a Macintosh running Netscape Navigator. s We will see how to write an applet and embed it in an 56 04/26/12 HTMLpage.Database System Concepts 3.56 ©Silberschatz, Korth and Sudarshan
    • 57. Sandbox Security s Applets run in a Web browser restricted to a set of security policies referred to as sandbox security. s The purpose of the sandbox security model is to ensure that the applet you are downloading and executing is not going to do terrible things to your computer. s This is especially important in today’s Internet world of viruses and other undesirable side effects of software applications. s Applets that are downloaded and executed in a Web browser must adhere to the following rules:  An applet cannot access any files on a user’s operating system.  An applet can only create network connections to the applet’s code base, and cannot connect to other network addresses.  An applet cannot execute a program on the user’s machine.  An applet cannot access system information or use system dialog boxes such the Open File or Print dialog boxes. 04/26/12 57Database System Concepts 3.57 ©Silberschatz, Korth and Sudarshan
    • 58. Using JAR files in Java s The J2SDK comes with a tool called jar for creating JAR (Java ARchive) files. s JAR files are used in all aspects of Java, and the further you progress in your Java programming, the more you will realize that JAR files are everywhere. s The reason they are so widely used is because both Java compilers and JVMs can read files from a JAR without requiring the JAR file to be uncompressed. s You can take the largest of Java applications, consisting of any number of .class files, and compress all these files into a single JAR file. s Your application can then be deployed by simply giving someone the JAR file, and they do not even have to uncompress it to execute it. s It is no surprise, therefore, that JAR files are a common aspect of 04/26/12 58 applets.Database System Concepts 3.58 ©Silberschatz, Korth and Sudarshan
    • 59. Playing Audio using Java Applets s Java has the feature of playing the sound file. s We can see how to play a audio clip in our java applet viewer or on the browser. s An applet can play an audio file represented by the AudioClip interface in the java.applet package. package s The AudioClip interface has three methods to get an Audio file play, stop or replay the Audio file. s Many sites detailing audio features of Java 04/26/12 59 applets are there :Database System Concepts 3.59 ©Silberschatz, Korth and Sudarshan
    • 60. Delegation Model & Working of Java Applet 04/26/12 60Database System Concepts 3.60 ©Silberschatz, Korth and Sudarshan
    • 61. Delegation Model s The Delegation model delegates an event’s processing to the Event listener. s Many different type of event can occur when the user interacts with a GUI. s The information about any GUI event that occurs is stored in an object of a class that extends AWTEvent s Next page illustrates a hierarchy containing many event classes from the package java.awt.event. s These event types are used with both AWT Swing components. 04/26/12 61Database System Concepts 3.61 ©Silberschatz, Korth and Sudarshan
    • 62. Some Event classes of package java.awt.event 04/26/12 62Database System Concepts 3.62 ©Silberschatz, Korth and Sudarshan
    • 63. Event handling s For example, when a user clicks a java.awt.Button, the Button Button generates a java.awt.event.ActionEvent. ActionEvent s The Button invokes the actionPerformed() method on each registered listener of the Button, passing in the ActionEvent object. object s The actionPerformed() method is defined in the java.awt.event.ActionListener interface, which each listener must implement. s In this scenario, the Button is the source of the event, the interface is ActionListener, and the listener is any class that implements ActionListener and registers itself with the Button. 04/26/12 63Database System Concepts 3.63 ©Silberschatz, Korth and Sudarshan
    • 64. Events handling : Ques. s Q: How do you register a listener with a Button? s A: Two steps are involved. You first need to write a class that implements ActionListener. You then invoke the addActionListener() method on the Button, passing in an instance of your class. s Q: So do all components generate an ActionEvent? s A: No. There are many types of events, and each event has a corresponding listener interface. For example, windows generate WindowEvent objects and invoke a method from the WindowListener interface. A check box generates an ItemEvent and invokes a method in the ItemListener interface.Database System Concepts 3.64 ©Silberschatz, Korth and Sudarshan
    • 65. Event Listener Interfaces s Java uses a standard naming convention for event classes and listener interfaces: 5The name of the event class uses the convention <Name>Event, and 5the corresponding listener interface uses the convention <Name>Listener. s For example, the ActionEvent class is associated with the one method of the ActionListener interface, and the WindowEvent class is associated with the seven methods of the WindowListener interface. 04/26/12 65Database System Concepts 3.65 ©Silberschatz, Korth and Sudarshan
    • 66. s The event listener interface contains the methods that the event source invokes on the listener, s And it provides the means of communication between the source of the event and the listener of the event. s Each type of event has a corresponding listener interface. s An event listener interface extends the java.util.EventListener interface. s The EventListener interface does not contain any methods, but is used for tagging an event listener interface for use with the delegation model of event handling. 04/26/12 66Database System Concepts 3.66 ©Silberschatz, Korth and Sudarshan
    • 67. Event listener interface  For example, the ActionListener interface is defined as: package java.awt.event; public interface ActionListener extends java.util.EventListener { public void actionPerformed(ActionEvent e); } s Notice that ActionListener extends EventListener and is in the java.awt.event package, which is where all the AWT event classes and listener interfaces are defined. s The javax.swing.event package contains the event classes and listener interfaces unique to Swing. s The AWT components only generate AWT events, while Swing components generate both AWT and Swing events. 04/26/12 67Database System Concepts 3.67 ©Silberschatz, Korth and Sudarshan
    • 68. 04/26/12 68Database System Concepts 3.68 ©Silberschatz, Korth and Sudarshan
    • 69. Applets s An applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. s An applet is typically embedded inside a web page and runs in the context of a browser. s An applet must be a subclass of the java.applet.Applet class. s The Applet class provides the standard interface between the applet and the browser environment. s Swing provides a special subclass of the Applet class called javax.swing.JApplet. s The JApplet class should be used for all applets that use Swing components to construct their graphical user interfaces (GUIs). 69Database System Concepts 3.69 ©Silberschatz, Korth and Sudarshan
    • 70. Applets There are some important differences between an applet and a standalone Java application, including the following: s An applet is a Java class that extends the java.applet.Applet class. s A main() method is not invoked on an applet, and an applet class will (typically) not define main(). s Applets are designed to be embedded within an HTML page. s When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user’s machine. s A user must have a JVM on his or her machine. The JVM can be either a plug-in of the Web browser or a separate runtime environment. s The JVM on the user’s machine creates an instance of the applet class and invokes various methods during the applet’s lifetime. 70Database System Concepts 3.70 ©Silberschatz, Korth and Sudarshan
    • 71. Truly Platform independent s Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed. s Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file. s Because applets are a part of a Web page, however, they can be accessed by any Web browser using any operating system on any device, and therefore can be executed on many different platforms and devices. s I can run an applet using Windows XP and Internet Explorer, and you can run the same applet on a Macintosh running Netscape Navigator. s We will see how to write an applet and embed it in an HTMLpage. s This will involve writing some HTML, so basics will be reqd. s JAR files will also be discussed in detail because they are important 04/26/12 aspects of applets. 71Database System Concepts 3.71 ©Silberschatz, Korth and Sudarshan
    • 72. Sandbox Security s Applets run in a Web browser restricted to a set of security policies referred to as sandbox security. s The purpose of the sandbox security model is to ensure that the applet you are downloading and executing is not going to do terrible things to your computer. s This is especially important in today’s Internet world of viruses and other undesirable side effects of software applications. s Applets that are downloaded and executed in a Web browser must adhere to the following rules:  An applet cannot access any files on a user’s operating system.  An applet can only create network connections to the applet’s code base, and cannot connect to other network addresses.  An applet cannot execute a program on the user’s machine.  An applet cannot access system information or use system dialog 04/26/12 boxes such the Open File or Print dialog boxes. 72Database System Concepts 3.72 ©Silberschatz, Korth and Sudarshan
    • 73. Sandbox Security s An applet can be granted permission to leave the sandbox and perform an otherwise restricted operation. s For example, you can grant an applet permission to access files on your local hard drive. s Of course, you will want to make sure you trust the source of the applet before granting such permission. s An applet can also be signed, which involves creating a security certificate. s This is the typical way to create an applet that needs to perform tasks outside the sandbox because it provides the user of the applet with some assurance as to the source of the applet, letting the user decide whom he or she trusts. Creating a certificate and associating permissions with it are s beyond the scope & for more information, check Sun’s 04/26/12 73 Java Web site at http://java.sun.com/.Database System Concepts 3.73 ©Silberschatz, Korth and Sudarshan
    • 74. Sandbox Security s Q: How does the sandbox enforce these rules? s A: The security permissions are enforced by the JVM. s Q: Suppose a programmer familiar with Java security writes an applet that grants itself permission to break the rules. Can this be done? s A: No, but there always seem to be holes in any security mechanism. I will say this: It would be extremely difficult to write an applet that steps outside its sandbox without the user granting it permission. It is probably easier for someone to write an applet that tricks a user into agreeing to a signed certificate so that the applet could do anything it wanted on the person’s machine than it is to write Java code that bypasses the built-in security features of applets and the JVM. s Q: So applets really are not that secure, are they? s A: No. Applets by their nature are much safer than other Web applications that do not have a sandbox-type security.If a user has security turned on, an applet cannot leave its sandbox without the express permission of the user. An applet has much tighter security restrictions than HTML, JavaScript, and other widely used Web development technologies.Database System Concepts 3.74 ©Silberschatz, Korth and Sudarshan
    • 75. Sandbox Security s Q: Can I turn off the security permissions so my own applets can run on my machine and perform actions such as accessing the local file system? s A: Certainly, how to do this using Microsoft Internet Explorer. You will find that Microsoft has hidden this feature deep in the browser settings, so you will need to work closely. s A: You can if you have an IDE like Visual Café or Visual Age. These IDEs have GUI editors that let you place components exactly where you want them. You can also organize components by assigning a null layout manager to your container and specifying the exact location and size of each component added. 04/26/12 75Database System Concepts 3.75 ©Silberschatz, Korth and Sudarshan
    • 76. The java.applet.Applet Class s An applet is a Java class; if the applet is to be viewed in a Web browser, the class must extend the java.applet.Applet class. s The Applet class provides a common interface so that a Web browser can communicate with the applet. s The intersting note about the Applet class is that it extends java.awt.Panel. s Panel is the simplest container class. A panel provides space in which an application can attach any other component, including other panels. 04/26/12 76Database System Concepts 3.76 ©Silberschatz, Korth and Sudarshan
    • 77. Panel Container for Applet The Class Hierarchy depicts that Panel inherit their common attributes and behavior . Class Panel ( package.java.awt) is subclass of Container and Applet is subclass of Panel Object Component Container Panel Applet 77Database System Concepts 3.77 ©Silberschatz, Korth and Sudarshan
    • 78. Writing Applet s An Applet Class extends Panel means that an applet is a panel, which is a java.awt.Container; panel 5 therefore, an applet can have components added to it just like any container, 5 as well as have a Layout manager assigned to it, and you can even nest panels within an applet to create the GUI you want. s The default layout manager for a panel is the FlowLayout layout manager. s Before taking a look at a simple Applet class named HelloWorldApplet which extends java.applet.Applet, introductory concepts of HTML will be required. s04/26/12 78Database System Concepts 3.78 ©Silberschatz, Korth and Sudarshan
    • 79. Introduction to HTML s HTML stands for HyperText Markup Language and is the language of the <strong> Hello, World Internet. </strong> s A Web page is an HTML document, Notice that a forward slash is and HTML looks nothing like the page used to denote the closing tag. you actually view. s Your Web browser takes the HTML and Not all tags require a closing marks it up (thus the term “markup” tag, however, most tags come language). in pairs with an opening and closing tag. s Let us cover a few of the basics so you can create the necessary Web pages An HTML document is a text to view your applets. HTML consists of file saved with either a .htm tags, which appear in angle brackets or .html extension. <>. s Most tags come in pairs, with an opening and closing tag. For example, the following <strong> tag makes the 79 string “Hello, HTML” appear in bold.Database System Concepts 3.79 ©Silberschatz, Korth and Sudarshan
    • 80. HTML s The root tag of an HTML The <head> tag can contain document is <html>, and the <title> tag, which the <html> tag can nest denotes the text to appear the optional <head> and in the title bar of the <body> tags: browser’s window. s For example, the following Web <html> page displays Welcome in the title bar of the browser and <head> defines keywords that are used </head> by search engines to determine the content of the page: <body> <header> </body> <title>Welcome</title> </html> <meta name=”keywords” content=”java, training, 04/26/12 courseware, 80Database System Concepts books”> 3.80 ©Silberschatz, Korth and Sudarshan
    • 81. HTML embedding Applet s Three attributes of the <applet> tag that are required: 5 Code: The name of the applet class for this applet. 5Width: The width in pixels of the applet. 5Height: The height in pixels of the applet. s For example, if the name of your applet class is com.world.MyApplet, the following HTML embeds an instance of MyApplet in a Web page: <applet code=”com.world.MyApplet” width=”400” height=”500”> 04/26/12 81 </applet>Database System Concepts 3.81 ©Silberschatz, Korth and Sudarshan
    • 82. HelloWorldApplet Class s Few comments about this applet:  This Class is Container & adds a button .  There is no main() & Most of the code of the HelloWorldApplet class appears in the init() method, which is overriding the init() method from the parent class Applet.  The Web browser invokes init() immediately after it creates an instance of HelloWorldApplet Class.  We could have used a constructor, but we wanted to demonstrate the init() method. s Within init(), the layout of the applet is changed to BorderLayout. (It had FlowLayout by default.)  The event handling is done in the ensuing PrintHello class. Study the following code, and try to determine what this applet does. 82 s A PrintHello object is listening for an ActionEvent from the Go and SudarshanDatabase System Concepts 3.82 ©Silberschatz, Korth
    • 83. import java.applet.*; import java.awt.*; import java.awt.*; import java.awt.event.*; public class PrintHello implements public class HelloWorldApplet extends Applet ActionListener { { private Button go; private Label label; private TextField name; private TextField textField; private Label hello; public PrintHello(Label s, TextField t) public void init() { label = s; { textField = t; go = new Button(“Go”); } name = new TextField(); public void actionPerformed(ActionEve hello = new Label(“”, Label.CENTER); a) { this.setLayout(new BorderLayout()); String name = textField.getText(); this.add(name, BorderLayout.NORTH); if(name != null && !(name.equals(“”))) Panel center = new Panel(); { center.add(go); label.setText(“Hello, “ + name); this.add(center, BorderLayout.CENTER); } } this.add(hello, BorderLayout.SOUTH); } //Set up the event handling. PrintHello listener = new PrintHello(hello, 83 name);Database System Concepts 3.83 ©Silberschatz, Korth and Sudarshan
    • 84. Applet s When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user’s machine.  A user must have a JVM on his or her machine. The JVM can be either a plug-in of the Web browser or a separate runtime environment.  The JVM on the user’s machine creates an instance of the applet class and invokes various methods during the applet’s lifetime. 04/26/12 84Database System Concepts 3.84 ©Silberschatz, Korth and Sudarshan
    • 85. Applet s This applet is displayed in a Web page named hello.html using the <applet> tag. s The HTML looks similar to: <html> <body> <h2>Enter your name and click the button.</h2> <applet code=”HelloWorldApplet” HelloWorldApplet width=”200” height=”75”> </applet> </body> </html> 04/26/12 85Database System Concepts 3.85 ©Silberschatz, Korth and Sudarshan
    • 86. Sample view of HelloWorldApplet in hello.html file Sample 04/26/12 86Database System Concepts 3.86 ©Silberschatz, Korth and Sudarshan
    • 87. Applet embedding s An applet can actually be embedded within any other application, not just a Web browser. s If your applet is going to be embedded in a Web page, it must extend the Applet class. If your applet is going to be embedded in some other application, extending Applet is not required. s On the same line, we may develop Swing Applets using JApplet. s JApplet class is subclass of Applet so it inherites all the methods of Applet. s The purpose of the JApplet class is to provide support for Swing. s Probably the biggest difference between an applet and a JApplet is how components are added to them. s A JApplet has three panes, much like a JFrame, and components are added to the content pane of the 87 Japplet.Database System Concepts 3.87 ©Silberschatz, Korth and Sudarshan
    • 88. Life Cycle methods of an Applet s Let us discuss the five applet methods that are called by the applet container from the time the applet is loaded into the browser to the time that it is terminated by the browser. s These methods correspond to various aspects of an applet’s life cycle. s These methods are inherited from class Japplet. that the browser invokes these methods on your applet. n public void init(). n public void start(). n public void stop(). n public void destroy(). 04/26/12 88 n public void paint(Graphics 3.88Database System Concepts g). ©Silberschatz, Korth and Sudarshan
    • 89. n public void init(). The first method invoked on the applet when it is initially instantiated. This is your chance to perform any initialization, such as locating resources or preparing event handlers. n public void start(). Invoked by the browser to inform the applet that it should start executing. The start() method is called right after the init() method, and is also called when the page is revisited. This is a good time to start any threads or other tasks like displaying animation or playing sound. n public void stop(). Invoked by the Web browser to inform the applet that it should stop executing. The stop() method is called right before the destroy() method is invoked, and also when a user leaves the Web page.Typically, anything you started in the start() method is stopped in the stop() method. n public void destroy(). Invoked by the Web browser to inform the applet that it is about to be destroyed (in other words, garbage collected). Typically,any resources allocated in the init() method are freed in the destroy() method. n public void paint(Graphics g). Invoked immediately 89Database Systemafter the start() method, and also any time the applet needs Sudarshan Concepts 3.89 ©Silberschatz, Korth and
    • 90. When a user views a Web page that contains an applet, the following sequence of events occurs regarding the life cycle of the applet: 1. The Web browser downloads the necessary bytecode and JAR file from the Web server where the code is located. (This Web server is referred to as the code base.) 2. The browser creates an instance of the Applet class, invoking the default constructor. 3. The applet is displayed in the Web page, with the location and size of the applet determined by the HTML. 4. The browser invokes the init() method on the applet. 5. The browser invokes the start() method on the applet. 6. The browser invokes the paint() method on the applet. 7. The applet is now live and running within the Web page. 8. The browser calls paint() whenever the applet needs to repaint itself. 9. The browser invokes the stop() method when the user leaves the Web page or the applet is about to be destroyed. 90 10. The browser invokes the destroy() method just beforeDatabase System Concepts 3.90 ©Silberschatz, Korth and Sudarshan
    • 91. Applet & HTML s If a Web browser does not understand the <applet> tag, it will ignore the tag and display any HTML that appears within the opening and closing <applet> tags. s For example, the following HTML displays a message that the user is unable to see the applet that was intended to appear: <applet code=”com.world.MyApplet” width=”200” height=”348” alt=”MyApplet failed”> failed <h2>Your browser does not support applets!</h2> <p>To view this page correctly, you will need to find a Web browser that provides support for applets, or install the 04/26/12 91 Java Plug-in.</p>Database System Concepts 3.91 ©Silberschatz, Korth and Sudarshan
    • 92. Applet & HTML s Visitors to this page who have a Web browser that supports applets will not see the message about their browser not supporting applets. Note that if their browser supports applets but, for some reason, cannot run applets, the visitor will see the alt message “MyApplet failed.” 04/26/12 92Database System Concepts 3.92 ©Silberschatz, Korth and Sudarshan
    • 93. 04/26/12 93Database System Concepts 3.93 ©Silberschatz, Korth and Sudarshan
    • 94. 04/26/12 94Database System Concepts 3.94 ©Silberschatz, Korth and Sudarshan
    • 95. Playing Audio s Java has the feature of the playing the sound file. s Let us see how to play a audio clip in your java applet viewer or on the browser. s An applet can play an audio file represented by the AudioClip interface in the java.applet package. package s The AudioClip interface has three methods, including: public void play()- Plays the audio clip one time, from the beginning. public void loop()- Causes the audio clip to replay continually. public void stop()- Stops playing the audio clip. s To obtain an AudioClip object, you must invoke the getAudioClip() method of the Applet class: 95 s public AudioClip getAudioClip(URL url)Database System Concepts 3.95 ©Silberschatz, Korth and Sudarshan
    • 96. Audio using Applet s The getAudioClip() method returns immediately, whether or not the URL resolves to an actual audio file. s The audio file is not downloaded until an attempt is made to play the audio clip. s The following AudioDemo applet demonstrates playing an audio clip that is specified as an applet parameter. Study the class, and try to determine how it looks and what it does. 04/26/12 96Database System Concepts 3.96 ©Silberschatz, Korth and Sudarshan
    • 97. import java.applet.*; catch(MalformedURLException e) import java.awt.*; { import java.net.*; e.printStackTrace(); context.showStatus(“Could not load audio public class AudioDemo extends Applet file!”); { } private AudioClip clip; } private AppletContext context; public void start() public void init() { if(clip != null) { { context = this.getAppletContext(); clip.loop(); String audioURL = } this.getParameter(“audio”); } if(audioURL == null) public void stop() { { if(clip != null) audioURL = “default.au”; { } clip.stop(); try } { } URL url = new URL(this.getDocumentBase(), } audioURL); 04/26/12 97 clip = context.getAudioClip(url);Database System Concepts 3.97 ©Silberschatz, Korth and Sudarshan
    • 98. Applet Communication s How two applets on the same Web page can communicate with each other using the applet context. s You will have write two applets: one that plays an audio clip, and a second applet that controls which audio clip is played. 04/26/12 98Database System Concepts 3.98 ©Silberschatz, Korth and Sudarshan
    • 99. 04/26/12 99Database System Concepts 3.99 ©Silberschatz, Korth and Sudarshan
    • 100. 04/26/12 100Database System Concepts 3.100 ©Silberschatz, Korth and Sudarshan
    • 101. 04/26/12 101Database System Concepts 3.101 ©Silberschatz, Korth and Sudarshan

    ×