Building Graphical User Interfaces 6.0. 2 Overview Constructing GUIs Interface components GUI layout Event handling © 2017 Pearson Education, Inc. Hoboken,

Slides:



Advertisements
Similar presentations
CS18000: Problem Solving and Object-Oriented Programming.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
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.
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.
GUI’s and eventhandlers in java Martin Jagersand.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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 JavaLynn Lambert CPSC150 Week 12 InheritanceInterfaces.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
Java Programming Chapter 10 Graphical User Interfaces.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Interfaces & Polymorphism part 2:
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
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.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
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.
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.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
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.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling Objects First with Java - A Practical.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
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 COS 260 DAY 24 Tony Gauvin. 2 Agenda Questions? 10 th Mini Quiz Corrected –So-so results Capstone progress report over due Course evaluations Assignment.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 IM103 week 10 Enhancing the user interface By the end of lecture you should be able to:  distinguish between heavyweight and lightweight components;
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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:
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
A First Look at GUI Applications
A Quick Java Swing Tutorial
Building Graphical User Interfaces
COS 260 DAY 23 Tony Gauvin.
Ellen Walker Hiram College
Timer class and inner classes
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Graphical User Interface
1 Graphical User Interfaces
A Quick Java Swing Tutorial
Advanced Programming in Java
Presentation transcript:

Building Graphical User Interfaces 6.0

2 Overview Constructing GUIs Interface components GUI layout Event handling © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

3 GUI Principles Components: GUI building blocks. –Buttons, menus, sliders, etc. Layout: arranging components to form a usable GUI. –Using layout managers. Events: reacting to user input. –Button presses, menu selections, etc. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

4 AWT and Swing © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

5 Elements of a frame © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. Title Menu bar Content pane Window controls

