Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interfaces
Graphical User Interfaces Java’s AWT and Swing APIs.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
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.
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.
1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and a Simple Demo Program l Layout Managers l Buttons and Action Listeners l Container.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Graphical User Interfaces Allow for interaction with –Buttons –Menus –Text Fields Two Java Libraries to assist in GUI Programming –AWT –Swing.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
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.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
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.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Layout Managers Arranges and lays out the GUI components on a container.
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.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Computer Programming with JAVA Chapter 7. Event-Driven Programming Using the AWT Event-Driven Programming GUIs and the AWT Simple Window Interfaces Components,
Chapter 7Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 7 l Event-Driven Programming »GUIs and the AWT l Simple Window.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Creating a GUI Class An example of class design using inheritance and interfaces.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
GUI- Model-View-Controller
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
Graphical User Interfaces -- GUIs
A First Look at GUI Applications
Abstract Window ToolKit (AWT)
Ellen Walker Hiram College
GUI building with the AWT
Programming Graphical User Interface (GUI)
GUI building with the AWT
Graphical User Interface
GUI- Model-View-Controller
Presentation transcript:

Graphical User Interface CSI 1101 N. El Kadri

Plan - agenda Graphical components Model-View-Controller Observer/Observable

AWT The Abstract Window Toolkit (AWT) is the oldest set of classes used to build graphical user interfaces (GUI) in Java. It has been part of all the Java releases. A more recent and improved toolkit is called Swing. For this introduction, we will focus on AWT.

Components/Containers A graphical element is called a component. Accordingly, there is a class called Component that defines the characteristics that are common to all components. Components include: windows, buttons, checkboxes, menus, text fields, scroll bars, etc.

The components that contain other components are called containers. Accordingly, there is a class called Container that defines the characteristics that are common to all the containers.

AWT is a rich source of examples of the use of inheritance. A Component defines a collection of methods that are common to all the graphical objects, such as setBackground( Color c ) and getX().

Components/Containers – Cont’d A Container will contain other graphical components, and therefore declares a method add( Component component ) and setLayout(LayoutManager mgr ). A Window is a Container that is not contained in any other Container. It defines the methods show() and addWindowListener(WindowListener l ).

