A First Look at GUI Applications

Slides:



Advertisements
Similar presentations
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Advertisements

Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
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.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Advanced Java and Android Programming Day 1: Introduction to GUI Programming Intro to GUI Programming1.
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 
Java Programming Chapter 10 Graphical User Interfaces.
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.
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,
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
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.
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.
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.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
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.
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Review_6 AWT, Swing, ActionListener, and Graphics.
GUI Components. The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
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,
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
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.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
Graphical User Interfaces
CompSci 230 S Programming Techniques
A First Look at GUI Applications Radio Buttons and Check Boxes
Chapter Topics 15.1 Graphical User Interfaces
Java GUI.
Processing Timer Events
Graphical User Interface (pronounced "gooey")
Java Programming: From Problem Analysis to Program Design,
Ellen Walker Hiram College
Event-driven programming for GUI
Timer class and inner classes
Advanced Programming in Java
Chapter 15: GUI Applications & Event-Driven Programming
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

A First Look at GUI Applications Chapter 7 A First Look at GUI Applications

Contents Introduction to GUI Applications Creating Windows

I. Introduction to GUI Applications What is GUI? Common GUI Components JFC, AWT and Swing Event-Driven Programming

I.1 What is GUI ? Java GUI Application A Java application with a Graphical User Interface (GUI) A GUI is a graphical window or a system of graphical windows GUI is presented by an application for interaction with users. GUI typically accepts input from the keyboard and a mouse as well.

I. 2 Common GUI Components A window in a GUI commonly consists of several components that present data to the user and/or allow interaction with the application. Some of common GUI components are: Button Causes an action to occur when it is clicked Label An area that can display text Text field An area in which the user may type a single line of input from the keyboard

I. 2 Common GUI Components Check box A component that has a box that may be checked or unchecked. List A list from which the user may select an item. Radio button A control that can be either selected or deselected. Slider A control that allows the user to select a value by moving a slider along a track.

I. 2 Common GUI Components Combo box A component that displays a drop-down list of items from which the user may select. A combo box also provides a text field in which the user may type input.

I. 3 The JFC, AWT and Swing The Java Foundation Classes (JFC) is used to create GUI applications. The JFC consists of several sets of classes: Abstract Windowing Toolkit (AWT) Swing

I. 3 The JFC, AWT and Swing AWT AWT allows programmers to create applications and applets that interact with the user via windows and other GUI components. AWT classes do not draw user interface components on the screen. AWT classes communicate with another layer classes, known as the peer classes. Peer classes direct the operating system to its own built-in components.

I. 3 The JFC, AWT and Swing AWT has some drawbacks Not all operating systems offer the same set of GUI components A component on one operating system might have slightly different behavior than the same component on a different operating system. Programmers cannot easily customize the AWT components because they rely on the appearance and behavior of the operating system. AWT classes are part of the java.awt package. import java.awt.*;

I. 3 The JFC, AWT and Swing Swing (introduced with release of Java 2) A library of classes that do not replace the AWT Provide an improved alternative for creating GUI applications and applets. Very few of the Swing classes rely on operating system of peer classes. Swing draws almost of its own components on the screen. The Swing classes are part of the javax.swing package. import javax.swing.*;

I.4 Event-Driven Programming Program that operate in a GUI environment must be event-driven. If there is an event, a specific method is then called to process the event. An event is an action that takes place within a program, such as The clicking of a button A mouse move ....

I.4 Event-Driven Programming An event listener is an object that automatically executes one of its methods when a specific event occurs. If you want to perform an operation when a specific event occurs, you must create an event listener object that responds when that event takes place.

II. Creating Windows Windows, Containers, Frames Using Inheritance to Extend the JFrame Class Adding Components to a Window Handling Events with Action Listener

II.1 Windows, Containers, Frames A window with various components allows the user to enter and/or select data and interact with the application. A window contains other components. It is more approximately considered a container. A container is simply a component that holds other components. A container that can be displayed as a window is known as a frame.

II.1 Windows, Containers, Frames A frame appears as a basic window that has A border A title bar A set of buttons for minimizing, maximizing, and closing the window. In a Swing application, a frame object is created from the JFrame class.

II.1 Windows, Containers, Frames There are a number of steps involved in creating and showing a window, for example: Create a window Create an object of the JFrame class. Set the title of the window Set the size of the window Specify what happens when the close button is clicked. Display the window

Display a Window

II.2 Using Inheritance to Extend the JFrame Class In order to add numerous components and capabilities to the windows, you should use inheritance to extend the JFrame class. When a class extends an existing class, it inherits many of the existing class's members. If you create a new class that extends the JFrame class, the new class will automatically inherit methods of JFrame class, such as setTitle setSize setDefaultCloseOperation setVisible

II.2 Using Inheritance to Extend the JFrame Class UML class diagram - Inheritance JFrame SimpleWindow

II.2 Using Inheritance to Extend the JFrame Class

II.2 Using Inheritance to Extend the JFrame Class

II. 3 Adding Components to a Window Swing provides numerous GUI components that can be added to a window. Three fundamental components are the label, the text field, and the button. Component Swing Class Description Label JLabel An area that can display text Text field JTextField An area in which the user may type a single line of input from the keyboard

The Kilometer Converter This application will present a window in which the user will be able to enter a distance in kilometers, and then click a button to see that distance converted to miles. The conversion formula Miles = Kilometers x 0.6214

The Kilometer Converter When designing a GUI application, it is usually helpful to draw a sketch showing the window. Window Title Kilometer Converter x Label Enter a distance in kilometers Text Field Calculate Button

Content Panes and Panels A content pane is a container that is a part of every JFrame object. We cannot see the content pane. The content pane does not have a border. Any component that is to be displayed in a JFrame object must be added to its content pane. Kilometer Converter x Content Pane

Content Panes and Panels A panel is a container that can hold GUI components. Panels cannot be displayed by themselves. Panels are used to hold and organize collections of related components. With Swing, you can create panels with JPanel class. JPanel Object Enter a distance in kilometers Calculate

The Kilometer Converter In the Kilometer Converter application: Create a panel to hold the label, text field, and button. Add the panel to the JFrame object's content pane. Kilometer Converter x Content Pane JPanel Object Enter a distance in kilometers Calculate

The Kilometer Converter UML class diagram JFrame KiloConverterWindow -panel: JPanel -messageLabel: JLabel -kiloTextField: JTextField -calcButton: JButton +KiloConverterWindow(): -buildPanel(): void

The Kilometer Converter

JLabel Component JTextField Component JButton Component

II.4 Handling Events with Action Listeners Writing Event Listener Classes as Private Inner Classes Event Listeners Must Implement an Interface Registering an Event Listener Object Example: Writing an Event Listener for KiloConverterWindow Class Example: Background and Foreground Colors The ActionEvent Object

II.4 Handling Events with Action Listeners An event is an action that takes place within a program, such as The clicking of a button The mouse move, ... When an event takes place, the component that is responsible for the event creates an event object in memory. The event object contains information about the event. The component that generated the event object is known as the event source.

II.4 Handling Events with Action Listeners For example, when user clicks a button, the JButton component generates an event object. The JButton component that generated the event object is the event source. The event source component is connected to one or event listeners. An event listener is an object that responds to event. The event object is automatically passed, as an argument, to a specific method in the event listener. The method then performs any actions that it was programmed to perform in response to the event.

II.4.a Writing Event Listener Classes as Private Inner Class A class that is defined inside of another class is known as an inner class. public class Outer { Fields and methods of the Outer class private class Inner { Fields and methods of the Inner class } }

II.4.a Writing Event Listener Classes as Private Inner Class When an inner class is private, it is accessible only to code in the class that contains it. The Inner class would be accessible to methods that belongs to the Outer class. The code outside the Outer class would not be able to access the Inner class. A common technique for writing an event listener class is to write it as a private inner class, inside the class that creates the GUI.

II.4.b Event Listeners Must Implement an Interface There is a special requirement that all event listener classes must meet: They must implement an interface. An interface is as something like a class: Contains one or more method headers. Interfaces do not have actual methods, however, only their headers. When a class implements an interface, the class will have all of the methods that are specified in the interface.

II.4.b Event Listeners Must Implement an Interface Java provides numerous interfaces to use with event listener class. There are several different types of events that can occur within a GUI application, and the specific interface that we use depends on the type of event we want to handle. JButton components generate action events, and an event listener class that can handle action events is also known as an action listener class. An action listener class for a JButton component must implement an interface known as ActionListener.

II.4.b Event Listeners Must Implement an Interface The ActionListener interface public interface ActionListener { public void actionPerformed(ActionEvent e); } The ActionListener interface, as well as other event listener interfaces, is in the java.awt.event package. import java.awt.event.*;

II.4.b Event Listeners Must Implement an Interface Here is an example of a class named MyButtonListener that implements the ActionListener interface: private class MyButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Write code here to handle the event. } } When we write a class the implement an interface, we are promising that the class will have the methods specified in the interface.

II.4.c Registering an Event Listener Object Once we have written an event listener class, we can create an object of that class, and then connect the object with a GUI component. The process of connecting an event listener object to a GUI component is known as registering the event listener. 2. The JButton component generates an event object and passes it to the action listener object's actionPerformed method. Event Object Action Listener Object void actionPerformed(ActionEvent e) JButton Component 1. The user clicks on a button. 3. The actionPerformed method executes the statements necessary to handle the event.

II.4.d Writing an Event Listener for KiloConverterWindow Class

II.4.e Background and Foreground Colors Many of the Swing component classes have methods named setBackground and setForeground. The backdground color is the color of the component itself. The foreground color is the color of the text that might be displayed on the component. The argument of these methods is a color code: Color.black Color.blue Color.cyan Color.darkgray Color.gray Color.green Color.lightGray Color.megenta Color.orange Color.pink Color.red Color.white Color.yellow

II.4.e Background and Foreground Colors

II.4.e Background and Foreground Colors Changing the Background Color of a JFrame Object's Content Pane Get the content pane Change the background color of the content pane. getContentPane().setBackground(Color.blue);

II.4.f The ActionEvent Object The ActionListener interface public interface ActionListener { public void actionPerformed(ActionEvent e); } When the actionPerformed method executes, the e parameter references the event object that was generated in response to the event. Event object contains information about the event. ActionEvent methods GetActionCommand(): Returns the action command for this event as a String GetSource(): Returns a reference to the object that generated this event.

The getActionCommand Method When a JButton component generates an event, the action command is the text that appears on the button. The getActionCommand method returns this text as a String. We can use the getActionCommand method to determine which button was clicked when several buttons share the same action listener class.

The getActionCommand Method This example produces a window with three buttons. The buttons have the text “Button 1”, “Button 2”, and “Button 3”. Three buttons share the same action listener class. The action listener class displays the contents of the event object's action command when any of these buttons are clicked.

The getSource Method We can use getSource method instead of using the getActionCommand method to determine which button was clicked. public void actionPerformed(ActionEvent e) { //Determine which button was clicked if(e.getSource() == button1) JOptionPane.showMessageDialog(null,"You clicked the first button."); else if(e.getSource() == button2) JOptionPane.showMessageDialog(null,"You clicked the second button."); else if(e.getSource() == button3) JOptionPane.showMessageDialog(null,"You clicked the third button."); }

Checkpoint 7.1 What is a frame? How do you create a frame with Swing? 7.2 How do you set a frame's size? 7.3 How do you display a frame on the screen? 7.4 What is a content frame? 7.5 What is the difference between a frame and a panel? 7.6 What is an event listener?

Checkpoint 7.7 If you are writing an event listener class for a JButton component, what interface must the class implement? What method must the class have? When is this method executed? 7.8 How do you register an event listener with a JButton component?