1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces.

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

Fondamenti di Java Introduzione alla costruzione di GUI (graphic user interface)
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Chapter 16 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.
User Interfaces II GUI – Awt, Swing, Web
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 22 GUI Programming I.
Introduction to Swing Components Chapter 14. Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
OLD BUSINESS : Lets talk about the next exam Project proposals – Functional Definitions Go over homework – NEW BUSINESS: Chapter 4 today GUIs Assignment.
Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
Graphical User Interfaces
Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave.
Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: identify and use some of the common components.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Event Handling --
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Graphical User Interfaces Java’s AWT and Swing APIs.
Graphical User Interfaces (Part IV)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
Drawing in a frame – Java GUI
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Swing: the art of the GUI COMP204, Bernhard Pfahringer.
Java Applets- Using SwingWorker Dave Price and Chris Loftus Computer Science Department University of Wales, Aberystwyth.
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.
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.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Java Programming Chapter 10 Graphical User Interfaces.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
CS12420 – Swing and threads Lynda Thomas
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,
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
Concurrent Programming and Threads Threads Blocking a User Interface.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
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.
1 cs205: engineering software university of virginia fall 2006 GUI Design and Implementation Schedule design meetings this week.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
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.
Event Handling CS 21a: Introduction to Computing I First Semester,
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Lecture 28 Concurrent, Responsive GUIs
Course Outcomes of Advanced Java Programming AJP (17625, C603)
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Introduction to Event Handling
Events, Event Handlers, and Threads
Using threads for long running tasks.
Graphical User Interface
Presentation transcript:

1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

2 cs205: engineering software Project Progress Reports Due Monday, Nov 27 1.Updated description of your design –Explain any changes to your project idea since the design review meeting –Explain major changes to your design 2.Clear list of what you have done: –Include print outs of code for some important modules –Explanation of your testing strategy (and how far you have gotten) 3.Plans for completing the project –List of what is left to do and how you will do it

3 cs205: engineering software public Component add(Component c) Component Container Window Frame JFrame JComponent JLabel AbstractButton JButton JPanel...and hundreds (?) more subtypes in API

