241-211 OOP (Java): GUI Intro/11 1 241-211. OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2, 2013-2014 11.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces (Part IV)
Advertisements

Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Lecture 19 Graphics User Interfaces (GUIs)
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling Objects First with Java - A Practical.
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.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
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.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Java Programming Chapter 10 Graphical User Interfaces.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
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 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
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.
OOP (Java): GUI II/ OOP Objectives – –describe some more GUI features: JPanel, and mouse listeners/adapters Semester 2,
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Concurrent Programming and Threads Threads Blocking a User Interface.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
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.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces 2.0.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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)
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
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.
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:
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling © 2017 Pearson Education, Inc. Hoboken,
©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.
Lecture 15 Basic GUI programming
CSC 205 Programming II Lecture 5 AWT - I.
Welcome To java
A First Look at GUI Applications
A Quick Java Swing Tutorial
Building Graphical User Interfaces
COS 260 DAY 23 Tony Gauvin.
1 Graphical User Interfaces
A Quick Java Swing Tutorial
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2, Introducing Java's GUI Features

OOP (Java): GUI Intro/11 2 Topics 1. GUI Principles 2. Background 3. Three Steps to GUIs 4. An Applicaton Window 5. Adding Menus 6. Event Handling 7. ImageViewer as a Listener continued

OOP (Java): GUI Intro/ An Inner Class 9. ImageViewer with an Inner Listener 10. An Anonymous (Inner) Class 11. ImageViewer with Anonymous Listeners 12. Listener Implementation Summary

OOP (Java): GUI Intro/ GUI Principles listeners (waiting code) events (info. objects) components layout manager

OOP (Java): GUI Intro/ GUI Principles GUIs are built from components – –buttons, menus, sliders, etc. The positioning of GUI components in a window is done with layout managers User actions are converted into events – –button presses, menu selections, etc. Events are processed by listeners.

OOP (Java): GUI Intro/ Background GUI interfaces should be written with Swing GUI components Swing includes buttons, text fields, scrolling windows, editor windows, tree displays, etc – –I'll describe some of them

OOP (Java): GUI Intro/ Swing and AWT In older Java programs, the AWT (Abstract Window Toolkit) GUI components are used – –Swing has replaced AWT GUIs – –never mix Swing and AWT components in a single program Some parts of AWT are still used – –e.g. its layout managers (see later) continued

OOP (Java): GUI Intro/11 8 use Swing

OOP (Java): GUI Intro/11 9 Swing supports lightweight GUI components – –they are drawn onto the screen in an area controlled by Java – –the GUI is implemented completely within the JVM – –advantage: the 'look' of GUI components can be controlled by Java continued Lightweight and Heavyweight

OOP (Java): GUI Intro/11 10 AWT supports heavyweight GUI components – –each Java GUI component is actually a simple layer hiding the OSes GUI component the OS component is called a peer component – –e.g. on Windows, a Java button in AWT is implemented using a Windows' button continued

OOP (Java): GUI Intro/ Disadvantages of AWT Java does not have complete control over the AWT GUI components – –some AWT GUIs do not work well because of problems with the underlying OS (e.g. file choosing in Windows) The "look and feel" of Java GUIs in AWT vary between OSes.

OOP (Java): GUI Intro/ JFC Swing is part of the Java Foundation Classes (JFC) – –a collection of GUI-related classes to solve the problems with AWT – –there are over 300 classes in JFC!! JFC also includes: – –pluggable "look and feel", an accessibility API, Java 2D, drag-and-drop, multiple undo's

OOP (Java): GUI Intro/11 13 Some Java"Look and Feel"s

