Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Graphical User Interfaces
Java Software Development Paradigm Lecture # 12. Basics of GUI.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Graphical User Interfaces CS 2110 Spring Ivan Sutherland: “Sketchpad”,
Corresponds with Chapter 12
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Graphical User Interface (GUI) Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
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.
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.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Layout Managers Arranges and lays out the GUI components on a container.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
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.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
Basics of GUI Programming Chapter 11 and Chapter 22.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Java Swing. Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: – Events:
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
A Quick Java Swing Tutorial
Welcome To java
Chapter 9: Graphical User Interfaces
Java GUI.
Graphical User Interfaces (GUIs)
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or programmatic action –Listeners: A method that responds to an event –Components: A GUI object –Containers: A collection of components –listener interface: An interface that contains listeners –Adapter class: A class that implements a listener interface with default methods –layout managers: An object that defines how components in a container present to the user –special features: Methods that customize a GUI's look and feel

Java GUI Facilities AWT (Abstract windowing toolkit) –Designed for creating applets –Not powerful enough for application programs –Simple and easy to use, and supported by almost all browsers –Peer model relies on platform-dependent native code (heavyweight) Swing (The creator was a "swing" dancer) –Newer and more sophisticated GUI facility –Swing class names start with the letter 'J'. –Consistent look and feel across operating systems –Does not depend on operating system facilities (lightweight) –Many swing classes extend their AWT counterparts –Most application developers now use Swing, not AWT –Supports a pluggable look and feel (UIManager.setLookAndFeel("javax.swing.plaf.windowsLookAndFeel"); AWT and Swing are each a collection of Java classes for GUI development Java GUI facilities are large and complex. We focus only on a small subset

AWT vs. Swing AWT advantages over Swing 1.Simpler to learn and use 2.Older browsers might not support Swing Swing advantages over AWT 1.Much greater functionality 2.Portability. Consistent look and feel across platforms 3.Vendor support. AWT functionality is frozen 4.Built-in double buffering overlaps processing with I/O

Containers and Components Container –A Java class instantiated to hold groups of components –Examples: JApplet, JFrame, JPanel, JTabbedPanel, JScrollPane Component –A Java class instantiated to create a type of GUI object –Examples: JButton, JCheckBox, JComboBox, JColorChooser, JFileChooser, JLabel, JList, JMenu, JOptionPane, JPasswordField, JRadioButton, JSlider, JTextArea, JTextField, JToggleButton, JTree Java GUI applications principally consist of containers and components

Layout Managers Java Layout Managers –AWT: Flow, Border, Card, Grid, GridBag –Swing: Flow, Border, Card, Grid, GridBag, Box, Overlay Comments –If a GUI doesn't choose a layout, Java uses FlowLayout –Each layout manager has its own idiosyncrasies responds to window resizing differently May or may not respond to a component's preferred size or alignment preferences Objects that controls how a container's components display

Layout Manager Summary AWT managers –Flow: left-to-right, top-to-bottom in order (The default) –Border: Sections for North, South, Center, East, West –Card: Tab like capability, display one card at a time –Grid: two dimensional array of components –GridBag: two dimensional array of components where components can span rows and columns Swing managers –Box: vertical or horizontal list of components –Overlay: components that can overlap each other. See the demo program on the class web-page (by Lewis/Loftus)

Overlay Layout Example OverlayLayout allows a container to display components over the top of each other.

GridBagLayout GridBagLayout defines a grid of cells. Components can span rows and columns and have varied heights and widths.

CardLayout Example CardLayout allows GUIs to display different panels at different times. CardLayout GUIs often use a combo box to control what panel displays JTabbedPane is an alternative to C

GUI Design The goal is to use Java GUI facilities to solve a user's application problem. The first steps for a GUI designer is to: –Fully understand the problem –Design the way the application interact with the user –Create a GUI containment hierarchy GUI applications need to be –Be robust, properly handling all possible errors. For example, make sure to see what happens when the window is resized. –Be intuitive and easy to use –Should have a consistent interface across panels and frames

GUI Look and Feel

GUI Containment Hierarchy North Panel: Two labels, flow layout South Panel: Two Buttons, box layout East Panel: Slider, label, combo box, box layout West Panel: Three box panels with label and text field, panel with two radio buttons, box layout Center Panel: Six check boxes, label, and text field Note: This slide ties to the picture shown on the previous slide Designers likely would create a class for each of the five sections

General Comments It is not hard to create a GUI application, but it can be tedious There is no drag and drop capability to create GUI components like we have in Visual Basic GUIs in java have many single line Statements –To create GUI components –To set GUI properties –To call special methods Good design –Break up GUI applications to a set of panel or component classes. –This makes the code easier to maintain

Steps to create a GUI Application 1.Create a class hierarchy diagram for the application 2.Determine components should be in their own Java class 3.Instantiate the application's JFrame with its title 4.Configure the window close procedure 5.Instantiate the components and set their properties 6.Call the JFrame getContentPane() method to get the default application container. 7.Add the components to the JFrame application container 8.Set the size of the frame 9.Make the frame visible Note: It is possible for a JFrame to have multiple containers (layers)

Steps to Create a Component or Container Class 1.GUI component class signature line –Extend the appropriate component class (extends) –Implement the appropriate listener (implements) 2.Create references to the needed sub-components 3.Instantiate the components in the constructor class or in the applet init() method 4.Define the layout for containers 5.Call methods for setting custom properties 6.Add components to containers 7.Add listeners

Components JButton JButton button = new Jbutton(“Add”); button.setMnemonic(‘A’); button.setToolTipText(“Add a record”); JFrame JFrame frame = new JFrame("A Title"); JTextField JTextField data = new JTextField(“”) String text = data.getText(); JLabel JLabel label = new JLabel(“label to display”); label.setText(“new Text”); JComboBox JComboBox box = new JComboBox(array or object); JRadioButton JRadioButton button= new JRadioButton(“end”, true); JCheckBox JCheckBox box = new JCheckBox((“Bold”, true); Jlist Jlist list = new Jlist(array or object); JScrollPane JScrollPane scroll = new JScrollPane(array or object); JtabbedPane JTabbedPane pane = new JTabbedPane(); pane.addTab(“label”, container);

Listeners: Methods responding to Events Examples MouseListener – respond to user mouse events –Add "implements MouseListener" to the GUI class –Code listener methods (e.g. mouseClicked()) and attach to the GUI object MouseMotionListener – respond to mouse movements –Add "implements MouseMotionListener" to the GUI class –Code listener methods (e.g. mouseMoved()) and attach to the GUI object ActionListener – Recponds once to button selections –Add "implements ActionListener" to the GUI class –Code the "actionPerformed" method and attach to the GUI object ItemListener – Responds multiple times to changes to a component –Add "implements ItemListener" to the GUI class –Code the "itemStateChanged" method –Attach the ItemListener to the GUI object Window Listener – respond to clicks of a frame's X button –Create a class that extends WindowAdapter –Code the WindowListener methods and attach to the frame

Future Direction Model, View, Controller (MVC) –Model: The classes that manipulate the data managed by the application –View: The User interface. Can be manually created, but normally created by a GUI builder (Java – Scene Builder), which generates XML. –Controller: The module that responds to user events and is a bridge between the Model and the view Design Pattern: A framework that can be used as a starting point to create many applications of a particular category (e.g. Master – Detail)

Languages Used Languages vary between platforms Desktops, Laptops, Microsoft tablets: Java/Java FX Apple-based Desktops, Laptops, iPhones, Ipods, iPads: Objective C moving to Swift Android-based phones and tablets: Java/Dalvik

Terminology The object describing the application environment –Android: Context –Java-FX: Stage –Apple: View Listeners –Android and Java: Listeners –Apple: targets and delegates Objects holding other objects –Views, Scenes (Java) Objects for buttons, text fields, etc. –Components, widgets, controls

Creating a View Java/FX –Create in code: not recommended –Create using XML: possible but not the best –Use the Scene Builder with drag and drop View organization (Tree Structure) –Root View –Child Views

Java FX example public class FXMLExample extends Application { public static void main(String[] args) { Application.launch(FXMLExample.class, args); public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("example.fxml")); stage.setTitle("FXML Welcome"); stage.setScene(new Scene(root, 300, 275)); stage.show(); } Note: No public static void main(String[] args)

Simple FXML Example

Partial Controller Code import javafx.scene.control.Button Import javafx.scene.control Label Public class JavaFXAppController implements Initializable private Label Private Button Private void handleButtonAction(ActionEvent event) { /* Code goes here */ } Public void initialize(URL url, ResourceBundle rb) { /* Code goes here */ } }