Dr. Hikmat A. M. AbdelJaber

Similar documents
14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

CS111: PROGRAMMING LANGUAGE II

Advanced Java Programming (17625) Event Handling. 20 Marks

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Event Driven Programming

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

COMPSCI 230. Software Design and Construction. Swing

Graphical User Interfaces in Java - SWING

The AWT Event Model 9

GUI Components: Part Pearson Education, Inc. All rights reserved.

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Programming Languages: Java

10/16/2008. CSG 170 Round 5. Prof. Timothy Bickmore. User Analysis Task Analysis (6) Problem Scenarios (3)

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

2010 가을학기부산대학교정보컴퓨터공학부 OVERVIEW OF GUI PROGRAMMING

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

User Interface components with swing

EVENTS, EVENT SOURCES AND LISTENERS

SD Module-1 Advanced JAVA

Systems Programming Graphical User Interfaces

SD Module-1 Advanced JAVA. Assignment No. 4

Assignment No 2. Year: Dept.: ComputerTech. Sanjivani Rural Education Society s Sanjivani KBP Polytechnic, Kopargaon

12/22/11. Copyright by Pearson Education, Inc. All Rights Reserved.

Java: Graphical User Interfaces (GUI)

Interacción con GUIs

Summary. Section 14.1 Introduction. Section 14.2 Java s New Nimbus Look-and-Feel. 618 Chapter 14 GUI Components: Part 1

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

GUI Programming: Swing and Event Handling

GUI in Java TalentHome Solutions

Chapter 6: Graphical User Interfaces

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

GUI Software Architecture

Swing from A to Z Some Simple Components. Preface

Chapter 13 - Graphical User Interface Components: Part 1

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Dr. Hikmat A. M. AbdelJaber

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Java GUI Design: the Basics

CS 251 Intermediate Programming GUIs: Event Listeners

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

GUI Event Handlers (Part I)

Contents Chapter 1 Introduction to Programming and the Java Language

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Chapter 1 GUI Applications

Programming graphics

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Unit 7: Event driven programming

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Chapter 12 Advanced GUIs and Graphics

SampleApp.java. Page 1

Part I: Learn Common Graphics Components

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

JAVA. Object Oriented Programming with. M.T. Somashekara D.S.Guru K.S. Manjunatha

user-friendly and easy to use.

3 Events and Listeners

BM214E Object Oriented Programming Lecture 13

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

MIT AITI Swing Event Model Lecture 17

SINGLE EVENT HANDLING

Handling Mouse and Keyboard Events

Starting Out with Java: From Control Structures Through Objects Sixth Edition

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

Packages: Putting Classes Together

Which of the following syntax used to attach an input stream to console?

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

PIC 20A GUI with swing

CSE 331 Software Design & Implementation

CSCI 201L Midterm Written Summer % of course grade

CoSc Lab # 6 (The Model) Due Date: Part I, Experiment classtime, Tuesday, Nov 6 th, 2018.

Graphical User Interfaces. Comp 152

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Lecture 19 GUI Events

Lecture 5: Java Graphics

Programação Orientada a Objetos

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Graphical interfaces & event-driven programming

Java AWT Windows, Text, & Graphics

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

GUI Event Handlers (Part II)

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Index SELF-STUDY. Symbols

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

More Swing. CS180 Recitation 12/(04,05)/08

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

Window Interfaces Using Swing Objects

AP Computer Science Unit 13. Still More Graphics and Animation.

IS311 Programming Concepts. GUI Building with Swing

Transcription:

Dr. Hikmat A. M. AbdelJaber

GUI are event driven (i.e. when user interacts with a GUI component, the interaction (event) derives the program to perform a task). Event: click button, type in text field, close window, etc. Event handler: The code that performs a task in response to an event. Event handling: The process of responding to events.

Top level classes: classes not declared inside another classes. Nested classes: classes declared inside another classes. Non-static 1. Requires an object of top-level class. 2. Has a reference to top-level object. static 1. Does not require. 2. Has no reference. Inner class: can access data + methods of top-level.

