Object-Oriented Software Engineering

Slides:



Advertisements
Similar presentations
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Advertisements

Graphical User Interface Bonus slides Interaction Between Components & Drawing.
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 Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
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.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.
Chapter 121 Window Interfaces Using Swing Chapter 12.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington User Interface COMP 112 #30.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
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,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Copyright © 2013 by John Wiley & Sons. All rights reserved. GRAPHICAL USER INTERFACES CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11.
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.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Layout Managers Arranges and lays out the GUI components on a container.
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.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-2 ( Book Chapter 14) GUI and Event-Driven Programming.
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.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
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,
Event Handling CS 21a: Introduction to Computing I First Semester,
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
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.
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.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Modular Event Handling
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Chapter 8 Event Handling.
CompSci 230 S Programming Techniques
CompSci 280 S Introduction to Software Development
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
CSE 114 – Computer Science I Event Programming
Ellen Walker Hiram College
Event-driven programming for GUI
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Basic Elements of The GUI
Object-Oriented Software Engineering
Introduction to Event Handling
PC02 Consolidation %WITTY_QUOTE%. PC02 Consolidation %WITTY_QUOTE%
Chapter 7-2 (Book Chapter 14)
CS288 Lab Exercise 2.
Component-Based Software Engineering
Window Interfaces Using Swing Objects Chapter 12
Chapter 7-2 (Book Chapter 14)
Graphical User Interface
Presentation transcript:

Object-Oriented Software Engineering PersonGui (Mark 1) Case Study

Contents The PersonGui (Mark1) The The Person Gui (Mark1) ToolBar PersonGui Constructor Adding Buttons Making Buttons Handling Events

The Person Gui (Mark1): Use Cases user Define Age Define Height Display Attributes

The Person Gui (Mark1): Class Diagram <<interface>> ActionListener 1 JPanel PersonGui class has caused the JPanel (which it extends) to be linked to a JToolBar JTextArea 1 PersonGui 1 JToolBar 1 JTextField 1 1 JFrame 1 1 Person private Double height private Double age void setAge (String) void setHeight (String) String getAge () String getHeight () String Attributes_Description ()

Person private Double height private Double age void setAge (String) public void setAge (String stringAge) { age = new Double(stringAge); } public void setHeight (String stringHeight) { height = new Double(stringHeight); } Person private Double height private Double age void setAge (String) void setHeight (String) String getAge () String getHeight () String Attributes_Description () public String getAge () { return age.toString(); } public String getHeight () { return height.toString(); } public String Attributes_Discription () { return "This Person has height " + getHeight() + " and has age " + getAge() + "\n"; }

The Person Gui (Mark1) JFrame JTextArea JToolBar JTextField

PersonGui Constructor: JToolBar (Full code available from course web site) public PersonGui() { super(new BorderLayout()); JToolBar toolBar = new JToolBar("Still draggable"); addButtons(toolBar); toolBar.setFloatable(false); toolBar.setRollover(true); textArea = new JTextArea(5, 30); textArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(textArea); setPreferredSize(new Dimension(750, 430)); add(toolBar, BorderLayout.PAGE_START); add(scrollPane, BorderLayout.CENTER); }

PersonGui Constructor super(new BorderLayout()); Causes the enclosed code to act on the super-class that extends PersonGui. In general for a class extension can invoke methods of parent (super) class via super.method(x,y,z)

Here's what happens if we don't call super. public PersonGui() { //super(new BorderLayout()); new BorderLayout(); //Create the toolbar. ................................ BorderLayout now attached to toolbar, not JPanel

PersonGui Constructor Internal method call JToolBar toolBar = new JToolBar("Still draggable"); addButtons(toolBar); toolBar.setFloatable(false); toolBar.setRollover(true); If the rollover state is true then border of the toolbar buttons will be drawn only when the mouse pointer hovers over them.

toolBar.setFloatable(true);

toolBar.setRollover(true);

addButtons method Internal method call causes actions on text field protected void addButtons(JToolBar toolBar) { JButton button = null; // add first button button = makeNavigationButton(PRINT_ATTRIBUTES, "Prints a String containing ageString and heightString", "Print Attributes"); toolBar.add(button); // add second button button = makeNavigationButton(AGE, "Sets the value of ageString", "Define Age"); toolBar.addSeparator(); textField.setColumns(10); textField.addActionListener(this); textField.setActionCommand(TEXT_ENTERED); toolBar.add(textField); } Internal method call causes actions on text field to be notified to 'this' (i.e PersonGui class)

makeNavigationButton method protected JButton makeNavigationButton(String actionCommand, String toolTipText, String altText) { //Create and initialize the button. JButton button = new JButton(); button.setActionCommand(actionCommand); button.setToolTipText(toolTipText); button.addActionListener(this); button.setText(altText); return button; } causes actions on button to be notified to 'this'

button.setActionCommand(AGE); button press causes an ActionEvent object to be created, with an action command string set to AGE button.setToolTipText("Sets the value of ageString"); when mouse hovers over button this text will appear

button.addActionListener(this); creates an ActionListener that is attached to the PersonGui class

Handling Events: Implementing interface public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); String description = null; if (PRINT_ATTRIBUTES.equals(cmd)) { //first button clicked String text = dude.Attributes_Discription(); description = text + newline } else if (AGE.equals(cmd)) { // second button clicked ageString = textField.getText(); dude.setAge(ageString); description = "The age of the person in: " + ageString + newline; textField.setText(""); } else if (HEIGHT.equals(cmd)) { // third button clicked ................................ } else if (TEXT_ENTERED.equals(cmd)) { // text field } displayResult(description);

actionPerformed(ActionEvent e) button:JButton PersonGui addActionListener(this) left_mouse_click e:Action Event <<create>> notify(e ) String cmd = e.getActionCommand();

actionPerformed(ActionEvent e) else if (AGE.equals(cmd)) { // second button clicked ageString = textField.getText(); dude.setAge(ageString); description = "The age of the person in: " + ageString + newline; textField.setText(""); } :PersonGui textField: dude:Person getText() ageString setAge(ageString); setText("")

PersonGui textField: dude:Person else if (HEIGHT.equals(cmd)) { // third button clicked heightString = textField.getText(); dude.setHeight(heightString); description = "The height of the person is: " + heightString + newline; textField.setText(""); } PersonGui textField: dude:Person getText() heightString setHeight(heightString); setText("")

PersonGui dude:Person if (PRINT_ATTRIBUTES.equals(cmd)) { //first button clicked String text = dude.Attributes_Discription(); description = text + newline; } PersonGui dude:Person Attributes_Discription( ) text

displayResult method Makes sure next append will protected void displayResult(String actionDescription) { textArea.append(actionDescription + newline); textArea.setCaretPosition(textArea.getDocument().getLength()); } Adds text after caret current position Makes sure next append will occur at the end of document.