Birds perspective on Java GUI Programming Introduction to the major concepts.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphic User Interfaces Layout Managers Event Handling.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Chapter 7 GUI design. So far this semester Have programmed in a stop and wait mode Program displays dialog box and waits for user to respond This was.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh 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.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Developing User Interfaces (DUI) Chris North cs3724: HCI.
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 
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Intro to Java 2 By Geb Thomas Based on the Java TutorialJava Tutorial.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
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.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CS Lecture 00 Swing overview and introduction Lynda Thomas
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.
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.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
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.
Java Applet. Introductions Applet is java program that can be embedded into HTML pages Java applets runs on the java enables web browsers such as mozila.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Software Design 5.1 From Using to Programming GUIs l Extend model of "keep it simple" in code to GUI  Bells and whistles ok, but easy to use and hide.
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.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
GUIs Graphical User Interfaces. Everything coming together Known: – Inheritance – Interfaces – Abstract classes – Polymorphism – Exceptions New: – Events.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Event Handling CS 21a: Introduction to Computing I First Semester,
Object Oriented Programming in Java Habib Rostami Lecture 10.
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.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
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:
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 17 : Applets King Fahd University of Petroleum & Minerals College of Computer Science.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Welcome To java
CHAPTER Reacting to the user.
A First Look at GUI Applications
GUIs Model/View/Controller Layouts
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
11.1 Applets & graphics.
Presentation transcript:

Birds perspective on Java GUI Programming Introduction to the major concepts

kelk & sh Indledende Datalogi Two GUI class-libraries AWT (Abstract Windowing Toolkit) –Came first – has ”Look & Feel” as platform (Windows, Linux, Apple...) Swing –Newer – common L&F across platforms –L&F Programmable –Class names mostly as AWT + ”J” in front –Not a complete replacement for AWT

kelk & sh Indledende Datalogi Two basic App’s Applications –Based on Frame or JFrame –Run with ”java” – to get a console (nice for debug) –Run with ”javaw” to have no console –Same ”rights” as ”native” programs Applets –Based on Applet or JApplet –Embedded in HTML –Runs in Browser – e.g. IExplorer –Sandbox model for security (restrictions on I/O)

kelk & sh Indledende Datalogi Applications in Swing Your class ”extends” JFrame (inheritance) - we get a lot for free this way The GUI is ”wired up” in the constructor Components are added to the pane which is found with ”getContentPane()” (in AWT directly added to the Frame) A LayoutManager places components - e.g. ”Border”, ”Grid”, ”Flow”... E.g. in ”main” the class is instantiated

kelk & sh Indledende Datalogi Application Code Skeleton MyClass extends JFrame { MyClass() { JButton hit = new JButton(”Hit!”); hit. getContentPane().add(hit);....Setup Layout+more components } public static void main(String[] a){ MyClass frame = new MyClass(); frame.pack(); frame.setVisible(true); }}

kelk & sh Indledende Datalogi From Application to Applet Change constructor into ”void init()” - and remove calls to ”super” if any Remove ”main” completely - no instantiation (done by browser) No setting of tittle Make an HTML-file with tag

kelk & sh Indledende Datalogi The wellknown console paradigm Client program is in charge Client calls OS when doing I/O etc Client continues after call Typically asks one question at the time and waits for an answer Client OS time

kelk & sh Indledende Datalogi The Event-Based Paradigm The OS is in charge OS calls fragments of client code - based on events Not easy to predict calling sequences You create code that you never call OS client time

kelk & sh Indledende Datalogi MVC-pattern: Model-View-Control Model is the data in e.g. attributes/fields or externally - can be complex data-structures. View is the GUI-components setup in the constructor (Buttons, Lists, Checkboxes etc) Control is program-flow - done via event-handlers (With MS MFC View and Control is mixed)

kelk & sh Indledende Datalogi Listeners & Event-Handlers Appl. Comp.Event ObjectListener Object The OS generates an event with help from the application’s component, and sends it to a handler. A handler is a method on a listener object Your ”listener” is registered at your component – typically in Constructor (Frame) or ”init()” (Applet)

kelk & sh Indledende Datalogi Listener Groups ActionListener for buttons and menus KeyListener for keyboard MouseListener WindowListener for Window opened, closed etc. There are 4 ways to implement listeners & handlers… following in (personal) prioritized order

kelk & sh Indledende Datalogi 1. Way: Inner Class Inside the client class a new class is coded Register listener using e.g. ”new MyListener()” It has access to all fields in the outer class There may be several instances of the inner class per instance of the outer class This concept encapsulates the listeners code – without mixing MVC too much

kelk & sh Indledende Datalogi 2. Way: Application is Listener As with inner-class, all code is in the same file – full access to fields. Register listener using ”this” MVC not as mixed as anonymous classes MVC not as separated as inner classes Need to use ”implements” in order to extend JFrame/Frame/JApplet/Applet....see later slide...

kelk & sh Indledende Datalogi 3. Way: External Class The listener is coded in it’s own file – exactly as any other class. Register listener using e.g. ”new MyListener()” Control & View is completely separated - maybe too much. No access to private fields of View class.

kelk & sh Indledende Datalogi 4. Way: Anonymous class The listener is registered with new without a class name at the exact place in the code where the component is inserted. This can be done automatically by many IDE’s This concept encapsulates the code – but mixes control and view It is confusing and difficult to read – drop it!

kelk & sh Indledende Datalogi Two ways to code listeners To assure that it contains the necessary methods with the right parameters, you can use ”implements” – the drawback is that all methods (actually rather few) must be implemented. Alternatively ”extend” an ”adapterclass” - the drawback is that it’s only possible to extend from one class