CPS 108/ola.1 From XP to javax.swing.* l What parts of embracing change can we embrace in 108?  Evolutionary design, small releases, iterative enhancement.

Slides:



Advertisements
Similar presentations
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Advertisements

Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Event Handling Events and Listeners Timers and Animation.
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.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Object-Oriented Analysis and Design
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 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.
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.
Design Patterns and Graphical User Interfaces Horstmann ,
Welcome to CIS 083 ! Events CIS 068.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
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 GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Software Design 4.1 Tell, Don't Ask l Tell objects what you want them to do, do not ask questions about state, make a decision, then tell them what to.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
Software Design 9.1 From Using to Programming GUIs l Extend model of "keep it simple" in code to GUI  Bells and whistles ok, should be easy to use and.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Field Trip #19 Animations with Java By Keith Lynn.
 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.
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
University of Limerick1 Software Architecture Java Layout Managers.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
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.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
CPS 100 spreadsheet.1 Spreadsheet: Graphs and MVC l Model, View, Controller is MVC  Model stores and updates state of application Example: calculator,
Computer Science 209 GUIs Model/View/Controller Layouts.
Project JETT/Duke 1 javax.swing, events, and GUIs l GUI programming requires processing events  There’s no visible loop in the program  Wire up/connect.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Duke CPS Modules in Java l Classes that are related should be grouped together, may even share access to otherwise private methods/instance variables.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
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.
1 Lecture 8: User Interface Components with Swing.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
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:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
A Quick Java Swing Tutorial
Graphical User Interfaces
A First Look at GUI Applications
GUIs Model/View/Controller Layouts
Lecture 27 Creating Custom GUIs
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
MVC overview Model, View, Controller is MVC
Graphical User Interface
Presentation transcript:

CPS 108/ola.1 From XP to javax.swing.* l What parts of embracing change can we embrace in 108?  Evolutionary design, small releases, iterative enhancement  Simple design, don’t build for the future (will you need it?)  Lots of testing, testing, testing  Refactoring: change design, not functionality l What may be hard to embrace in 108?  Code the test first  Pair Programming  Business aspects: meetings, customers, … l Links    

CPS 108/ola.2 Code, Design and Refactoring (See Martin Fowler’s Refactoring book) l Do some up front design: think a little before coding  But, refactoring can help once you’ve got working code  You don’t have to get the design right, you’ll change it  Make things simple, don’t worry too much about future l When do you refactor? When the code smells (Fowler, Ch. 3)  Duplicate code: methods in one class, in two classes, similar but not identical, …  Long methods (if it’s long, it’s wrong)  Long parameter lists  Speculative generality  Middle man  Inappropriate intimacy

CPS 108/ola.3 Model, View, and Primitive Puzzle l Model and GUI/View communicate via inner classes/message adapters  These are a kind of distributed controller in MVC  Often the GUI and the controller are the same thing  Sometimes a separate controller isn’t warranted l Who is responsible for button/image functionality?  Consider clicking on “5”, initially in location 5, but later … In the GUI 5 is 5, but in the model it’s different  Who is responsible for what kind of image should be drawn in Minesweeper?

CPS 108/ola.4 MVC in Swing (see TextFieldDemo.java) l Nearly every Jxxyy has a model (button, list, …)  Model maintains state, GUI responsible for drawing  GUI via java.awt.event.* also handles control, so View and Control are co-located  Consider JButton, what’s in state? String, default, enabled, … l Changes in model are automatically reflected in view, not using Observer/Observable (which are clunky)  Consider example in ExpandableList.java, what happens when new value added to list?  What happens when value is selected, part of MVC?

CPS 108/ola.5 Horstmann on JList (Core Java V. I) The internal architecture of the list component … is rather elegant. Unfortunately, the designers at Sun felt that they needed to show off that elegance, rather than hiding it from the programmer who just wants to use the component. You will find that the list control is somewhat awkward to use for simple cases because you need to manipulate some of the machinery that makes the general cases possible. l How does this mesh with XP/Simplicity?  Is there a difference between COTS and our code? (Is java source commercial?)  What are lists used for?  Is it that hard to create a simple, expandable list?

CPS 108/ola.6 Listeners and Adapters l MouseListener has five methods, KeyListener has three  What if we’re only interested in one, e.g., key pressed or mouse pressed? As interface, we must implement all methods as no-ops As adapter we need only implement what we want l Single inheritance can be an annoyance in this situation  See FlawedLowerTextField and ExpandableList In former, must use Listener, in latter can use adapter, why? l What about click/key modifiers, e.g., shift/control/left/both  Platform independent, what about Mac?

CPS 108/ola.7 A GUI object can be its own client l Occasionally a GUI will be a listener of events it generates  Simple, but not extendable  Inner classes can be the listeners, arguably the GUI is still listening to itself, but … Encapsulating the listeners in separate classes is better l Client (nonGUI) objects cannot access GUI components  Properly encapsulated JTextField, for example, responds to aGui.displayText(), textfield not accessible to clients  If the GUI is its own client, it shouldn’t access textfield Tension: simplicity vs. generality l Don’t wire widgets together directly or via controller that manipulates widgets directly  Eventual trouble when GUI changes

CPS 108/ola.8 Swing Concepts l Object < JComponent < JContainer < Jframe  The component/container pair illustrates Composite pattern: Container has Components, including other Containers Understanding Z-order, painting hierarchy is important in complex applications, see Core Java/Java Tutorial/O’Reilly  Containers have layout managers that control how widgets are added and appear BorderLayout is versatile, FlowLayout is simple, GridBagLayout is the kitchen sink, GridLayout could be useful in OOGA l JPanel is the simplest container, use for holding widgets  Defaults to FlowLayout, add widgets and other panels, …

CPS 108/ola.9 Swing continued l JFrame is a top-level window which is a container  Widgets added to a frame’s content pain, retrieved via getContentPane(), different from AWT  LayoutManager used in default getContentPane() is BorderLayout Use BorderLayout.NORTH, not “North” (some books) l Layout managers illustrates the Strategy Design Pattern  Encapsulates algorithm/behavior as a class, pluggable  Users of strategy delegate responses/use to the the strategy  Put a new layout in every container you use (can’t hurt?)

CPS 108/ola.10 Listeners l Events propagate in a Java GUI as part of the event thread  Don’t manipulate GUI components directly, use the event thread  Listeners/widgets register themselves as interested in particular events Events go only to registered listeners, can be forwarded/consumed ActionListener, KeyListener, ItemListener, MouseListener, MouseMotionListener, …, see java.awt.event.*  Isolate listeners as separate classes, mediators between GUI, Controller, Application  Anonymous classes can help here too

CPS 108/ola.11 MVC l Model, View, Controller is MVC  Model stores and updates state of application Example: calculator, what's the state of a GUI-calculator?  When model changes it notifies its views appropriately Example: pressing a button on calculator, what happens?  The controller interprets commands, forwards them appropriately to model (usually not to view) Example: code for calculator that reacts to button presses Controller isn't always a separate class, often part of GUI- based view in M/VC l MVC is a fundamental design pattern : solution to a problem at a general level, not specific code per se  Patterns part of new foundation of object-orientation

CPS 108/ola.12 Spreadsheet Model l What happens when we enter a number, string, or formula?  Model changes to record new entry in a cell Propagate change to views  Model changes to incorporate dependent calculations What about D10 change when B10 changes below? l Graphs: why? l Controller? l View? l Dump command?