GUI Programming Graham Hardman 12 th June 2002.

Slides:



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

Introduction to Java 2 Programming
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 (Part IV)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
CS18000: Problem Solving and Object-Oriented Programming.
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.
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.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
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.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
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.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
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.
Software Construction Lecture 10 Frameworks
Welcome to CIS 083 ! Events CIS 068.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
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.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
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.
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.
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.
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.
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.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
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 Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Basics of GUI Programming Chapter 11 and Chapter 22.
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.
Java Swing One of the most important features of Java is its ability to draw graphics.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
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.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
A Quick Java Swing Tutorial
A First Look at GUI Applications
Ellen Walker Hiram College
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Graphical User Interface
Presentation transcript:

GUI Programming Graham Hardman 12 th June 2002

Java GUI Programming2 Overview GUI Packages & Technologies GUI program layout Components Swing event-driven programming model Useful(?) Examples (all the way through) GUI component layout

Java GUI Programming3 Abstract Windowing Toolkit GUI package in original SDK Platform-dependent (heavyweight) components – contains native components which rely on underlying window manager Provides components, and methods for laying them out within containers

Java GUI Programming4 Swing Became part of core API in JDK 1.2 Platform-independent (lightweight) components, as Swing is written in Java Consistent look-and-feel across platforms Built on top of AWT

Java GUI Programming5 AWT vs Swing Swing designed to complement AWT rather than replace it (completely…) Swing components offer more functionality Swing components still being developed / enhanced, AWT API (probably) complete Swing components offer built-in support for accessibility (eg braille readers), which is becoming more significant

Java GUI Programming6 A (uselessly) Basic GUI Program import java.awt.*; import javax.swing.*; public class Hello { public static void main(String args[]) { JFrame frame = new JFrame(“Hello world”); frame.setVisible(true); } Import the AWT and Swing classes Make the frame visible Make the program executable Create a top-level frame

Java GUI Programming7 GUI Components Swing provides a rich set of widgets (Almost) all inherit from one superclass … …so much of the API is consistent across widgets Without further ado, let’s look at some widgets

Java GUI Programming8 Some Basic Components JLabel Displaying text or icon JTextField A text type-in box JComboBox A drop-down option list JPanel Groups other components JSlider Allow selection from range JButton A simple push-button JFrame Visible window on screen

Java GUI Programming9 JFrame Very important in most Swing applications – without it you won’t see anything! Many Swing apps are subclasses of JFrame, therefore taking advantage of its methods A heavyweight Swing component – relies on OS windowing system for appearance

Java GUI Programming10 JFrame (continued 1) JFrame uses the concept of a content pane to keep hold of and arrange (most) components : Different layers for other component types (to accommodate overlap etc.) Menu bar Pane to handle mouse events etc. Content pane

Java GUI Programming11 Using Simple Components Often the GUI appearance can be achieved by calling the same few methods on each widget… setText(“fish”); setSize(123,321); setEnabled(true); setForeground(Color.red); setBackground(Color.blue); setIcon(new ImageIcon(“fish.jpg”)); setLocation(15,50); setVisible(false); …and of course frame.add(widget). Correct?

Java GUI Programming12 JFrame (continued 2) frame.add(widget); frame.getContentPane().add(widget); Wrong – program compiles, but error occurs at runtime This is the correct method Frequently the code looks slightly different… Container c = frame.getContentPane(); c.add(widget1); c.add(widget2);

Java GUI Programming13 JPanel Invisible by default, but its appearance can be tailored in the same way as any other component. Can be treated in pretty much the same way as the main content pane – for example, the setLayout( ) and add( ) methods are identical. Designed specifically as a container for displaying other components together in logical groups.

Java GUI Programming14 JPanel (continued) Much of the power and usefulness of the JPanel lies in its ability to contain other JPanels… …which, when combined with carefully-chosen layout managers, provides almost infinite flexibility in terms of GUI appearance JPanel also recognises low-level events such as mouse motion and clicking, so it can be used as a canvas to support mouse-intensive tasks such as drawing shapes by following the mouse pointer.

Java GUI Programming15 Layout Managers While we can work without using any of Java’s predefined layout managers… …they do make it easier to add extra widgets, resize windows etc. without too much disruption to code or interface. We’ll look at three provided with AWT – FlowLayout, BorderLayout and GridLayout (There are others, but sophisticated enough GUIs can be made without them, so we’ll ignore them).

Java GUI Programming16 BorderLayout Default layout for JFrame and JApplet Display is divided into 5 regions Each region holds one visible component (only the last component is seen if more than one is added to a single region) Each component expands to fill the entire region by default

Java GUI Programming17 BorderLayout NORTH SOUTH WESTEASTCENTER

Java GUI Programming18 FlowLayout Default layout for JPanel Components arranged from left to right, top to bottom, as they are added Components can be centered, or aligned to the left or the right of the container

Java GUI Programming19 GridLayout Container is logically divided into a grid Components can be laid out in the order they are added (default), or any arbitrary order by using an overloaded add( ) method All grid cells are of equal size, and each component expands to fill its cell

Java GUI Programming20 Taking stock So far we’ve looked at how we can make GUIs look the way we want them to… … but we also need to look at how we can make them work the way we want. End of lecture 1. More later :-)