inner class object Event handler (listener) [must be registered] action If event occurs

JPasswordField extends JTextField JTextField extends JTextComponent JTextComponent JTextField extends JPasswordField extends JPasswordField shows typed characters as hidden with an echo character. seteditable is inherited by JTextField from JTextComponent. JTextComponent is in javax.swing.text

object TextField Event handler (listener) [must be registered] listens If event occurs class implements ActionEvent (interface) ActionListener calls automatically ActionListener (method)

Pressing Enter ( ) generates ActionEvent. This event is processed by an object that implements the interface ActionListener. Steps required to setup event handling for GUI component: 1. Create a GUI component (e.g. TextField, Label, Button) 2. Create a class that represent the event handler. In this class, implement an appropriate event-listener interface. 3. Register the event handler. i.e. notify the handler when event occurs.

import import import import import import import java.awt.flowlayout; java.awt.event.actionevent; java.awt.event.actionlistener; javax.swing.jframe; javax.swing.joptionpane; javax.swing.jpasswordfield; javax.swing.jtextfield; public class TextFieldFrame extends JFrame { private JTextField textfield1; private JTextField textfield2; private JTextField textfield3; private JPasswordField passwordfield; public TextFieldFrame() { super("testing JTextField and JPasswordField"); setlayout(new FlowLayout()); First Program TextFieldFrame.java

textfield1 = new JTextField(10); add(textfield1); textfield2 = new JTextField("Enter text here"); add(textfield2); textfield3 = new JTextField("Uneditable text field", 21); textfield3.seteditable(false); add(textfield3); passwordfield = new JPasswordField("Hidden text"); add(passwordfield); TextFieldHandler handler = new TextFieldHandler(); textfield1.addactionlistener(handler); textfield2.addactionlistener(handler); textfield3.addactionlistener(handler); passwordfield.addactionlistener(handler); } // end constructor Step 1: Create GUI component Step 3: Register the event handler

private class TextFieldHandler implements ActionListener { public void actionperformed (ActionEvent event) { String string = ""; if (event.getsource()==textfield1) string = String.format("textField1: %s", event.getactioncommand()); else if (event.getsource()==textfield2) string = String.format("textField2: %s", event.getactioncommand()); else if (event.getsource()==textfield3) string = String.format("textField3: %s", event.getactioncommand()); else if (event.getsource()==passwordfield) string = String.format("passwordField: %s", new String (passwordfield.getpassword())); JOptionPane.showMessageDialog(null, string); } } // end inner class } // end TextFieldFrame class Step 2: Create an inner class that represent the event handler

import javax.swing.jframe; Second Program TextFieldTest.java public class TextFieldTest { public static void main(string[] args) { TextFieldFrame textfieldframe = new TextFieldFrame(); textfieldframe.setdefaultcloseoperation(jframe.exit_on_close); textfieldframe.setsize(450, 200); textfieldframe.setvisible(true); } }

Output

The information about any GUI event that occurs is stored in an object of a class such as ActionEvent, ItemEvent, TextEvent, KeyEvent, MouseEvent, ListSelectionEvent, ComponentEvent, WindowEvent, ContainerEvent, etc. Example: (10 )هذا الحدث موجود في البرنامج السابق في شريحة Public void actionperformed (ActionEvent event) {. }

Each component generates specific event object. TextField generates ActionEvent object هذا الحدث تم التعامل معه في Button generates ActionEvent object )10 المثال السابق (شريحة TextComponent generates TextEvent object JCheckBox generates ItemEvent object JRadioButton generates ItemEvent object JComboBox generates ItemEvent object JList generates ListSelectionEvent object هذا الحدث سوف نتعامل معه )22 في المثال الاحق (شريحة Mouse generates MouseEvent object Key generates KeyEvent object

For each event-object type, there is a corresponding event-listener interface. Ex: ActionEvent has ActionListener interface Some event-listener classes: ActionListener, ItemListener, KeyListener, MouseListener, ListSelectionListener, etc.

A handler class is any class that implements a listener interface that is appropriate for the type of event object generated by the component. Component generates event object when a listener object is registered with the component. The method of the component that accomplishes this is the addxxxlistener method. The xxx represents the name of the specific listener. Example: textfield1 generates ActionEvent which implements ActionListener. Therefore, textfield1.addactionlistener(handler);

The listener interface specifies the methods by which the event object can be handled. The class must implement the code for these methods. The following table shows the listener interfaces that can be implemented and their methods: Category Interface Name Methods Action ActionListener actionperformed( ActionEvent ) Item ItemListener itemstatechanged( ItemEvent ) Text TextListener textvaluechanged( TextEvent ) List ListSelectionListener valuechanged( ListSelectionEvent )

Category Interface Name Methods Key KeyListener keypressed( KeyEvent ) keyreleased( KeyEvent ) keytyped( KeyEvent ) Mouse MouseListener mousepressed( MouseEvent ) mousereleased( MouseEvent ) mouseentered( MouseEvent ) mouseexited( MouseEvent ) mouseclicked( MouseEvent ) Mouse Motion MouseMotionListener mousedragged( MouseEvent ) mousemoved( MouseEvent ) Window WindowListener windowclosing( WindowEvent ) windowopened( WindowEvent ) windowiconified( WindowEvent ) windowdeiconified( WindowEvent ) windowclosed( WindowEvent ) windowactivated( WindowEvent ) windowdeactivated( WindowEvent )

ComboBox (drop-down list) provides a list of items. ComboBox is implemented with class JComboBox JComboBox generates ItemEvent event. Show the program. Each item added to the list has an index (0, 1, 2, ). setmaximumrowcount sets the max. no. of elements that are displayed when the user clicks the JComboBox.

Anonymous inner class is a special form of inner class that is declared without a name and appears inside a method declaration. Declare event listener class, create an object of that class, and register that object as the listener for the JComboBox object ItemEvent. The declaration of an anonymous inner class, in this example, begins after ItemListener() which implements interface ItemListener.

import import import import javax.swing.*; java.awt.*; java.awt.event.itemevent; java.awt.event.itemlistener; First Program AnonymousInnerClass_ComboBox.java public class AnonymousInnerClass_ComboBox extends JFrame { private JComboBox imagesjcombobox; private JLabel label; private String names[]={"birds.gif","cat.gif","glassfish.gif","turtle.gif"}; private Icon icons[] = {new ImageIcon(getClass().getResource(names[0])), new ImageIcon(getClass().getResource(names[1])), new ImageIcon(getClass().getResource(names[2])), new ImageIcon(getClass().getResource(names[3]))}; public AnonymousInnerClass_ComboBox() { super("using Anonymous Inner Class with JComboBox"); setlayout(new FlowLayout());

imagesjcombobox = new JComboBox(names); imagesjcombobox.setmaximumrowcount(3); add(imagesjcombobox); label = new JLabel(icons[0]); add(label); imagesjcombobox.additemlistener( new ItemListener() { public void itemstatechanged(itemevent e) { if(e.getstatechange()==itemevent.selected) label.seticon(icons[imagesjcombobox.getselectedindex()]); } } ); } // end constructor } // end class Anonymous inner class

Second Program AnonymousInnerClass_ComboBox_Test.java import javax.swing.jframe; public class AnonymousInnerClass_ComboBox_Test { public static void main( String[] args) { AnonymousInnerClass_ComboBox combobox = new AnonymousInnerClass_ComboBox(); combobox.setdefaultcloseoperation(jframe.exit_on_close); combobox.setextendedstate(jframe.maximized_both); combobox.setvisible(true); } }

Develop a GUI application to place three buttons on a frame. Use event handling that is when clicking the first button the background of frame changes to red color and when clicking the second button the background of frame changes to blue color and when clicking the third button the background of frame changes to green color.

Develop a GUI application that contains three programs (i.e. three classes). The first class is used to allow the user input the correct password. When the user enters the correct password, the password frame closes and the frame of the second class opens. The second class is used to connect a database and manipulate transactions. The third class contains main method to test the other classes.