F27SB2 Software Development 2 Lecture 8: State Diagram & GUI Example.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Case Study Lecture 4 UML Huma Ayub Department of Software Engineering
F27SB2 Software Development 2 Lecture 7: State Diagrams.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
1 CS2200 Software Development Lecture 29: Polymorphism I A. O’Riordan, 2008 Based on notes by K. Brown.
Final Review. Review A Binary Search divides a sorted data structure into two equal parts and each time throws away the part that cannot contain the item.
Graphical User Interfaces
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary A Java Interface Iterators Using the ADT Dictionary A Directory of Telephone.
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
Address Book in JAVA By What is Address Book Address Book is book or database used for storing entries called contacts Each contact.
ADSA: Maps/ Advanced Data Structures and Algorithms Objectives – –examples of maps, and introduce map collection views Semester 2,
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Chapter 10 Exceptions and File I/O. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Exceptions Exception handling is an important aspect of object-oriented.
Simple Address Book Case Study Lecture 4 UML Huma Ayub Department of Software Engineering.
Consolidation. Code making (i.e. making ciphers) is undertaken by a cryptographer whereas cryptanalysts try to break ciphers in order to gain intelligence.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
F27SB2 Software Development 2 Lecture 6: Java GUIs 5.
MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 21 INTRODUCTORY PROGRAMMING Week 4 Lecture 2 Fonts –Horstmann 4.6 ActionEvents –different events.
F27SB2 Software Development 2 Lecture 4: Java GUIs 3.
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.
F27SA1 Software Development 1 9. Java Programming 8 Greg Michaelson.
GUI programming Graphical user interface-based programming.
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,
Exceptions Syntax, semantics, and pragmatics Exceptions1.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
F27SA1 Software Development Case study: Interactive Contacts System Greg Michaelson.
CS Lecture 01 Frames and Components and events Lynda Thomas
Chapter 18 Java Collections Framework
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 12: Programming Project.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Event Driven Programming. Event-driven Programming In the early days of computing communication with the outside world was accomplished using a technique.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
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.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Creating a GUI Class An example of class design using inheritance and interfaces.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
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.
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:
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
회원가입 - GUI ** 오지*.
Arrays Chapter 7.
Modular Event Handling
CompSci 230 S Programming Techniques
A Quick Java Swing Tutorial
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
A Quick Java Swing Tutorial
CiS 260: App Dev I Chapter 6: GUI and OOD.
Web Design & Development Lecture 6
Presentation transcript:

F27SB2 Software Development 2 Lecture 8: State Diagram & GUI Example

Overview software address book each entry consists of: – name – street – town – postcode – telephone number – address

Overview load entries from file save entries to file controls to: – move forwards/backwards through entries – add new entry – delete entry – change entry – quit

Overview inspect/ modify extend NEW/ clear data fields ADD/ insert new entry CHANGE/ modify entry; update display UP;DOWN/ update display.../ load file; display first entry QUIT/ update file

Design suggests: – JButton s for controls – JLabel s for headings/prompts – JTextField s for changeable details separate JPanel s for controls & details – may want to vary number of controls or details

Design NEW ADD CHANGE UP DOWN QUIT name street town postcode phone JFrame JPanel JButton s JLabel s JTextField s

Entry representation represent entry within program as a class 6 String fields use individual variables? advantage – can see which field is which disadvantage – need lots of individual assignments/accesses – might want to add fields – e.g. given/family names; country

Entry representation use array of string to hold details for each entry advantage – manipulate with for loops disadvantage – may forget which array element corresponds to which field – does this matter?