Java GUI Programming21 Anatomy of a GUI Program Graphical components Listeners Application code (Visible bits which generate actions) (Listen for actions and respond in a useful way) (Performs the donkey work)

Java GUI Programming22 Analogy ‘User interface’? ‘Listener’? ‘Application code’? Dial Variable resistor Volume changes

Java GUI Programming23 Event-driven Programming Order of execution is governed by user Program responds to events generated by user interaction with GUI components Swing achieves this by using events, event listeners and event handlers

Java GUI Programming24 A Diagram Handler processesEvent occurs Interaction with widget Listener receives EventObject generated Automatic method call Code executed Wait for event Initialise Quit

Java GUI Programming25 Events Common events are keystrokes, mouse clicks and mouse cursor movement across components (also known as rollovers) Programmer doesn’t (usually) need to create them by hand… … Swing generates events automatically for all common interactions with GUI components… …we just need to know what kind of event is generated for each action, and how to listen for it

Java GUI Programming26 Some Events ActionEvent – when a JButton is clicked ItemEvent – when a list item is selected KeyEvent – a key stroke MouseEvent –mouse movement or click ChangeEvent – when a JSlider is moved

Java GUI Programming27 Event examples JButton ActionEvent generated (left click) Container (right click) MouseEvent generated Currently, these events fire off into space…

Java GUI Programming28 Event Listeners Events are useless to us if we don’t know they’ve occurred (seems obvious, but is a common gotcha) For each component we expect to generate an event, we register an event listener to deal with it (one listener can handle one or more components). Listener can then execute useful methods depending on the type of event The listener can also tell which widget generated the event by calling the event’s getSource( ) method, which returns a reference to the widget.

Java GUI Programming29 Event Listeners (continued) Each listener must implement certain method(s), which are automatically called when the relevant event is generated : Example – ActionListeners must implement the actionPerformed( ) method, which is called whenever the listener detects an ActionEvent We are now at the stage where we can create GUIs that do more than just look nice. Almost… See the examples on the web page for more in- depth information

Java GUI Programming30 Event Handlers Event handlers are just pieces of code that do useful things (no different to standard code in appearance) … …the difference is they are only executed when specific events occur (ie the user determines the execution path) Event handler code must be called by a listener, or included within a listener method, or once again, events are useless to us (another common gotcha)

Java GUI Programming31 Event handling in WidgetDemo comboBox = new JComboBox(names); frame.add(comboBox); comboBox.addItemListener ( new ItemListener() { public void itemStateChanged(ItemEvent ie) { changeLandF(selectedName); } ); changeLandF(arg) { UIManager.setLookAndFeel(arg); } Define and register listener Define application method Initialise component Call application method when event occurs