Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.

Slides:



Advertisements
Similar presentations
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Advertisements

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.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
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.
Lecture 19 Graphics User Interfaces (GUIs)
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
Java Programming Chapter 10 Graphical User Interfaces.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
GUI programming Graphical user interface-based programming.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Objectives of This Session
Dale Roberts GUI Programming using Java - Introduction Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 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.
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.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Javax.swing Class JOptionPane Java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.JOptionPane.
A simple swing example GETTING STARTED WITH WIND CHILL.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
Sadegh Aliakbary Sharif University of Technology Fall 2011.
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.
Graphical User Interface (GUI)
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
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.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
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:
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
© Copyright by Pearson Education, Inc. All Rights Reserved. Appendix I GUI Components and Event Handling Android How to Program, 3/e.
GUI Programming using Java - Event Handling
Lecture 15 Basic GUI programming
CSC 205 Programming II Lecture 5 AWT - I.
Lecture 11 Object Oriented Programming Using Java
Welcome To java
Introduction to Graphics
Graphical User Interfaces -- GUIs
Web Design & Development Lecture 11
Appendix I GUI Components and Event Handling
A First Look at GUI Applications
Lecture 8 Object Oriented Programming Using Java
A Quick Java Swing Tutorial
Ellen Walker Hiram College
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Graphical user interface-based programming
IFS410: Advanced Analysis and Design
Introduction to Event Handling
Graphical User Interfaces in Java Event-driven programming
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar

Graphical User Interface A graphical user interface presents a user friendly mechanism for interacting with a program. A GUI pronounced as (GOO-EE) gives a program a distinctive “look” and “feel”.

Introductory Concepts Most swing components are written, manipulated and displayed completely in java. (so called pure java components). Swing components are part of java foundation classes (JFC)-java libraries for cross platform GUI development. Concept of AWT (Abstract Window Toolkit) and the concept of “look and feel” Concept of heavy weight and light weight components. Each heavy weight component has a peer that is responsible for the interations b/w the component and the local platform. Some swing components are heavyweight components, Such as subclass of (Java.awt.Window) e.g JFrame.

Hierarchy of GUI Classes Object Component Container JComponent Class component declares the common attributes and behaviors of derived classes. e.g. paint method A container manages a collection of related components JComponent is the super class Of most swing components. Declares The common attributes and behaviors of all subclasses

JComponent class This class declares the common attributes and behaviors of all subclasses of JCompoent. –A pluggable look and feel –Mnemonics –Common event handling capabilities –Tooltips –Support for assistive technologies –Support for user interface localization

A simple GUI program for JLabel import java.awt.*; import java.awt.Event; import javax.swing.*; public class LabelTesting extends JFrame{ private JLabel label1,label2,label3; public LabelTesting() { super("Firs GUI Program"); Container container = getContentPane(); container.setLayout(new FlowLayout()); label1 = new JLabel("Text on First Label"); label1.setToolTipText("Hi i am label"); container.add(label1);

Icon icon = new ImageIcon("icon1.gif"); label2 = new JLabel("Second Label",icon, SwingConstants.LEFT); label2.setToolTipText("Hi am second label"); container.add(label2); setSize(200,200); setVisible(true); } } Continued….

public class Main { /** Creates a new instance of Main */ public Main() { } /** args the command line arguments */ public static void main(String[] args) { LabelTesting test = new LabelTesting(); } Main Method

Event Handling The interaction of the user with GUI generates events. Three parts of an event handling mechanism –Event Source –Event Object –Event Listener

How event handling works How did the event handler get registered? How does the GUI Component know to the right event handler? Answers Step1:- Event registration for a control. Every JComponent has an object of class EventListenerList (javax.swing.event) called listenerList as an instance variable. References to all registered listeners are stored in listenerList. Consider the statement (textField1.addActionListener(handler))

Listener type is important in handling the respective event. Each event type has a corresponding event listener interface (Key Point) When an event occurs, the GUI Component is handed a unique event id specifying the event type. How event handling works

Practice on Event Handling public class TextFieldsTest extends JFrame implements ActionListener { private JTextField text1,text2; private JPasswordField passwd; String string = ""; public TextFieldsTest() { super("Text Fields Demonstration"); Container container = getContentPane(); container.setLayout(new FlowLayout()); text1 = new JTextField(10); container.add(text1); text2 = new JTextField("Enter Text"); container.add(text2);

passwd = new JPasswordField("Hidden Text"); container.add(passwd); text1.addActionListener(this); text2.addActionListener(this); passwd.addActionListener(this); setSize(350,250); setVisible(true); } Continued…

public void actionPerformed(ActionEvent event){ if (event.getSource() == text1){ string = "Text 1: " + event.getActionCommand(); } else if (event.getSource() == text2){ string = "Text 2: " + event.getActionCommand(); } else if (event.getSource() == passwd){ string = "Password: " + event.getActionCommand(); } JOptionPane.showMessageDialog(null,string); }

public static void main(String[] args) { TextFieldsTest test = new TextFieldsTest(); } Main Class