Constructors, GUI’s(Using Swing) and ActionListner

Slides:



Advertisements
Similar presentations
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.
Advertisements

Java Tutorial – Building GUIs Java with Added Swing Daniel Bryant
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Cosc 4755 Phone programming: GUI Concepts & Threads.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
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 
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Intro to Java 2 By Geb Thomas Based on the Java TutorialJava Tutorial.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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 GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
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,
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
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.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Concurrent Programming and Threads Threads Blocking a User Interface.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
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.
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.
UID – Event Handling and Listeners Boriana Koleva
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Sadegh Aliakbary Sharif University of Technology Fall 2011.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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,
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
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)
Object Oriented Programming in Java Habib Rostami Lecture 10.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
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:
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.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
GUI building with the AWT
CompSci 230 S Programming Techniques
3 Introduction to Classes and Objects.
CHAPTER Reacting to the user.
Web Design & Development Lecture 11
A First Look at GUI Applications
“Form Ever Follows Function” Louis Henri Sullivan
Chapter Topics 15.1 Graphical User Interfaces
A Quick Java Swing Tutorial
Programming in Java Event Handling
Ellen Walker Hiram College
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Introduction to Event Handling
JAVA Constructors.
A Quick Java Swing Tutorial
Advanced Programming in Java
Java Tutorial – Application Building
Tonga Institute of Higher Education
Chapter 5 Processing Input with Applets
Presentation transcript:

Constructors, GUI’s(Using Swing) and ActionListner

Constructors A block of code similar to a method that’s called when an instance of an object is created. Here are the key differences between a constructor and a method: A constructor doesn’t have a return type. The name of the constructor must be the same as the name of the class. Unlike methods, constructors are not considered members of a class. A constructor is called automatically when a new instance of an object is created.

Constructor has same name as the class and looks like this in a java code: public class MyClass{ //This is the constructor MyClass(){ } ..statements..

Constructor Example Program In the program below we have created an object “obj” of class Hello and then we have displayed the instance variable name of the object. this keyword Refers to the current object, object obj in this example. public class Hello { String name; //Constructor Hello(){ this.name = “Constructors are easy"; } public static void main(String[] args) { Hello obj = new Hello(); System.out.println(obj.name); } new keyword Creates the object of the Hello class and invokes the constructor to initialize it

Default Constructor Every class has a constructor If you don’t set one up Java will do it for you at run time You will not find it in your source code(the java file) It would be inserted into the code during compilation and exists in .class file. //Constructor inserted public class Hello { public static void main(String[] args) { Hello obj = new Hello(); } public class Hello { Hello(){ } public static void main(String[] args) { Hello obj = new Hello(); Compiler

Difference between Constructor and Method The purpose of constructor is to initialize the object of a class while the purpose of a method is to perform a task by executing java code. Constructors cannot be abstract (without braces, and followed by a semicolon), final, static while methods can be. Constructors do not have return types while methods do. Remember the name of the constructor must be the same as the name of the class

Creating GUI’s in Java – Swing Package Java Swing is a lightweight Graphical User Interface (GUI) toolkit that includes a rich set of widgets. It includes a package which lets you make GUI components for your Java applications, and it is platform independent.

Java Swing class Hierarchy Diagram What is a container class? Container classes are classes that can have other components on it. Panel: The sole purpose of a Panel is to organize the components on to a window. Frame: A fully functioning window with its title and icons. Dialog: Like a pop-up window, displays a message. Not a fully functioning window like the Frame. More info on the Swing package on Oracle’s website: https://docs.oracle.com/javase/ 7/docs/api/javax/swing/package- summary.html All components in swing are JComponent which can be added to container classes.

Action Listener You implement an action listener to define what should be done when an user performs certain operation. An action event occurs, whenever an action is performed by the user. Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field. The result is that an actionPerformed message is sent to all action listeners that are registered on the relevant component.

To write an Action Listener, follow the steps given below: Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface. For example: public class MyClass implements ActionListener { Register an instance of the event handler class as a listener on one or more components. For example: someComponent.addActionListener(instanceOfMyClass); Include code that implements the methods in listener interface. For example: public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }