1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.

Slides:



Advertisements
Similar presentations
Algorithmic Programming II
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Things to mention public static void main(String [] args) imports comments –block comments /* … */ –single-line comments // –javadoc comments and tags.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
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.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
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.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
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.
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,
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
GUI programming Graphical user interface-based programming.
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,
CS Lecture 01 Frames and Components and events Lynda Thomas
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
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.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Creating a GUI Class An example of class design using inheritance and interfaces.
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,
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.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Graphical user interface-based programming
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

2 GUIs  Most applications have graphical user interfaces to respond to user's

3 A Few Graphical Components  A Graphical User Interface (GUI) presents a graphical view of an application to users  To build a GUI application — Begin with a well-tested model like GameTree, MineSweeper, GameOfLife, LinkedPriorityList — Make graphical components visible to the user — Ensure the correct things happen for each event user clicks button, moves mouse, presses enter key,...  First some Java's graphical components — Windows, buttons, text fields, and text areas

4 Classes in the swing package  The javax.swing package has components that show in a graphical manner JFrame : window with title, border, menu, buttons JButton : A component that can "clicked" JLabel : A display area for a small amount of text JTextField : Allows editing of a single line of text

5 Get a "window" to show itself  Have a class extend javax.swing.JFrame — Your new class inherits all the methods and instance variables for putting a window on the computer  Add a main method to call the constructor  Layout Set up the GUI in the constructor — See program on the next slide

6 Can you see the "window"? import javax.swing.*; public class FirstGUI extends JFrame { public static void main(String[] args) { JFrame aWindow = new FirstGUI(); // Tell the JFrame object to show itself aWindow.setVisible(true); } public FirstGUI() { layoutGUI(); // The first of three things to do } private void layoutGUI() { setTitle("Graffiti"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

7 Some JFrame messages  Set the size and location of the window with setSize(200, 150); // 200 pixels wide, 150 pixels tall setLocation(75, 40); // left 75, down 40

8 Building Graphical Components  Add these three graphical components to the "content pane" of the JFrame private JLabel aLabel = new JLabel("Change with setText(String)"); private JTextArea textEditor = new JTextArea("You can edit this text "); private JButton clickMeButton = new JButton("Nobody is listening to me");

9 Using Null Layout  By default, JFrame objects have only five places where you can add components a 2nd add wipes out the 1 st  There are many layout strategies, we'll use null setLayout(null);  Each component added needs it size and location set

10 Add components to a window  Add the previously constructed components to one of the five areas of a JFrame // Add components aLabel.setSize(180, 30); aLabel.setLocation(5, 0); add(aLabel); textEditor.setSize(180, 60); textEditor.setLocation(5, 35); add(textEditor); clickMeButton.setSize(180, 30); clickMeButton.setLocation(5, 95); add(clickMeButton);

11 Other useful methods  String getText get the text stored in components JButton JTextArea TextField  setText(String) changes the text stored in JButton JTextArea TextField  Two methods to wrap at the word level in a JTextArea default is NO wrapping textEditor.setLineWrap(true); textEditor.setWrapStyleWord(true);

12 In Class  Complete code on handout to get this layout  First volunteer done, type it in | | | | x pixel

13 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field, move the mouse, press a key — And NOTHING happens  So let’s make something happen …

14 Java's Event Model  Java can let the operating system notify graphical components of user interaction — Button objects are notified when clicked — JTextField objects with focus know when the user presses the enter key — A menu item can know that a user selected it  Event driven programs respond to many things mouse clicks, mouse movements clicks on hyperlinks, buttons, menu items Users pressing any key, selecting a list item

15 Example: Action Events  The buttons, text fields, and menu items do not perform the actions — Instead JButton, JTextField, JMenuItem objects send actionPerformed messages to other objects  The code that responds to events is often located in actionPerformed methods  We need to have a class that implements the ActionListener interface to guarantee the object has an actionPerformed message

16 Event Driven Program with GUIs  Key elements of an event-driven GUI — Graphical components The screen elements that a user manipulates with the mouse and keyboard JFrame JLabel JButton JScrollbar JMenuItem JTextField JTextArea Jlist... — Layout managers Govern how the components appear on the screen Examples FlowLayout GridLayout SpringLayout — Events Signals the OS that a user interacted with a GUI component Examples: mouse clicks, keys pressed, hyperlinks selected

17 Java's Event Model JFrame JButton 1 Layout Graphical Components JFrame JButton JTextField JMenuItem Listener 3 You register objects that waits for messages from graphical components addActionListener Listener 4 Users interact with these graphical components 2 You write classes that implement the correct interface ActionListener

18 No one is "Listening"  Okay, now we have a GUI — but when run, nothing happens  Wanted: An object to listen to the button that understands a specific message such as — actionPerformed  Also need to tell the button who it can send the actionPerfomed message to — Register the listener with this method addActionListener(ActionListener al)

19 Handling Events  Add a private inner class that can listen to the event that the component generates Your class must implement a listener interface to guarantee that it has the expected methods: First up: ActionListener  In the constructor register an instance of the listener so the component can later send messages (we do not see this) to the listener’s actionPerformed method events occur anytime in the future--the listener is listening (waiting for user generated events such as clicking a button or entering text into a text field)

20 Inner class  Add an inner class — inner classes have access to the enclosing classes' instance variables — make it private since no one else needs to know about it — otherwise you need a separate class that gets the graphical components passed as an argument

21 Have a class that implements ActionListener // Inner class to listen to events private class ButtonListener implements ActionListener { // clickMeButton will call this method once registered (below) public void actionPerformed(ActionEvent anActionEvent) { JOptionPane.showMessageDialog(null, textEditor.getText()); } // In the constructor of the class that extends JFrame, // register the instance of the listener so the component // can later send messages to that object ButtonListener aListener = new ButtonListener(); clickMeButton.addActionListener(aListener); Caution: This is easy to forget. It is not a compile time error