Handling User Events with Swing

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: - Maggiore flessibilità - Minor.
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.
TCU CoSc Programming with Java Handling Events.
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
Event Handling.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 16 Java Fundamentals Java2 Graphical User Interfaces.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
Events in Java Swing Chris North cs3724: HCI. Typical command line program Non-interactive Linear execution program: main() { code; }
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
Graphical User Interface Components: Part 1 Chapter 11.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
– 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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 15 Java Fundamentals Java2 Graphical User Interfaces.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
UID – Event Handling and Listeners Boriana Koleva
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
© Marty Hall, Larry Brown Web core programming 1 Handling Mouse and Keyboard Events.
Object Oriented Programming.  Interface  Event Handling.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
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.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
Lecture 8 Object Oriented Programming Using Java
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Chapter 12 Event-Driven Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Mouse Event Handling in Java (Review)
Advanced User Interfaces
Lecture 09 Applets.
Programming in Java Event Handling
CSE 114 – Computer Science I Event Programming
GUI Programming III: Events
Event-driven programming for GUI
Unit I: Chapter 1 Event handling.
Web Design & Development Lecture 12
Chapter 13—Understanding Swing Components
Chapter 16 Event-Driven Programming
Making Java GUIs Functional
Final project.
Presentation transcript:

Handling User Events with Swing Program Swing: May re-do old Java program Update it using Swing

Swing Handles Different through use of classes of event listners ActionListener =click on button AdjustmentListener like scrollbar move FocusListener = keyboard (text field) ItemListener = like check box change KeyListener = enter text from keys

More Swing Handles MouseListener = clicks or moves out of area MouseMotionListener = mouse moves track it WindowListener = max, min, move, close it public class Suspense extends Jframe implements ActionListner, TextListener { …}

Setting up Components addActionListener( ) Jbutton…. addAdjustmentListener ( ) JScrollBar … addFocusListener( ) All Swing Components addItemListener ( ) ..Jbutton … addKeyListener ( ) All Swing Components addMouseListener( ) “ “ addMouseMotionListener( ) “ “

Last few addMouseMotionListener( ) All Swing Components addWindowListener( ) All Jwindow and Jframe Components Jbutton zap = new Jbutton( “Zap”); zap.addActionListener(this);

Program CHANGETITLE.JAVA Results of execution “Rosencrantz” “Guildenstern”

Working with Methods Action Events Jbutton, JCheckBox, JTextField, Jradio actionPerformed(ActionEvent evt) { ….}

Statements create JButton & JTextField Jbutton sort = new Jbutton(“Sort”); JTextField name = new JTextField( ); sort.setActionCommand(“Sort Files:); name.setActionCommand(“Sort Files”); Both cause same thing to happen

Adjustment Events Happen when JScrollBar component moved by means of arrows on the bar, box, clicks public void adjustmentValueChanged(AdjustmentEvent evt) { …}

AdjustmentEvent object’s getAdjustmentType( ) method UNIT-INCREMENT = +1 UNIT_DECREMENT = -1 BLOCK_INCREMENT = LARGER INCREASE BLOCK_DECREMENT = LARGER DECREASE TRACK = VALUE CHANGE - moving the box

WellAdjusted.java A scrollbar showing an integer of where the thumb button is in relation to the bar (position)

Null String In last program “ “ is a null String. The int value concatenated into a String “ “ + 73 is then a String “ 73” Focus Events = a gain/loss of focus on GUI The blinking field is in focus = text may then be entered

More Focus Events public void focusGained(FocusEvent evt) {…} public void focusLost(FocusEvent evt) { …} Determine which object has gained/lost focus

Item Events Occur when an item is selected/deselected (Jbutton, JCheckBox, JComboBox,JRadioButton) void itemStateChanged(ItemEvent evt) { …}

SelectItem.java Source code Produces output Which pick.additem(“ text of item”); Scroll down button click on item desired Text of item displayed

Key Events Occur when a key is pressed on the keyboard public void keyTyped(KeyEvent evt) {…}

Mouse Events A mouse click A mouse enters component’s area A mouse leaves “ “ MouseListener (5) Events: mouseClicked(MouseEvent) …ALSO mouseEntered, mouseExited, mousePressed, mouseReleased public void mouseReleased(MouseEvent evt) {…}.

More Mouse stuff getClickCount( ) getPoint( ) getX( ) getY( ) MOUSE-MOVEMENT EVENTS mouseDragged(MouseEvent evt) … mouseMoved(MouseEvent evt)...

Swing with old SPOTS & LINES Program A possible suggestion import java.awt.swing.*; With your old Spots/Lines program

Window Events WindowActivated(WindowEvent) windowClosed( ) windowClosing( ) windowDeactivated( ) windowDeiconified( ) windowIconified() windowOpened( )

Last Progam SwingColorTest.java Output Same as Old ColorTest program Red - Green - Blue OR Hue Saturation Brightness BUT with Swing Text official web site http://www.prefect.com/java21