OOP (Java): GUI Intro/11 14 Using the Substance L&F add-on library (

OOP (Java): GUI Intro/ Three Steps to GUIs There are three main steps to creating a GUI for a Java program: – –1. Declare the GUI components; – –2. Implement the listeners for the components; – –3. Position the controls on the screen by using layout managers (and containers).

OOP (Java): GUI Intro/ An Application Window title content pane window controls Implemented by subclassing JFrame

OOP (Java): GUI Intro/11 17 Coding an Application public class ImageViewer extends JFrame { public ImageViewer() { super("ImageViewer 0.1"); // create the GUI Container c = getContentPane(); JLabel label = new JLabel("I am a label. I can display some text."); c.add(label); : JLabel is a GUI component Version 0.1

OOP (Java): GUI Intro/11 18 // set close behaviour for JFrame as exit // set close behaviour for JFrame as exit setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 200); setSize(300, 200); // pack(); // reduce size to fit GUI components // pack(); // reduce size to fit GUI components setVisible(true); setVisible(true); } // end of ImageViewer() } // end of ImageViewer() // // public static void main(String[] args) public static void main(String[] args) { new ImageViewer(); } { new ImageViewer(); } } // end of ImageViewer class A GUI object is not deleted at the end of main(). We must call exit.

OOP (Java): GUI Intro/11 19 Sizing the Application Replace setSize() by pack(): Replace setSize() by pack():

OOP (Java): GUI Intro/ Adding Menus JMenuBar – –contains the menus JMenu – –contains the menu items JMenuItem – –individual items in a menu

OOP (Java): GUI Intro/11 21 Add Component to JFrame public ImageViewer() { super("ImageViewer 0.2"); // create the GUI makeMenuBar(); Container c = getContentPane(); JLabel label = new JLabel("I am a label. I can display some text."); c.add(label); : // the same as before } // end of ImageViewer() Version 0.2

OOP (Java): GUI Intro/11 22 private void makeMenuBar() // Create the main frame's menu bar. { JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); // add to 'menu area' of JFrame // create the File menu JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); JMenuItem openItem = new JMenuItem("Open"); fileMenu.add(openItem); JMenuItem quitItem = new JMenuItem("Quit"); fileMenu.add(quitItem); } // end of makeMenuBar()

OOP (Java): GUI Intro/ Event Handlers An event handler is a method that is called automatically when an event occurs in a GUI component. Examples of events include: – –typing return in a text field – –pressing a button – –selecting a menu item continued

OOP (Java): GUI Intro/11 24 Event handlers (methods) for different events are predefined by Java inside listeners. A listener is an interface – –it defines the interfaces of its event handler methods (i.e. the names, types of arguments) – –the programmer must implement each method to respond to the event that triggers it continued

OOP (Java): GUI Intro/ Event Handler as a Diagram Program on-screen GUI Program Code data methods class that implements the listener interface event event handler method A typical action is for the event handler method to update the data back in the program.

OOP (Java): GUI Intro/ Using Event Handlers The programmer must: – –1. Implement the listener, by coding its event handler methods – –2. 'Link' the GUI components in their program with the implemented listener The Java runtime system will automatically pass events to the handlers for processing.

OOP (Java): GUI Intro/ Components and Events There are manyListener interfaces that can handle events from different GUI components. I'll look at: – –ActionListener – –ItemListener – –MouseListener – –MouseMotionListener

OOP (Java): GUI Intro/11 28 ActionListener It deals with events from: – –JButton,JMenu, JMenuItem, JRadioButton, JCheckBox when a component is pressed/selected – –JTextField when enter is typed The interface has one method: public void actionPerformed(ActionEvent e)

OOP (Java): GUI Intro/11 29 Using the Listener The GUI component must be 'linked' to code which implements the method in the listener. item GUI Window the 'link' which sends an event e public class Foo implements ActionListener { public void actionPerformed( ActionEvent e ) { // do something with e System.out.println("Ouch"); } }

OOP (Java): GUI Intro/11 30 Centralized Event Processing We write a single listener to handle all the events triggered in the program – –implements the ActionListener interface – –defines an actionPerformed() method We must register the listener with each component – –component.addActionListener(listener)

OOP (Java): GUI Intro/ ImageViewer as a Listener public class ImageViewer extends JFrame implements ActionListener { private JMenuItem openItem, quitItem; // GUI components which use listeners public ImageViewer() { // just as before } : public void actionPerformed(ActionEvent event) { // the event handler code } } // end of ImageViewer class Version 0.3

OOP (Java): GUI Intro/11 32 Registering the Listener private void makeMenuBar() { JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); openItem = new JMenuItem("Open"); // global var openItem.addActionListener(this); fileMenu.add(openItem); quitItem = new JMenuItem("Quit"); // global var quitItem.addActionListener(this); fileMenu.add(quitItem); } // end of makeMenuBar()

OOP (Java): GUI Intro/11 33 Implementing the Listener public void actionPerformed(ActionEvent event) // Receive event, and do something { Object src = event.getSource(); if (src == openItem) openFile(); else if (src == quitItem) System.exit(0); else System.out.println("Cannot process action event for " + event.getActionCommand()); } // end of actionPerformed()

OOP (Java): GUI Intro/11 34 private void openFile() // open a Swing file chooser to select a // new image file { // test output, until we do this properly System.out.println("open file"); } // end of openFile()

OOP (Java): GUI Intro/11 35 Class Diagram multiple inheritance, using an interface

OOP (Java): GUI Intro/11 36 Execution

OOP (Java): GUI Intro/11 37 ImageViewer as a Diagram GUI data methods the 'link' which sends an event e Open Quit actionPerformed(...) {...}

OOP (Java): GUI Intro/11 38 actionPerformed() The method must decide what action to do, so it must be able to refer to the GUI components – –the components must be declared as global The decision code will be a long series of if-tests, to decide which action to carry out.

OOP (Java): GUI Intro/11 39 Single Listener Features Works well when there are only a few components. When there are many components, the listener soon gets very big – –many if-tests, lots of global variables

OOP (Java): GUI Intro/11 40 Other Ways of Implementing Listeners There are two other ways of implementing a listener – –as an inner class, inside the GUI class – –as a series of anonymous inner classes, one for each component

OOP (Java): GUI Intro/ An Inner Class An inner class is defined inside another class: public class Enclosing { // fields, methods class Inner { // fields, methods } // end of Inner class } // end of Enclosing class

OOP (Java): GUI Intro/11 42 Inner Class Objects Objects created from an inner class only exist within the enclosing class. Inner class objects can use the global fields of the enclosing class – –useful for implementing listeners

OOP (Java): GUI Intro/ ImageViewer with an Inner Listener public class ImageViewer extends JFrame { // no implements private JMenuItem openItem, quitItem; // GUI components with actions public ImageViewer() { // just as before } // no actionPerformed() method : Version 0.4

OOP (Java): GUI Intro/11 44 private void makeMenuBar() { JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); MenuHandler mh = new MenuHandler(); openItem = new JMenuItem("Open"); // global var openItem.addActionListener( mh ); fileMenu.add(openItem); quitItem = new JMenuItem("Quit"); // global var quitItem.addActionListener( mh ); fileMenu.add(quitItem); } // end of makeMenuBar() use same inner class object

OOP (Java): GUI Intro/11 45 class MenuHandler implements ActionListener { public void actionPerformed(ActionEvent event) // Receive notification of an action { Object src = event.getSource(); if (src == openItem) openFile(); else if (src == quitItem) System.exit(0); else System.out.println("Cannot process action event for " + event.getActionCommand()); } // end of actionPerformed() : still inside ImageViewer class globals in ImageViewer

OOP (Java): GUI Intro/11 46 private void openFile() // open a Swing file chooser to select file { // test output, until we do this properly System.out.println("open file"); } // end of openFile() } // end of MenuHandler inner class // public static void main(String[] args) { new ImageViewer(); } } // end of ImageViewer class

OOP (Java): GUI Intro/11 47 Class Diagrams

OOP (Java): GUI Intro/11 48 Execution (same as before)

OOP (Java): GUI Intro/11 49 Inner Classes Features Inner classes allow code to be better structured – –all the event handling is moved to a separate class from the GUI code But, the event handler method can still get very large if there are many components, and the components must be declared as globals.

OOP (Java): GUI Intro/ An Anonymous (Inner) Class “Anonymous” means “no name” – –an inner class with no name An object created from an anonymous (inner) class is always referenced via its superclass name, as it has no class name – –rather confusing at first continued

OOP (Java): GUI Intro/11 51 The usual way of using anonymous classes is to use them to implement a separate event handler for each component – –lots of anonymous classes, but small Also, this coding approach means that the GUI components do not need to be declared globally.

OOP (Java): GUI Intro/11 52 An Anonymous Action Listener // in makeMenuBar() : JMenuItem openItem = new JMenuItem("Open"); openItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { openFile(); } }); :

OOP (Java): GUI Intro/11 53 Anonymous Class Elements Anonymous object creation Anonymous class definition: fields, methods (in this case just 1 method) openItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { openFile(); } });

OOP (Java): GUI Intro/ ImageViewer with Anon. Listeners public class ImageViewer extends JFrame { // no implements // no globals required public ImageViewer() { // just as before } // no actionPerformed() method : Version 0.5

OOP (Java): GUI Intro/11 55 private void makeMenuBar() { JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); JMenuItem openItem = new JMenuItem("Open"); openItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { openFile(); } }); fileMenu.add(openItem); JMenuItem quitItem = new JMenuItem("Quit"); quitItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); fileMenu.add(quitItem); } // end of makeMenuBar() use two anon. class objects

OOP (Java): GUI Intro/11 56 private void openFile() // open a Swing file chooser to select a file { // test output, until we do this properly System.out.println("open file"); } // end of openFile() // no inner class // public static void main(String[] args) { new ImageViewer(); } } // end of ImageViewer class

OOP (Java): GUI Intro/11 57 Class Diagram unfortunately my UML tool, essModel, does not show anonymous classes

OOP (Java): GUI Intro/11 58 Execution (same as before)

OOP (Java): GUI Intro/11 59 Anon. Classes Features Anonymous classes allow event handler code to be placed with the GUI component code – –everything is in one place – –the GUI components do not need to be globals – –less coding required But, anon. classes can be hard to find and read – keep them small (1-5 lines each)

OOP (Java): GUI Intro/ Listener Implementation Summary There are three ways to implement a listener: – –have the GUI class implement the listener itself (e.g. ImageViewer 0.3) – –implement the listener as an inner class (e.g. ImageViewer 0.4) – –implement multiple listeners as anonymous (inner) classes (e.g. ImageViewer 0.5)