GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.

Slides:



Advertisements
Similar presentations
Graphic User Interfaces Layout Managers Event Handling.
Advertisements

Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
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.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
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.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
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.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
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.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
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.
CS Fall 2012, Lab 09 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /20/2015 GUI - Graphical User Interface.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
UID – Event Handling and Listeners Boriana Koleva
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
CS2210: SW Development Methods Topics: Comparable and Comparator interfaces in JCF Function objects Textbook readings: More from MSD, Chapter 9 Pages
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Review_6 AWT, Swing, ActionListener, and Graphics.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java Swing, Events Readings: Just Java 2: Chap 19 & 21, or Eckel’s Thinking in Java: Chap 14 Slide credits to CMPUT 301, Department of Computing Science.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
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.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
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:
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
Graphical User Interfaces
CompSci 230 S Programming Techniques
A First Look at GUI Applications
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Event-driven programming for GUI
Event Driven Programming and Graphical User Interface
A Quick Java Swing Tutorial
Graphical User Interface
Presentation transcript:

GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise

2 Why Study GUIs in CS2110 First, why not? –Complex topic, complex library –Many classes, methods –Hard to do this well initially Reasons we study GUIs –Again, example of inheritance in a framework, software reuse, etc. –Event-driven programming –An important form of program-control

3 Swing Swing is a Java library (framework) for creating GUIs –Part of a larger JFC (Java Foundation classes) –Replaces but uses an older library, AWT –Another, newer alternative: SWT –Used in Eclipse Swing apps will use look-and-feel of the system they’re running on –Or can be set by the program

4 Learning Swing Important things to learn –Swing components –E.g. buttons, text-fields, frames, etc. –How to organize and arrange them –Containers, layout managers –How to make things change when something happens –Event-based programming

5 Containment Hierarchy Top-level container: –place for other Swing components to paint themselves –e.g., JFrame, JDialog, Japplet Intermediate container: –simplify positioning of atomic components –e.g., JPanel, JSplitPane, JTabbedPane

6 Components and Containers See pages in text All Swing GUI objects are Components Some are also Containers –Example: JFrame, JPanel, etc You place other Components inside Containers –Example: a JFrame has buttons, text-fields, etc. –Example: a JPanel is part of a window, in which we organize GUI components

7 What We Do with Containers Add components to them Determine how these items will be arranged on the screen –Layout control –We associate a Swing layout-manager with each container Layout is very hard to do at the beginning –So we won’t sweat it in CS2110

8 Non-Containers Atomic components: –self-sufficient components that present information to and get input from the user –e.g., JButton, JLabel, JList, JComboBox, JTextField, JTable

9 Swing Components and containers: –superclasses and interfaces –extends and implements © O’Reilly 1999

10 Top-Level Containers JFrame example: –contains a single component JRootPane, which has a JMenuBar (optional) and a content pane –add non-menu components to its content panel –theFrame.add( aButton ) –Pre Java 5.0 –theFrame.getContentPane().add( aButton )

Events and User Interaction Programs so far: main() is called and runs to completion Swing programs: –“main” class has (or may be) a JFrame –main() sets up the GUI and ends –Program keeps running, waiting for and responding to “events” –Button pressed, text typed, window closed, etc –Program is event-driven 11

Demo 12

13 Events Two approaches to event handling –read-evaluation loop (client-written loop) –notification-based (callbacks) Swing uses the 2nd approach

14 Events Swing: –objects communicate by “firing” and “handling” events (event objects) –(conventional method call) –events are sent from a single source object to one or more registered listener objects

15 Events Swing: –different event sources produce different kinds of events e.g., a JButton object, when clicked, generates an ActionEvent object, which is handled by an ActionListener (an object whose class implements this interface)

MSD book, p

17 Events Handling: –create a component –e.g., a JButton –add it to the GUI –e.g., to a JPanel –register a listener to be notified when the component generates an event –e.g., interface ActionListener –define the callback method –e.g., actionPerformed()

18 Event Handling class MyListener implements ActionListener { … public void actionPerformed( ActionEvent event ) { // react to event … } } … // instantiate event listener ActionListener listener = new MyListener(); … // instantiate event source JButton button = new JButton( “Hello” ); … // register event listener with event source button.addActionListener( listener );

Common Technique (e.g. Jigloo) Use anonymous class to create a listener object “on the fly” // register event listener with event source button1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { handlerForButton1Press(evt); //see below } });... // and later you write the handler method public void handlerForButton1Press(evt) { // your code to handle button press } 19

What’s this? Java Anonymous Classes There’s a Java technique called anonymous classes –One of several types of nested class definition –You’ll very often see it in GUI programming (Swing) and with threads Situation: –Sometimes Java’s design encourages us to create some thing that might be used just once –That thing needs to be wrapped up in a class, say because we need a function object What couldn’t we just declare it at the same place we use it? Why create a separate file?

Creating and Using an Anonymous Class Example: sort a list of Strings by their length Collections.sort ( stringList, new Comparator() { public int compare( Object o1, Object o2 ) { return ((String) o1).length() - ((String) o2).length(); } } ) ; We’ve created a new Comparator “on the fly” –new creates a new instance, but what kind? –Some object that implements Comparator –Object not named, and its “true” class not named! –What must a Comparator have? compare() –We defined it right here, where it’s used!

Anonymous Classes: Comments Anonymous classes are unlike other classes –They have no name –Typically only implement methods in their interface or superclass. No new methods! –Since they have no name, can only define and use them at one point in your code! Hard to understand at first? Sure! –Naming an abstraction is important for human understanding! –Sorting, a Collection, Comparing Advice –Keep them very short (and simple)! –Be ready to understand them when you see them in Swing and with threads