MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.

Slides:



Advertisements
Similar presentations
Introduction to JFC Swing Written by Adam Carmi. Agenda About JFC and Swing Pluggable Look and Feel Swing Components Borders Layout Management Events.
Advertisements

Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
Event Handling Events and Listeners Timers and Animation.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Graphical User Interfaces
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
GUI Programming in Java
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Java Swing, Events Readings: Just Java 2: Chap 19 & 21, or Eckel’s Thinking in Java: Chap 14 Slide credits to CMPUT 301, Department of Computing Science.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 3: Layout Basics.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
GUIs & Event-Driven Programming Chapter 11 Review.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Module 13: Swing API Object Oriented Programming(Java)
A Quick Java Swing Tutorial
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CompSci 230 S Programming Techniques
Web Design & Development Lecture 11
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Event-driven programming for GUI
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

MIT AITI 2003 Lecture 17. Swing - Part II

The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up until now, we have focused on GUI's to present information (with one exception) How do GUIs interact with users? How do applications recognize when the user has done something? How do GUIs interact with users? How do applications recognize when the user has done something? In Java this depends on 3 related concepts: In Java this depends on 3 related concepts: –events: objects that represent a user action with the system –event sources: in Swing, these are components that can recognize user action, like a button or an editable text field –event listeners: objects that can respond when an event occurs

Event Sources Event sources can generate events. Event sources can generate events. The ones you will be most interested are subclases of JComponents like JButtons and JPanels The ones you will be most interested are subclases of JComponents like JButtons and JPanels You find out the kind of events they can generate by reading the Javadoc You find out the kind of events they can generate by reading the Javadoc

