40-244 – Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Graphic User Interfaces Layout Managers Event Handling.
Dale Roberts GUI Programming using Java - Mouse Events Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Event Handling Events and Listeners Timers and Animation.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Graphical User Interface Components: Part 1
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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
Java Programming Chapter 10 Graphical User Interfaces.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
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.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Graphical User Interface Components: Part 1 Chapter 11.
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.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CS Lecture 00 Swing overview and introduction Lynda Thomas
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.
Layout Managers Arranges and lays out the GUI components on a container.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Graphical User Interface Components: Part 1 Chapter 11.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
1 Lecture 8: User Interface Components with Swing.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
© Copyright by Pearson Education, Inc. All Rights Reserved. Appendix I GUI Components and Event Handling Android How to Program, 3/e.
Java Swing What is swing? Swing is a ”Lightweight” GUI ToolKit for Java Different from AWT / SWT Toolkits for GUIs Few lines of code to produce GUI elements.
GUI Programming using Java - Event Handling
Lecture 15 Basic GUI programming
Events and Event Handling
Appendix I GUI Components and Event Handling
A First Look at GUI Applications
Advanced User Interfaces
Ellen Walker Hiram College
Introduction to Computing Using Java
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Border Layout, using Panels, Introduction to PinBallGame
Graphical User Interface
Presentation transcript:

– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing

What We Will Learn Overview of the Swing library Working with basic Swing components Event Handling Listeners and Adapters Layout Managers and Panels

Introduction to GUIs Graphical User Interface ("Goo-ee") Pictorial interface to a program Distinctive "look" and "feel" Different applications with consistent GUIs improve productivity GUIs built from components Component: object with which user interacts Examples: Labels, Text fields, Buttons, Checkboxes

Swing Overview Swing GUI components Defined in package javax.swing Original GUI components from Abstract Windowing Toolkit in java.awt Heavyweight components - rely on local platform's windowing system for look and feel

Swing Overview Swing vs. AWT Swing components are lightweight Written in Java, not weighed down by complex GUI capabilities of platform More portable than heavyweight components Swing components allow programmer to specify look and feel Can change depending on platform Can be the same across all platforms

The Inheritance Hierarchy Swing component inheritance hierarchy java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent

java.awt.Component Component defines methods that can be used in its subclasses (for example, paint and repaint ) Some capabilities of subclasses of JComponent Pluggable look and feel Shortcut keys (mnemonics) Direct access to components through keyboard Common event handling If several components perform same actions Tool tips Description of component that appears when mouse over it

java.awt.Container Container - collection of related components When using JFrames, attach components to the content pane (a Container ) Method add

Using Swing A simple application demonstrating JLabel component

Event Handling GUIs are event driven Generate events when user interacts with GUI Mouse movements, mouse clicks, typing in a text field, etc. Event information stored in object that extends AWTEvent Example: Enter pressed in a JTextField Method actionPerformed called for registered listener

Processing Events Register an event listener Object from a class that implements an event- listener interface (from java.awt.event or javax.swing.event ) "Listens" for events Implement event handler Method that is called in response to an event Event handling interface has one or more methods that must be defined

Processing Events Delegation event model Use of event listeners in event handling Processing of event delegated to particular object When an event occurs GUI component notifies its listeners Calls listener's event handling method

Using Event Handlers A simple application that uses JTextField and JPasswordField

How it works? Registering event listeners All JComponents contain an object of class EventListenerList called listenerList When text1.addActionListener( handler ) executes New entry placed into listenerList Handling events When event occurs, has an event ID Component uses this to decide which method to call If ActionEvent, then actionPerformed called (in all registered ActionListeners)

Mouse Event Handling Can be trapped for any GUI component derived from java. awt. Component Mouse event handling methods Take a MouseEvent object Contains info about event, including x and y coordinates Methods getX and getY

Listeners MouseListener Interface public void mousePressed( MouseEvent e ) public void mouseClicked( MouseEvent e ) public void mouseReleased( MouseEvent e ) public void mouseEntered( MouseEvent e ) public void mouseExited( MouseEvent e ) Using addMouseListener MouseMotionListener Interface public void mouseDragged( MouseEvent e ) public void mouseMoved( MouseEvent e ) Using addMouseMotionListener

Handling Mouse Events When implementing mouse events, Time consuming to define all interface methods MouseListener and MouseMotionListener have seven methods What if we only want to use one? Required to define all methods in interface

Adapter Classes Implements an interface Default implementation (empty body) for all methods Programmer extends adapter class Overrides methods he wants to use Has "is a" relationship with interface MouseAdapter is a MouseListener

Adapter Classes ComponentAdapterComponentListener ContainerAdapterContainerListener FocusAdapterFocusListener KeyAdapterKeyListener MouseAdapterMouseListener MouseMotionAdapterMouseMotionListener WindowAdapterWindowListener

Using Adapters A simple drawing example using Adapters

Layout Managers Layout managers Arrange GUI components on a container Provide basic layout capabilities Easier to use than determining exact size and position of every component Programmer concentrates on "look and feel" rather than details Examples for Flow, Border, and Grid Layouts

Panels Class JPanel inherits from JComponent, which inherits from java.awt.Container Every JPanel is a Container JPanels can have components (and other JPanels ) added to them JPanel sized to components it contains Grows to accommodate components as they are added

Using Panels Can use multiple panels Each panel's components arranged in a specific layout Create panels, and set the layout for each Add components to the panels as needed Add the panels to the content pane (default BorderLayout )