6 Creating a frame © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ImageViewer { private JFrame frame; /** * Create an ImageViewer show it on screen. */ public ImageViewer() { makeFrame(); } // rest of class omitted. }

7 The content pane © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. /** * Create the Swing frame and its content. */ private void makeFrame() { frame = new JFrame("ImageViewer"); Container contentPane = frame.getContentPane(); JLabel label = new JLabel("I am a label."); contentPane.add(label); frame.pack(); frame.setVisible(true); }

8 Adding menus JMenuBar –Displayed below the title. –Contains the menus. JMenu –e.g. File. Contains the menu items. JMenuItem –e.g. Open. Individual items. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

9 private void makeMenuBar(JFrame frame) { JMenuBar menubar = new JMenuBar(); frame.setJMenuBar(menubar); // 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); }

10 Event handling Events correspond to user interactions with components. Components are associated with different event types. –Frames are associated with WindowEvent. –Menus are associated with ActionEvent. Objects can be notified when an event occurs. –Such objects are called listeners. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

11 Centralized event receipt A single object handles all events. –Implements the ActionListener interface. –Defines an actionPerformed method. It registers as a listener with each component. –item.addActionListener(this) It has to work out which component has dispatched the event. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

12 ActionListener © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. public interface ActionListener { /** * Invoked when an action occurs. ev Details of the event. */ public void actionPerformed(ActionEvent ev); }

13 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. public class ImageViewer implements ActionListener { … public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if(command.equals("Open")) { … } else if (command.equals("Quit")) { … } … } … private void makeMenuBar(Jframe frame) { … openItem.addActionListener(this); … }

14 Centralized event handling The approach works. It is used, so you should be aware of it. However … –It does not scale well. –Identifying components by their text is fragile. An alternative approach is preferred. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

15 Lambdas as event handlers Review earlier material if necessary: –Chapter 5 introduces lambdas. –Chapter 12 introduces functional interfaces. Many event handler interfaces consist of a single abstract method. –They are functional interfaces. –Suited to implementation as lambdas. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

16 Lambda for ActionListener © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. JMenuItem openItem = new JMenuItem("Open");... openItem.addActionListener( (ActionEvent ev) -> { openFile(); } ); item.addActionListener(ev -> { actions }); General form

17 The imageviewer project © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

18 Image processing © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

19 Class responsibilities ImageViewer –Sets up the GUI structure. ImageFileManager –Static methods for image file loading and saving. ImagePanel –Displays the image within the GUI. OFImage –Models a 2D image. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

20 OFImage Our subclass of BufferedImage. Represents a 2D array of pixels. Important methods: –getPixel, setPixel –getWidth, getHeight Each pixel has a color. –We use java.awt.Color. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

21 Adding an ImagePanel © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. public class ImageViewer { private JFrame frame; private ImagePanel imagePanel; … private void makeFrame() { Container contentPane = frame.getContentPane(); imagePanel = new ImagePanel(); contentPane.add(imagePanel); } … }

22 Loading an image © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. public class ImageViewer { private JFrame frame; private ImagePanel imagePanel; … private void openFile() { File selectedFile = …; OFImage image = ImageFileManager.loadImage(selectedFile); imagePanel.setImage(image); frame.pack(); } … }

23 Layout managers Manage limited space for competing components. –FlowLayout, BorderLayout, GridLayout, BoxLayout, GridBagLayout. Manage Container objects, e.g. a content pane. Each imposes its own style. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

24 FlowLayout © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

25 BorderLayout © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

26 GridLayout © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

27 BoxLayout © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. Note: no component resizing.

28 Nested containers Sophisticated layouts can be obtained by nesting containers. –Use JPanel as a basic container. Each container will have its own layout manager. Often preferable to using a GridBagLayout. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

29 Struts and Glue Invisible components used as spacing. Available from the Box class. Strut: fixed size. –Component createHorizontalStrut(int width) –Component createVerticalStrut(int height) Glue: fills available space. –Component createHorizontalGlue() –Component createVerticalGlue() © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

30 Dialogs Modal dialogs block all other interaction. –Forces a response from the user. Non-modal dialogs allow other interaction. –This is sometimes desirable. –May be difficult to avoid inconsistencies. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

31 JOptionPane standard dialogs Message dialog –Message text plus an OK button. Confirm dialog –Yes, No, Cancel options. Input dialog –Message text and an input field. Variations are possible. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

32 A message dialog © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. private void showAbout() { JOptionPane.showMessageDialog(frame, "ImageViewer\n" + VERSION, "About ImageViewer", JOptionPane.INFORMATION_MESSAGE); }

33 Image filters Functions applied to the whole image. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. int height = getHeight(); int width = getWidth(); for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { Color pixel = getPixel(x, y); alter the pixel's color value; setPixel(x, y, pixel); }

34 Adding further filters © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. private void makeLighter() { if(currentImage != null) { currentImage.lighter(); frame.repaint(); showStatus("Applied: lighter"); } else { showStatus("No image loaded."); } private void threshold() { if(currentImage != null) { currentImage.threshold(); frame.repaint(); showStatus("Applied: threshold"); } else { showStatus("No image loaded."); } Code duplication? Refactor!

35 Adding further filters Define a Filter superclass (abstract). Create function-specific subclasses. Create a collection of subclass instances in ImageViewer. Define a generic applyFilter method. See imageviewer2-0. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

36 imageviewer2-0 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

37 Buttons and nested layouts © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. A GridLayout inside a FlowLayout inside a BorderLayout.

38 Borders Used to add decoration around components. Defined in javax.swing.border –BevelBorder, CompoundBorder, EmptyBorder, EtchedBorder, TitledBorder. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

39 Adding spacing © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. JPanel contentPane = (JPanel)frame.getContentPane(); contentPane.setBorder(new EmptyBorder(6, 6, 6, 6)); // Specify the layout manager with nice spacing contentPane.setLayout(new BorderLayout(6, 6)); imagePanel = new ImagePanel(); imagePanel.setBorder(new EtchedBorder()); contentPane.add(imagePanel, BorderLayout.CENTER);

40 Other components Slider Spinner Tabbed pane Scroll pane © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

41 Nested class syntax Class definitions may be nested. –public class Enclosing { … private class Inner { … } } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

42 Inner classes Instances of the inner class are localized within the enclosing class. Instances of the inner class have access to the private members of the enclosing class. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

43 Anonymous inner classes Obey the rules of inner classes. Used to create one-off objects for which a class name is not required. Use a special syntax. The instance is always referenced via its supertype, as it has no subtype name. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

44 Anonymous action listener © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. JMenuItem openItem = new JMenuItem("Open"); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openFile(); } }); Compare with the lambda version

45 Anonymous class elements © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. openItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { openFile(); } ); Anonymous object creation Actual parameter Class definition

46 Exit on window close © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); WindowAdapter provides a no-op implementation of the WindowListener interface.

47 Review Aim for cohesive application structures. –Endeavor to keep GUI elements separate from application functionality. Pre-defined components simplify creation of sophisticated GUIs. Layout managers handle component juxtaposition. –Nest containers for further control. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

48 Review Many components recognize user interactions with them. Reactive components deliver events to listeners. Lambdas are commonly used to implement listeners. Anonymous inner classes are used where lambdas are not possible. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.