4 cs205: engineering software Adding Components import javax.swing.*; import java.awt.*; public class Main { private static void showGUI() { //Create and set up the window. JFrame frame = new JFrame("Swing GUI"); java.awt.Container content = frame.getContentPane(); content.setLayout(new FlowLayout()); content.add(new JLabel ("Yo!")); content.add(new JButton ("Click Me")); frame.pack(); frame.setVisible(true); }

5 cs205: engineering software Making Buttons public void addActionListener(ActionListener l) MODIFIES: thisActionListener EFFECTS: Adds an ActionListener l to the button. java.awt.event interface ActionListener extends EventListener { void actionPerformed (ActionEvent e) EFFECTS: anything Note: this method is called when an action occurs. }

6 cs205: engineering software import javax.swing.*; import java.awt.*; import java.awt.event.*; class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { System.out.println ("Got a button press:” + e); } public class Main { private static void showGUI() { JFrame frame = new JFrame("Swing GUI"); java.awt.Container content = frame.getContentPane(); content.setLayout(new FlowLayout()); content.add(new JLabel ("Yo!")); JButton button = new JButton ("Click Me"); button.addActionListener(new ButtonListener()); content.add(button); frame.pack(); frame.setVisible(true); }

7 cs205: engineering software Action Events Got a button press: java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=Click Me, when= ,modifiers=Button1] on javax.swing.JButton[,27,5,82x26,alignmentX=0.0,alignmentY=0.5, 9ab3e,flags=296,maximumSize=,minimumSize=, preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=, margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=1 4],paintBorder=true,paintFocus=true, pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=, selectedIcon=,text=Click Me,defaultCapable=true]

8 cs205: engineering software class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { if (e.getActionCommand().equals ("On")) { System.out.println("On!"); } else if (e.getActionCommand().equals("Off")) { System.out.println("Off!"); } else { System.out.println("Unrecognized button press!"); } } } public class Main { private static void showGUI() { … ButtonListener bl = new ButtonListener(); JButton onButton = new JButton ("On"); onButton.addActionListener(bl); content.add(onButton); JButton offButton = new JButton ("Off"); offButton.addActionListener(bl); content.add(offButton); …

9 cs205: engineering software Activating/Deactivating // in JButton: void setEnabled(boolean b) MODIFIES: this EFFECTS: If b, enables this. Otherwise, disables this.

10 cs205: engineering software class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { if (e.getActionCommand().equals ("On")) { System.out.println("On!"); } else if (e.getActionCommand().equals("Off")) { System.out.println("Off!"); } else { System.out.println("Unrecognized button press!"); } } } public class Main { private static void showGUI() { … ButtonListener bl = new ButtonListener(); JButton onButton = new JButton ("On"); onButton.addActionListener(bl); content.add(onButton); JButton offButton = new JButton ("Off"); offButton.addActionListener(bl); content.add(offButton); … Can we make clicking “On” enable the “Off” button (and vice versa)?

11 cs205: engineering software Inner Classes Added to JDK 1.1 (no JavaVM support) Define a class inside a scope It has access to variables in the containing scope –Including private instance variables!

12 cs205: engineering software public class Main { private static void showGUI() { JFrame frame = new JFrame("Swing GUI"); java.awt.Container content = frame.getContentPane(); content.setLayout(new FlowLayout()); final JButton onButton = new JButton ("On"); final JButton offButton = new JButton ("Off"); class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { if (e.getActionCommand().equals ("On")) { onButton.setEnabled(false); offButton.setEnabled(true); } else if (e.getActionCommand().equals("Off")) { onButton.setEnabled(true); offButton.setEnabled(false); } }; ButtonListener bl = new ButtonListener(); onButton.addActionListener(bl); content.add(onButton);

13 cs205: engineering software Anonymous Classes No need to give inner classes names! var = new Superclass ( ) { // override methods here }

14 cs205: engineering software public class Main { private static void showGUI() { JFrame frame = new JFrame("Swing GUI"); java.awt.Container content = frame.getContentPane(); content.setLayout(new FlowLayout()); final JButton onButton = new JButton ("On"); final JButton offButton = new JButton ("Off"); ActionListener bl = new ActionListener (){ public void actionPerformed (ActionEvent e) { if (e.getActionCommand().equals ("On")) { onButton.setEnabled(false); offButton.setEnabled(true); } else if (e.getActionCommand().equals("Off")) { onButton.setEnabled(true); offButton.setEnabled(false); } }; onButton.addActionListener(bl); content.add(onButton); What is the actual type of bl?

15 cs205: engineering software Not just Buttons Component JComponent AbstractButton JButton JToggleButton JCheckBoxJRadioButton

16 cs205: engineering software Menus Too... JComponent AbstractButton JButton JMenuItem JMenu JCheckboxMenuItem JRadioButtonMenuItem JMenuBar JPopupMenu Awkward design: JMenu is a button – action is to popup JPopupMenu JMenu contains a list of JMenuItem’s Why is JMenu a subtype of JMenuItem?

17 cs205: engineering software Concurrency in GUIs Responsiveness of GUI depends on multiple threads Swing thread types: –Initial threads (start program) –One event dispatch thread (all event- handling code) –Worker threads (do time-consuming tasks in background) Swing framework does most of the work – programmer doesn’t need to create threads

18 cs205: engineering software Event Dispatch Why is there only one event dispatch thread? Hint: did we need to synchronize? One event thread means all compute-intensive work should be done in worker threads. (Otherwise interface freezes like Rhocasa).

19 cs205: engineering software Worker Threads Create a background thread to do compute-intensive tasks SwingWorker is added to Java 6 (but we have Java 5). Need to download it from:

20 cs205: engineering software invokeLater import javax.swing.*; public class Main { private static void showGUI() {... } public static void main(String args[]) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { showGUI(); } }); } // in SwingUtilities: public static void invokeLater(Runnable doRun)Runnable EFFECTS: Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI.

21 cs205: engineering software Charge Projects: make progress! < 17 days until final reports are due Friday’s class: network programming