Hello World -1- A Frame is a top-level window with a title and a border. import java.awt.*; public class HelloWorld { public static void main( String args[] ) { Frame f = new Frame( "Hello World!" ); f.setSize( 200,300 ); f.setVisible( true ); }  a top-level component is one that is not contained within any other component.

DrJava Alternatively, use DrJava to create and experiment with graphical objects. Use the interactions window and type each of the following statements one by one. > import java.awt.*; > Frame f = new Frame( "Hello World!" ); > f.setSize( 100, 200 ); > f.setVisible( true ); > f.setVisible( false ); > f.setVisible( true ); > f.setVisible( false ); You will see that a Frame of object is not visible unless you make it visible.

Hello World -2- Let’s create instead a specialized Frame that has the required characteristics for this application. import java.awt.*; public class MyFrame extends Frame { public MyFrame( String title ) { super( title ); setSize( 200,300 ); setVisible( true ); } Which would be used as follows: import java.awt.*; public class Run { public static void main( String args[] ) { Frame f = new MyFrame( "Hello World" ); }

MyFrame is a specialized Frame, which is a specialized Container, therefore, it may contain other components. import java.awt.*; public class MyFrame extends Frame { public MyFrame( String title ) { super( title ); add( new Label( "Some text" ) ); setSize( 200,300 ); setVisible( true ); }

LayoutManager When adding new components, we would like to have control over the placement of the objects (components). A layout manager is an object responsible for placing and sizing the components in a container. LayoutManager is an interface and Java provides several implementations: FlowLayout, BorderLayout and GridLayout are the main ones. FlowLayout adds the components from left to right, from top to bottom, this is the default layout manager for a Panel. BorderLayout is a layout that divides the container into zones: north, south, east, west and center, this is the default layout manager for a Frame. GridLayout divides the container into m×n zones (2 dimensional grid).  The Java library has approximately 20 layout manager implementations.

BorderLayout import java.awt.*; public class MyFrame extends Frame { public MyFrame( String title ) { super( title ); add(new Label( "North" ),BorderLayout.NORTH ); add(new Label( "South" ),BorderLayout.SOUTH ); add(new Label( "East" ),BorderLayout.EAST ); add(new Label( “West" ),BorderLayout.WEST ); add(new Label( "Center" ),BorderLayout.CENTER ); setSize( 200,300 ); setVisible( true ); }

FlowLayout import java.awt.*; public class MyFrame extends Frame { public MyFrame( String title ) { super( title ); setLayout( new FlowLayout() ); add( new Label( "-a-" ) ); add( new Label( "-b-" ) ); add( new Label( "-c-" ) ); add( new Label( "-d-" ) ); add( new Label( "-e-" ) ); setSize( 200,300 ); setVisible( true ); }

Panel A Panel is the simplest Container. It can be used to regroup several components and may have a different layout than the container that it is part of.

import java.awt.*; public class MyFrame extends Frame { public MyFrame( String title ) { super( title ); setLayout( new BorderLayout() ); add( new Label( "Nord" ),BorderLayout.NORTH ); add( new Label( "Est" ),BorderLayout.EAST ); add( new Label( "Ouest" ),BorderLayout.WEST ); add( new Label( "Centre" ),BorderLayout.CENTER ); Panel p = new Panel(); p.setLayout( new FlowLayout() ); p.add( new Label( "-a-" ) ); p.add( new Label( "-b-" ) ); p.add( new Label( "-c-" ) ); p.add( new Label( "-d-" ) ); p.add( new Label( "-e-" ) ); add( p,BorderLayout.SOUTH ); setSize( 200,300 ); setVisible( true ); }

Event-driven programming Graphical user interfaces are programmed different from most applications. In an event-driven application, the program waits for something to occur, the user clicks a button or presses a key. An event is an object that represents the action of the user. In Java, the components are the source of the events. A component generates an event or is the source of an event. For example, –When a button is pressed and released, AWT sends an instance of ActionEvent to the button, by calling processEvent on the button.

ActionListener To handle the events that will be generated by the button, one needs to add (sometimes we say register) an object that implements the interface ActionListener.

import java.awt.*; import java.awt.event.*; public class Square extends Frame { Button button = new Button( "Square" ); TextField input = new TextField(); public Square() { super( "Square GUI" ); setLayout( new GridLayout( 1,2 ) ); add( button ); add( input ); button.addActionListener( new SquareActionListener( this ) ); pack(); show(); } protected void square() { int v = Integer.parseInt( input.getText() ); input.setText( Integer.toString( v*v ) ); }

The interface ActionListener lists only one method actionPerformed(ActionEvent e). A SquareActionListener object must know which method square to call, therefore, it has an instance variable that designates the Square object, and this variable is initialized by the constructor. class SquareActionListener implements ActionListener { private Square appl; SquareActionListener( Square appl ) { this.appl = appl; } public void actionPerformed( ActionEvent e ) { appl.square(); }

Alternatively, the class Square could be handling the event, as shown on the following slide.

import java.awt.*; import java.awt.event.*; public class Square extends Frame implements ActionListener { Button button = new Button( "Square" ); IntField input = new IntField(); public Square() { super(" Square GUI" ); setLayout( new GridLayout( 1,2 ) ); add( button ); add( input ); input.setValue( 2 ); addWindowListener( new SquareWindowAdapter( this ) ); button.addActionListener( this ); pack(); show(); } protected void square() { int v = input.getValue(); input.setValue( v*v ); } public void actionPerformed( ActionEvent e ) { square(); }

class SquareWindowAdapter extends WindowAdapter { private Square appl; SquareWindowAdapter( Square appl ) { this.appl = appl; } public void windowClosing( WindowEvent e ) { System.exit(0); } class IntField extends TextField { public int getValue() { return Integer.parseInt( getText() ); } public void setValue( int v ) { setText( Integer.toString( v ) ); }

Let’s add a button to quit the application. The class Square will be the event- handler for both buttons. Therefore, the method actionPerformed must be able to distinguish between an event that originated from pressing the button square and one that originated from pressing the button quit; fortunately, the event encapsulates this information, see method getSource().

import java.awt.*; import java.awt.event.*; public class Square extends Frame implements ActionListener { Button bSquare = new Button( "Square" ); Button bQuit = new Button( "Quit" ); IntField input = new IntField(); public Square() { super( "Square GUI" ); setLayout( new GridLayout( 1,3 ) ); add( bSquare ); bSquare.addActionListener( this ); add( input ); input.setValue( 2 ); add( bQuit ); bQuit.addActionListener( this ); addWindowListener( new SquareWindowAdapter( this ) ); pack(); show(); } protected void square() { int v = input.getValue(); input.setValue( v*v ); }

public void actionPerformed( ActionEvent e ) { if ( e.getSource() == bSquare ) { square(); } else if ( e.getSource() == bQuit ) { System.exit(0); } class SquareWindowAdapter extends WindowAdapter { private Square appl; SquareWindowAdapter( Square appl ) { this.appl = appl; } public void windowClosing( WindowEvent e ) { System.exit( 0 ); } class IntField extends TextField { public int getValue() { return Integer.parseInt( getText() ); } public void setValue( int v ) { setText( Integer.toString(v) ); }

To close the application when the closing button is clicked add the call addWindowListener(... ). import java.awt.*; import java.awt.event.*; public class Square extends Frame { Button button = new Button( "Square" ); TextField input = new TextField(); public Square() { super( "Square GUI" ); setLayout( new GridLayout( 1,2 ) ); addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit(0); } add( button ); add( input ); button.addActionListener( new SquareActionListener( this ) ); pack(); show(); } protected void square() { int v = Integer.parseInt( input.getText() ); input.setText( Integer.toString( v*v ) ); }

The method square retrieves the user input, converts it to an int and puts back the square value in the text field. private void square() { int v = Integer.parseInt( input.getText() ); input.setText( Integer.toString( v*v ) ); }

Instead, it might come handy to have a specialized version of the text field that handles the String to int and int to String conversions for us. Let’s create a new subclass called IntField: class IntField extends TextField { public int getValue() { return Integer.parseInt( getText() ); } public void setValue( int v ) { setText( Integer.toString( v ) ); }

which would replace the TextField in our application: import java.awt.*; import java.awt.event.*; public class Square extends Frame { Button button = new Button( "Square" ); IntField input = new IntField(); public Square() {... } private void square() { int v = input.getValue(); input.setValue( v*v ); }

class IntField extends TextField { public int getValue() { return Integer.parseInt( getText() ); } public void setValue( int v ) { setText( Integer.toString( v ) ); }

Nested Components Fancier presentations often require nested components. The following example illustrates the use of a Panel to contain two buttons, the layout of that Panel is GridLayout while the top- level widow uses a BorderLayout

public class Square extends Frame { private static final String newline = System.getProperty( "line.separator" ); Button button = new Button( "Square" ); IntField input = new IntField(); TextArea output = new TextArea( 5, 40 ); public Square() { //... setLayout( new BorderLayout() ); add( output, "Center" ); Panel bottom = new Panel(); bottom.setLayout( new GridLayout( 1,2 ) ); bottom.add( button ); bottom.add( input ); add( bottom, "South" ); pack(); show(); } //... }

Next Time… Model-View-Controller (MVC) pattern Observer/Observable