Entry representation class Entry { String [] details; final int MAXDETAILS = 6; public Entry(String [] newdetails) { details = new String[MAXDETAILS]; for(int i=0;i<MAXDETAILS;i++) details[i]=newdetails[i]; }

Entry representation outside program, keep Entry s in a text file save each Entry to file when program ends public void writeEntry(PrintWriter file) { for(int i=0;i<MAXDETAILS;i++) file.println(details[i]); }

Entry representation within program, hold Entry s in an array class Address extends JFrame implements ActionListener {... Entry [] entries; int entryno; final int MAXENTRIES = 100; identify file final String addressbook = "addressbook.txt";

Entry representation load array from file when program starts void readEntries() throws IOException { BufferedReader file; entries = new Entry[MAXENTRIES]; entryno=0; String [] details = new String[MAXDETAILS]; open file try { file = new BufferedReader (new FileReader(addressbook)); } catch(FileNotFoundException e){ return; }

Entry representation read 1st line from file => name for 1st Entry String line = file.readLine(); loop until file empty while(line!=null) {

Entry representation program will check for space in array before additions using New however, user may extend text file by hand outside program if(entryno==MAXENTRIES) { System. out.println("More than "+MAXENTRIES+ " entries."); System.exit(0); }

Entry representation set up name, read rest of details, build new Entry and add to array details[0]=line; for(int i=1;i<MAXDETAILS;i++) details[i]=file.readLine(); entries[entryno]=new Entry(details); entryno++;

Entry representation try to read next line of file => name for next Entry line = file.readLine(); } close file at end file.close();... } not quite enough for readEntries...

Interface & Initialisation require: – JButton for each control action array of JButton – text for JButton s array of String class Address extends JFrame implements ActionListener { JButton [] actions; String [] actionText = {“New”,”Add”,”Change”,”Delete”, “Forward”,”Back”,”Quit”}; final int MAXACTIONS = 7;

Interface & Initialisation NB need to remember JButton map – actions[0] => New – actions[1] => Add – actions[2] => Change – actions[3] => Delete – actions[4] => Forward – actions[5] => Back – actions[6] => Quit

Interface & Initialisation JLabel s for field titles – array of JLabel text for JLabel – array of String JLabel [] headings; String [] text ={“Name”,”Street”,”Town”,”Postcode”, “Telephone”,” ”};

Interface & Initialisation JTextField s for modifiable field details – array of JTextField JTextField [] details; final int MAXDETAILS = 6; JPanel s for Entry on interface and controls JPanel entry,controls;

Interface & Initialisation methods to initialise JLabel s, JButton s and JTextField s: JTextField setupTextField(String s,Container c) { JTextField t = new JTextField(s); t.setFont(new Font(“Sansserif”,Font.PLAIN,18)); t.setBackground(Color.white); c.add(t); t.addActionListener(this); return t; }

JButton setupButton(String s,Container c) { JButton b = new JButton(s); b.setFont(new Font(“Sansserif”,Font.PLAIN,18)); b.setBackground(Color.white); c.add(b); b.addActionListener(this); return b; } JLabel setupLabel(String s,Container c) { JLabel l = new JLabel(s,JLabel.CENTER); l.setFont(new Font(“Sansserif”,Font.PLAIN,18)); l.setBackground(Color.white); c.add(l); return l; } Interface & Initialisation

constructor initialises interface with empty details public Address() { entry = new JPanel(new GridLayout(MAXDETAILS,2)); headings = new JLabel[MAXDETAILS]; details = new JTextField[MAXDETAILS]; for(int i=0;i<MAXDETAILS;i++) { headings[i]=setupLabel(text[i],entry); details[i]=setupTextField(“”,entry); } add(“Center”,entry); Interface & Initialisation

controls = new JPanel(new GridLayout(MAXACTIONS,1)); actions = new JButton[MAXACTIONS]; for(int i=0;i<MAXACTIONS;i++) actions[i]= setupButton(actionText[i],controls);... add(“West”,controls); } Interface & Initialisation

method to display Entry from array in interface void showEntry(Entry e) { for(int i=0;i<MAXDETAILS;i++) details[i].setText(e.details[i]); } need to keep track of current Entry i.e. Entry on display in interface int current; Interface & Initialisation

at end of readEntries, need to set current Entry to first in array and display it... file.close(); if(entryno!=0) { current=0; showEntry(entries[0]); } Interface & Initialisation

write array back to file at end of program void doQuit() { if(entryno==0) System.exit(0); try { PrintWriter file = new PrintWriter(new FileWriter(addressbook)); for(int i=0;i<entryno;i++) entries[i].writeEntry(file); file.close(); System.exit(0); } catch(IOException e){}; } Quit control

Add controls use a pair of JButton s New – clear all details JTextField s – disable all JButton s apart from “Add” – user adds new details to empty JTextField s Add – copy details from JTextField s to new Entry – insert Entry into array in ascending name order – enable all JButton s and disable “Add”

Add controls disable Add in Address() at start actions[1].setEnabled(false); for New – check if space in array void doNew() { int i; if(entryno==MAXENTRIES) { System.out.println("More than "+ MAXENTRIES+" entries."); return; }

Add controls – disable all JButton s and enable Add actions[0].setEnabled(false); actions[1].setEnabled(true); for(i=2;i<MAXACTIONS;i++) actions[i].setEnabled(false); – set all JTextField s to “” for(i=0;i<MAXDETAILS;i++) details[i].setText(""); }

Add controls for Add… – get details from JTextField s and make new Entry void doAdd() { int i,j; String [] newdetails = new String[MAXDETAILS]; for(i=0;i<MAXDETAILS;i++) newdetails[i]=details[i].getText(); Entry e = new Entry(newdetails);

Add controls – search array to find first with name > new Entry ’s name for(i=0;i<entryno;i++) if(e.details[0].compareTo(entries[i].details[0])<0) break; – move all Entry s down one place in array for(j=entryno;j>i;j--) entries[j]=entries[j-1];

Add controls – put new Entry in place, update count and remember new as current entries[i]=e; current=i; entryno++; – enable all JButton s and disable Add actions[0].setEnabled(true); actions[1].setEnabled(false); for(i=2;i<MAXACTIONS;i++) actions[i].setEnabled(true); }

Move controls for Forward... – check if at end of array – increment current and display Entry void doForward() { if(entryno==0 || current+1==entryno) return; current++; showEntry(entries[current]); }

Move controls for Back… – check if at start of array – decrement current and display Entry void doBack() { if(entryno==0 || current==0) return; current--; showEntry(entries[current]); }

Delete control for Delete… – ignore if no Entry s void doDelete() { if(entryno==0) return; – move all Entry s after current back one place in array and decrement number of Entry s for(int i=current;i<entryno-1;i++) entries[i]=entries[i+1]; entryno--;

Delete control – if no Entry s then set all interface details to “” if(entryno==0) { for(int i=0;i<MAXDETAILS;i++) details[i].setText(""); return; } – if deleted Entry was last Entry then reset current to new last Entry if(current==entryno) current--;

Delete control – display Entry in current position usually Entry following deleted Entry showEntry(entries[current]); }

Change control for Change… – user can alter any JTextField at any time – change only takes effect if Change selected – can’t just copy details back to current – if name changes then entry order changes – delete current entry – can’t use doDelete - updates text fields – add details from text fields as new entry - use doAdd

Change control void doChange() { if(entryno==0) return; for(int i=current;i<entryno-1;i++) entries[i]=entries[i+1]; entryno--; if(entryno==0) { for(int i=0;i<MAXDETAILS;i++) details[i].setText(""); return; } if(current==entryno) current--; doAdd(); }

Change control void doChange() { if(entryno==0) return; for(int i=current;i<entryno-1;i++) entries[i]=entries[i+1]; entryno--; if(entryno==0) { for(int i=0;i<MAXDETAILS;i++) details[i].setText(""); return; } if(current==entryno) current--; doAdd(); }

actionPerformed int state; final int INSPECT = 0; final int EXTEND = 1; public void actionPerformed(ActionEvent e) { switch(state) { case INSPECT: if(e.getSource()==actions[0]) // NEW { doNew(); state = EXTEND; return; } if(e.getSource()==actions[2]) // CHANGE { doChange(); return; } if(e.getSource()==actions[3]) // DELETE { doDelete(); return; }

actionPerformed if(e.getSource()==actions[4]) // UP { doForward(); return; } if(e.getSource()==actions[5]) // DOWN { doBack(); return; } if(e.getSource()==actions[6]) // QUIT { doQuit(); return; } case EXTEND: if(e.getSource()==actions[1]) // ADD { doAdd(); state = INSPECT; return; } }

main class TestAddress { public static void main(String [] args) throws IOException { Address a; a = new Address(); a.setSize(600,280); a.setTitle("Address"); a.setVisible(true); a.addWindowListener (new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); a.readEntries(); a.state = a.INSPECT; }

Program

Improvements/extensions 1.add a JLabel to display system messages 2.after NEW, check that all requisite fields have been filled in before ADD can succeed 3.add a search facility 4.check for duplicate entries before CHANGE or ADD can succeed