1 / 14

Java GUI Components and Events

Java GUI Components and Events. Learning Outcomes Distinguish between GUI components and containers. Identify and distinguish top-level containers from other containers. Write simple programs to add components into containers.

Download Presentation

Java GUI Components and Events

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java GUI Components and Events • Learning Outcomes • Distinguish between GUI components and containers. • Identify and distinguish top-level containers from other containers. • Write simple programs to add components into containers. • Explain 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 Unit 11

  2. 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 Unit 11

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

  4. 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 Unit 11

  5. 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: Unit 11

  6. 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. Unit 11

  7. 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 } Unit 11

  8. 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 } Unit 11

  9. 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 listenersare three groups of Java classes crucial for event-driven programming. Unit 11

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

  11. Events Source Classes • An event source is the component that generates an event. • A source must registerlisteners 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. Unit 11

  12. 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. Unit 11

  13. Events: A Pictorial View Unit 11

  14. Exercises • The Panel, Window and JComponent class each subclasses Component class. Write down the similarities and differences between these classes in a tabular form. • Write down in tabular form the similarities and differences between top-level components and other GUI components. • 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. • 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 packmethod. Re-run the program and notice the output. • Write short notes on a. Event classes b. Event sources c. Event listeners Unit 11

More Related