Event Listeners An object becomes an event listener when its class implements an event listener interface. For example: An object becomes an event listener when its class implements an event listener interface. For example: public interface ActionListener extends EventListener { extends EventListener { public void actionPerformed(ActionEvent e); public void actionPerformed(ActionEvent e);} The event listener gets called when the event occurs if we register the event listener with the event source The event listener gets called when the event occurs if we register the event listener with the event source

Events Events are instances of simple classes that supply information about what happened. Events are instances of simple classes that supply information about what happened. For example, instances of MouseEvent have getX() and getY() methods that will tell you where the mouse event (e.g., mouse press) occurred. For example, instances of MouseEvent have getX() and getY() methods that will tell you where the mouse event (e.g., mouse press) occurred. All event listener methods take an event as an argument. All event listener methods take an event as an argument.

How do I Set Up to Receive an Event? 1. Figure out what type of event you are interested in and what component it comes from. 2. Decide which object is going to handle (act on) the event. 3. Determine the correct listener interface for the type of event you are interested in. 4. Write the appropriate listener method(s) for the class of the handler object. 5. Use an add EventType Listener() method to register the listener with the event source

A Final Example – SwingApplication

Step-by-step Guide Setting up the top-level container Setting up the top-level container Setting up buttons and labels Setting up buttons and labels Adding components to containers Adding components to containers Handling events Handling events

Four Swing components:  A frame ( JFrame ). The frame is a top-level container. It provides a place for other Swing components to paint themselves. The other commonly used top-level containers are dialogs ( JDialog ) and applets ( JApplet ).  A frame ( JFrame ). The frame is a top-level container. It provides a place for other Swing components to paint themselves. The other commonly used top-level containers are dialogs ( JDialog ) and applets ( JApplet ).  A panel ( JPanel ). The panel is an intermediate container. Its only purpose is to simplify the positioning of the button and label. Other intermediate Swing containers include JScrollPane (scroll panes) and JTabbedPane (tabbed panes)  A panel ( JPanel ). The panel is an intermediate container. Its only purpose is to simplify the positioning of the button and label. Other intermediate Swing containers include JScrollPane (scroll panes) and JTabbedPane (tabbed panes)  A button ( JButton ) and a label ( JLabel ). The button and label are atomic components -- components that exist not to hold other Swing components, but to interface with the user. The Swing API provides many atomic components, including combo boxes ( JComboBox ), text fields ( JTextField ), and tables ( JTable ).  A button ( JButton ) and a label ( JLabel ). The button and label are atomic components -- components that exist not to hold other Swing components, but to interface with the user. The Swing API provides many atomic components, including combo boxes ( JComboBox ), text fields ( JTextField ), and tables ( JTable ).

Containment Hierarchy

1. Setting up the top-level container //Create the top-level container titled “SwingApplicatoin” //Create the top-level container titled “SwingApplicatoin” JFrame frame = new JFrame("SwingApplication"); JFrame frame = new JFrame("SwingApplication"); frame.pack(); frame.pack(); frame.setVisible(true); frame.setVisible(true); setDefaultCloseOperation( EXIT_ON_CLOSE ); setDefaultCloseOperation( EXIT_ON_CLOSE );

2. Setting up buttons and labels 2. Setting up buttons and labels //Create a button //Create a button JButton button = new JButton("I'm a Swing button!"); JButton button = new JButton("I'm a Swing button!"); //Create a label //Create a label JLabel label = new JLabel(“Number of button clicks: “ + "0 "); JLabel label = new JLabel(“Number of button clicks: “ + "0 "); //Set the label text //Set the label text int numClicks = 0; int numClicks = 0; label.setText(“Number of button clicks: “ + numClicks); label.setText(“Number of button clicks: “ + numClicks);

3. Adding components to containers 3. Adding components to containers JPanel pane = new JPanel(); pane.setLayout(new GridLayout(0, 1)); pane.add(button); pane.add(label); frame.getContentPane().add(pane, BorderLayout.CENTER); JPanel pane = new JPanel(); pane.setLayout(new GridLayout(0, 1)); pane.add(button); pane.add(label); frame.getContentPane().add(pane, BorderLayout.CENTER);

Recap: GUI part of the Example import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingApplication extends JFrame{ int numClicks = 0; JPanel pane = new JPanel(); JLabel label = new JLabel(“Number of button clicks: “ + numClicks); JButton button = new JButton("I'm a Swing button!"); //Constructor SwingApplication () { super (“SwingApplication”); //set the frame title pane.setLayout(new GridLayout(0, 1)); pane.add(button);pane.add(label); this.getContentPane().add(pane, BorderLayout.CENTER); } this.getContentPane().add(pane, BorderLayout.CENTER); } public static void main(String[] args) { SwingApplication app = new SwingApplication(); app.pack(); app.setVisible(true); } } }

4. Handling Events a. 3 Key components of Event Handling Process Event (click a button, press a key, etc.) Event (click a button, press a key, etc.) Listener interface(ActionListener, WindowListner, etc.) Listener interface(ActionListener, WindowListner, etc.) Object (button, frame, textfield, etc.) who is “listening to” the event Object (button, frame, textfield, etc.) who is “listening to” the event

4. Handling Events b. 3 Steps to Implement an Event Handler 1. Implement a listener interface: public class MyClass implements ActionListener public class MyClass implements ActionListener 2. Add the listener to an object: button.addActionListener(this) button.addActionListener(this) 3. Define the methods of the listener interface: public void actionPerformed(ActionEvent e){...//code that reacts to the action..} public void actionPerformed(ActionEvent e){...//code that reacts to the action..}

4. Handling Events c. SwingApplication Example Event: ActionEvent Event: ActionEvent Object: button Object: button Interface: ActionListener with the method actionPerformed Interface: ActionListener with the method actionPerformed

4. Handling Events c. SwingApplication Example 1. Implement a listener interface: –public class SwingApplication extends JFrame implements ActionListener 2. Add the listener to the button (after it is created) –button.addActionListener(this); –button.addActionListener(this); 3. Define the methods of the listener interface: –Public void actionPerformed (ActionEvent e){ numClicks ++; label.setText (“Number of button clicks: “ + numClicks); }

After 2 clicks ….