Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Similar presentations


Presentation on theme: "Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers."— Presentation transcript:

1 Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers from other containers. oWrite simple programs to add components into containers. oExplain what Events are, and distinguish between Event sources, Event classes and Event listeners.  GUI Components and Containers  Swing's Top-level Containers  Adding Components to Containers  Self-check Exercise 1  GUI Events  GUI Events Classes  Self-check Exercise 2  Exercises

2 Unit 112 Introduction to Components A component is an object having a graphical representation that can be displayed on the screen. Example of components in a typical GUI include: buttons, text boxes, lables and pop-up menus

3 Unit 113 Introduction to Containers  A container is a special component that can hold other components. Example of containers in typical GUI applications include: opanels, windows, applets, frames Functionality of most GUI components derive from the Component and Container classes.

4 Unit 114 Swing’s Top-level Containers JApplet, JWindow, JFrame and JDialog are the top-level Swing containers. Strictly speaking, JWindow and JFrame are the top-level containers. However, use top-level classes to refer to JApplet, JWindow, JFrame and JDialog. A top-level container can be displayed without being added to another container. Top-level containers are the only heavyweight Swing components. If you create an AWT window and add 3 buttons to it, AWT will create four peers for you. Top-level containers do not behave exactly like other containers

5 Unit 115 Anatomy of Top-level Containers Each top-level container has only one component, JRootPane. The root pane consists of three other containers: the layered, content and glass panes:

6 Unit 116 Anatomy of Top-level Containers (cont’d) The root pane has a glass pane on top and a layered pane underneath. The glass pane component is always painted last and appears on top of the content pane and menu bar. The layered pane consists of a menu bar and a content pane. A content pane is a Container that covers the visible area of a container. A content pane is automatically created for a newly created container. Components must be added to the content pane of a container. The content pane can be retrieved using the getContentPane method.

7 Unit 117 Example 1: Creating Windows & Frames 1 import java.awt.*; import javax.swing.*; 2 public class TopLevelWindows{ 3 public static void main(String [] args){ 4 JFrame frame = new JFrame("My JFrame"); 5 frame.setLocation(100,100); 6 frame.setSize(300,300); 7 Container fcp = frame.getContentPane(); 8 JButton fb = new JButton("Draggable, Resizable Frame"); 9 fcp.add(fb); 10 11 JWindow window = new JWindow(); 12 window.setLocation(500,100); 13 window.setSize(300,300); 14 Container wcp = window.getContentPane(); 15 JButton wb = new JButton("Unmovable, No Frills Window"); 16 wcp.add(wb); 17 18 frame.setVisible(true); 19 window.setVisible(true); 20 } 21 }

8 Unit 118 Example 2: Adding Components to Containers 1 import java.awt.*; 2 import javax.swing.*; 3 public class AddingComponents extends JFrame{ 4 5 JButton button = new JButton("Press Me"); 6 JLabel label = new JLabel( "Running Total:"); 7 JTextField textField = new JTextField(10); 8 Container cp = getContentPane(); 9 10 public AddingComponents() { 11 super("A Container With Components"); 12 setSize(300,100); 13 cp.setLayout(new FlowLayout()); 14 cp.add(label); 15 cp.add(textField); 16 cp.add (button); 17 setVisible(true); 18 } 19 public static void main(String args []) { 20 new AddingComponents(); 21 } 22 }

9 Unit 119 Introduction to GUI Events We will now discuss how components and containers communicate. When a user interacts with a GUI component, events are triggered. An event is an action triggered by the user or by some other means. When an event is triggered an event object is created and delivered to the event receivers. Execution of these kinds of programs is driven by users’ activation of events. Event classes, event sources and event listeners are three groups of Java classes crucial for event-driven programming.

10 Unit 1110 Events Classes Hierarchy Event classes represent events and contain methods for getting information on the events. Here is the class hierarchy for events classes:

11 Unit 1111 Events Source Classes An event source is the component that generates an event. A source must register listeners who may wish to take some action when the event is generated. When an event occurs the source informs all registered listeners for this event. An event listener can register or unregister with the following methods: 1. public void addTypeListener(TypeListener t) 2. public void removeTypeListener (TypeListener t) Type is the name of the event and t is a reference to the event listener.

12 Unit 1112 Events Listener Classes The java.awt.event package contains interfaces and classes for dealing with events. Each listener interface is a specification for receiving a particular type of event. An event listener class implements the listener interfaces of interest to the class. Every event handler should satisfy these two conditions: 1. Implement an interface. 2. Register as event listener.

13 Unit 1113 Events: A Pictorial View

14 Unit 1114 Exercises 1.The Panel, Window and JComponent class each subclasses Component class. Write down the similarities and differences between these classes in a tabular form. 2.Write down in tabular form the similarities and differences between top-level components and other GUI components. 3.Write a Java program to display three frames as follows. The top-left corner of the second frame should have the same coordinates as the bottom-right corner of the first frame. Similarly, the top-left corner of the third frame should have the same coordinates as the bottom-right corner of the second frame. 4.Run the program in Example 2. Try resizing the window and watch how the components placements change. Modify this program by adding four more buttons. Remove the setSize method call and replace it with the call to the pack method. Re-run the program and notice the output. 5.Write short notes on a. Event classes b. Event sources c. Event listeners


Download ppt "Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers."

Similar presentations


Ads by Google