Chapter 4© copyright Janson Industries 20141 More GUI ▮ Events, Actions, and Listeners ▮ The Visual Editor ▮ More GUI component properties Non-graded assg.

Slides:



Advertisements
Similar presentations
Java Graphical User Interface (GUI) using Visual Editor in eclipse CSI 1390 – Java Programming Instructor: Saeid Nourian University.
Advertisements

Step-by-Step: Add a Graphical Hyperlink USE the Special Events Final presentation that is still open from the previous exercise. 1.Go to slide 4, and click.
Graphic User Interfaces Layout Managers Event Handling.
Chapter 1: An Introduction to Visual Basic 2012
Using Macros and Visual Basic for Applications (VBA) with Excel
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
CREATING A MULTIPLE PAGE REPORT Presented by: Dr. Ennis-Cole.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
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.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Microsoft Visual Basic 2012 CHAPTER TWO Program and Graphical User Interface Design.
Chapter 4 Code Editor Goals and Objectives Program more efficiently? How can you speed up your development process? Do you want to learn useful shortcuts.
Tutorial 6 Using Form Tools and Creating Custom Forms
® Microsoft Access 2010 Tutorial 6 Using Form Tools and Creating Custom Forms.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Chapter 1a© copyright Janson Industries RAD ▮ Explain Eclipse, RAD, and their relationship ▮ Basic RAD concepts ▮ How to install RAD ▮ How to import.
European Computer Driving Licence Syllabus version 5.0 Module 4 – Spreadsheets Chapter 22 – Functions Pass ECDL5 for Office 2007 Module 4 Spreadsheets.
© Paradigm Publishing, Inc Access 2010 Level 2 Unit 1Advanced Tables, Relationships, Queries, and Forms Chapter 4Creating and Using Custom Forms.
© copyright Janson Industries 2014
Using Form Tools and Creating Custom Forms Microsoft Access 2010.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Chapter 8: Writing Graphical User Interfaces
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 1© copyright Janson Industries Java ▮ Java programming concepts ▮ Downloading the JDK ▮ Java coding and executing example This presentation.
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
Microsoft Visual Basic 2005 CHAPTER 7 Creating Web Applications.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
COMPREHENSIVE Access Tutorial 6 Using Form Tools and Creating Custom Forms.
IC 3 BASICS, Internet and Computing Core Certification Key Applications Lesson 11 Organizing the Worksheet.
Chapter 5 Quick Links Slide 2 Performance Objectives Understanding Framesets and Frames Creating Framesets and Frames Selecting Framesets and Frames Using.
1 Intro to Programming & Algorithm Design GUI Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch11.IntrotoProg.GUI.ppt.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 15 Advanced Tables.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. M I C R O S O F T ® Revising Presentation Text Lesson 3.
Chapter 2 – Introduction to the Visual Studio .NET IDE
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Object Oriented Programming.  Interface  Event Handling.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
Foundation year Practical Lec. 4:Practical Lec. 4: Presentation Software Using Microsoft Office 2007 Practical Lec. 4:Practical Lec. 4: Presentation Software.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Chapter 12© copyright Janson Industries Java Server Faces ▮ Explain the JSF framework ▮ SDO (service data objects) ▮ Facelets ▮ Pagecode classes.
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.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 1a© copyright Janson Industries RAD ▮ Explain Eclipse, RAD, and their relationship ▮ Basic RAD concepts ▮ How to install RAD ▮ How to import.
CSC 205 Programming II Lecture 5 AWT - I.
Chapter 1: An Introduction to Visual Basic .NET
Chapter 2: The Visual Studio .NET Development Environment
CHAPTER Reacting to the user.
Chapter 1: An Introduction to Visual Basic 2015
Chapter 2 – Introduction to the Visual Studio .NET IDE
Event-driven programming for GUI
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
DREAMWEAVER MX 2004 Chapter 3 Working with Tables
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

Chapter 4© copyright Janson Industries More GUI ▮ Events, Actions, and Listeners ▮ The Visual Editor ▮ More GUI component properties Non-graded assg part1 part2part1part2

Chapter 4© copyright Janson Industries WindowListener ▮ A class that reacts to window events ▮ Window events ▮ Clicking X to close the window ▮ Resizing the window ▮ This is another one of those JDK classes that makes your programming life easier!

Chapter 4© copyright Janson Industries WindowListener ▮ For a class to be a WindowListener it must: import java.awt.event.WindowEvent; import java.awt.event.WindowListener; ▮ In the class header have: … implements WindowListener {

Chapter 4© copyright Janson Industries WindowListener ▮ Also, the WindowListener must have these seven methods : public void windowClosing(WindowEvent e) { System.exit(0); } public void windowClosed(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} Ends the JVM

Chapter 4© copyright Janson Industries WindowListener ▮ Two ways you can do this: ▮ 1. Create a separate class to act as the window listener ▮ Then in the frame class: ▮ Create the window listener object ▮ Add the window listener object to the frame this.addWindowListener(new WindowListener()); ▮ 2. Have the frame implement WindowListener ▮ Add the frame to itself this.addWindowListener(this);

Chapter 4© copyright Janson Industries WindowListener ▮ extends relates two classes as super/sub classes ▮ implements defines the class as a WindowListener (in addition to whatever the extends specifies, e.g. Frame) ▮ addWindowListener makes the WindowListener part of the frame ▮ Just like creating & adding a label to the frame

Chapter 4© copyright Janson Industries WindowListener Create a separate class to act as the window listener Must import WindowEvent and implement Window Listener

Chapter 4© copyright Janson Industries WindowListener Create the window listener object Add the window listener object to the frame

Chapter 4© copyright Janson Industries Add the frame to itself Option 2: Define the frame as a WindowListener Have the frame import and implement WindowListener Either way, close button will now work

Chapter 4© copyright Janson Industries Superclass/subclass ▮ Problem with option 2 ▮ Every frame must have code ▮ Alternative 3 rd solution ▮ Use inheritance/specialization to avoid coding these methods multiple times ▮ Have a superclass with needed code ▮ Advantages: ▮ Save programmer time ▮ Less chance of error ▮ Easier to modify ▮ P.S. Inherited methods and variables can be overridden in subclass (more on this later)

Chapter 4© copyright Janson Industries Visual Editor ▮ Create a new CustomerFrame class that implements WindowListener ▮ CustomerFrame constructor will expect a Customer variable ▮ CustomerFrame retrieves info from Customer object and puts into labels ▮ Will create a Visual Class and use the Visual Editor

Chapter 4© copyright Janson Industries New Customer Example CustApp Customer Object CustomerFrame Object Create a Customer object, assign to variable c, set object’s properties Create a CustomerFrame object, pass c, assign to variable cf Retrieve & display Customer object properties

Chapter 4© copyright Janson Industries Visual Class ▮ Click File, New, Visual Class

Chapter 4© copyright Janson Industries Specify CustomerFrameUse the Style box to specify Frame as superclass Have RAD generate main and window event handling methods Implement by clicking Add

Chapter 4© copyright Janson Industries Start typing WindowListener, select WindowListener from the AWT package, click OK

Chapter 4© copyright Janson Industries Will use main method for testing. Click Finish.

Chapter 4© copyright Janson Industries import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class CustomerFrame extends Frame implements WindowListener { private static final long serialVersionUID = 1L; public void windowOpened(WindowEvent e) {} public void windowClosing(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public static void main(String[] args) {} public CustomerFrame() { super(); initialize(); } private void initialize() { this.setSize(300, 200); this.setTitle("Frame"); } Initialize method used to set frame properties and add components VE initially generates this code

Chapter 4© copyright Janson Industries Design pane Source pane Resize Open Component Palette

Chapter 4© copyright Janson Industries Visual Editor ▮ Components stored in trays ▮ Click on tray to open/close ▮ Click on component to select ▮ Then click on design pane to add selected component ▮ Click on source pane to edit source code

Chapter 4© copyright Janson Industries Change all properties in Properties pane Enter/change source code Open/close trays Select a component Add components and change some properties in design pane

▮ Make frame visible and layout null by adding this source code into initialize ▮ Or change in Properties view Chapter 4© copyright Janson Industries Visual Editor this.setLayout(null); this.setVisible(true);

Chapter 4© copyright Janson Industries Click Label in palette then click on design pane to add Should give the Label variable a meaningful name

Chapter 4© copyright Janson Industries Or accept the default Label's source code and property values are displayed

Chapter 4© copyright Janson Industries Visual Editor ▮ Modify component in design pane, properties pane, or source code ▮ VE does not name components well (i.e. label, label1, label2, etc.) ▮ Right click component and select rename field

Chapter 4© copyright Janson Industries Specify new name, click OK, and save all modified resources VE changes all the source code

Chapter 4© copyright Janson Industries

Chapter 4© copyright Janson Industries Labels ▮ Lots of label properties ▮ Foreground and background colors ▮ Size, Location ▮ Font style ▮ Alignment ▮ Some nice VE features to define labels

Chapter 4© copyright Janson Industries Labels ▮ Select many components by ▮ Holding CTRL key and clicking each OR ▮ Change pointer from selection to marquee then click and drag around all the components

Chapter 4© copyright Janson Industries Click Marquee and pointer changes from arrow to +...to here, so all components are inside the dotted line box Click here and drag... I’ve added 3 more labels

Chapter 4© copyright Janson Industries When released all components selected, any changes will change all Alignment and size changes according to the anchor component Alignment button brings up

Chapter 4© copyright Janson Industries Anchor ▮ When selecting by Marquee, it is the newest component ▮ When selecting by Selection, it is the last component selected ▮ Anchor component indicated with solid resize handles

Chapter 4© copyright Janson Industries Changed pointer to Selection, then reselected so oldest label is anchor (held CTRL key, clicked each label) Click left align

Chapter 4© copyright Janson Industries Resize all by dragging any label resize handle

Chapter 4© copyright Janson Industries Need to align labels vertically Click distribution box then distribute vertically

Chapter 4© copyright Janson Industries Distributes evenly within the box. Make distribution box larger, distribute vertically again

Chapter 4© copyright Janson Industries Drag labels to center on frame Click distribution box button again (to deselect), then close Customize Layout window

Chapter 4© copyright Janson Industries Visual Editor ▮ Rename the labels from top to bottom ▮ custNameLbl, shipToLbl1, shipToLbl2, contactInfo ▮ Make sure custNameLbl is on top ▮ Add code to: ▮ receive a Customer variable ▮ move the info from the Customer object into the labels

Chapter 4© copyright Janson Industries Visual Editor ▮ Need to create a class variable of type Customer ▮ private Customer customer = null; ▮ Change constructor to accept a Customer variable ▮ public CustomerFrame(Customer cust) { ▮ In constructor, move method variable (cust) to class variable (customer) – why? ▮ customer = cust;

Chapter 4© copyright Janson Industries Change CustApp so that a CustomerFrame object is created and change Customer Frame so that info is displayed like this Enable the Window Close button Non-graded assg:

Chapter 4© copyright Janson Industries Visual Editor Probs ▮ “Error trying to set new file into editor” ▮ 1) Bring up a VE session and close it. Make sure no VE's are open. 2) Click Project, then Clean..., then click Clean All Projects radio button, then OK button 3) Wait for build to complete. 4) Try VE again. It should work.

Chapter 4© copyright Janson Industries Visual Editor Probs ▮ When creating, Visual Class not an option ▮ When RAD is installed the Visual Editor is not in the default configuration ▮ You must change the configuration to include the Visual Editor ▮ Go back to the Ch01a slides ▮ Slides 37 – 46 show how to change the configuration to include VE

Chapter 4© copyright Janson Industries ActionListener ▮ “Reacts” to actions on buttons and lists ▮ Actions ▮ Clicking a button ▮ Clicking a list item ▮ Need to add: import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

Chapter 4© copyright Janson Industries ActionListener ▮ In the class header, define class as an ActionListener by adding: …extends Frame implements ActionListener { ▮ Add the ActionListener to a component: ▮ myButton.addActionListener(this); ▮ In the ActionListener class must define an actionPerformed method ▮ This method is executed when an action occurs

Chapter 4© copyright Janson Industries CustomerFrame1 ▮ Create a visual class CustomerFrame1 ▮ Implement both action and window listeners ▮ Has a button that when pressed will display the data

Chapter 4© copyright Janson Industries CustomerFrame1 Initially displayed with no data Click Display button…

Chapter 4© copyright Janson Industries CustomerFrame1 … info displayed in labels

Chapter 4© copyright Janson Industries

Chapter 4© copyright Janson Industries import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class CustomerFrame1 extends Frame implements WindowListener, ActionListener { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent e) {} public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} : ::: public void windowOpened(WindowEvent e) {} public static void main(String[] args) {} public CustomerFrame1() { super(); initialize(); } private void initialize() { this.setSize(300, 200); this.setTitle("Frame"); } RAD took care of imports and class header Took care of actionPerformed and window methods Initialize sets Frame props and (as added) component properties

Chapter 4© copyright Janson Industries CustomerFrame1 ▮ Notice that the “window” methods don’t do anything ▮ Need to add code to remove window this.dispose(); ▮ “Code a little, test a little” ▮ Verify close button works by running app ▮ In main, instantiate a CustomerFrame1 object ▮ Then run CustomerFrame1 as application

Chapter 4© copyright Janson Industries CustomerFrame1 ▮ Nothing happens – why not? ▮ Never set Frame visible ▮ Wish I had created a useful frame superclass that automatically did that! ▮ Add code to set visible then ▮ Run CustomerFrame1 as application ▮ Test close button

Chapter 4© copyright Janson Industries Click Frame to select and display properties In properties pane, click visible’s drop down button and select true

Chapter 4© copyright Janson Industries import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class CustomerFrame1 extends Frame implements ActionListener, WindowListener{ public void actionPerformed(ActionEvent arg0) {} public void windowOpened(WindowEvent arg0) {} public void windowClosing(WindowEvent arg0) { this.dispose(); } : : : : : public void windowDeactivated(WindowEvent arg0) {} public static void main(String[] args) { CustomerFrame1 cf1 = new CustomerFrame1(); } public CustomerFrame1() { super(); initialize();} private void initialize() { this.setSize(300, 200); this.setTitle("Frame"); this.setVisible(true); } VE added to source You need to add to source

Chapter 4© copyright Janson Industries CustomerFrame1 ▮ If run and close button clicked, nothing happens – why not? ▮ Never added WindowListener to the frame ▮ This is why you should test often: catch errors early!! ▮ Add code in initialize() to add WindowListener and test this.addWindowListener(this);

Chapter 4© copyright Janson Industries CustomerFrame1 ▮ Change source code so that ▮ Class Customer variable called customer is created ▮ Constructor: ▮ accepts a Customer variable called cust ▮ assigns cust to the class variable customer ▮ Why? ▮ So actionPerformed() can access the Customer object

Chapter 4© copyright Janson Industries public class CustomerFrame1 extends Frame implements ActionListener, WindowListener { private Customer customer = null; : : : : public CustomerFrame1(Customer cust) { super(); customer = cust; initialize(); } : : : : } CustomerFrame1 Added

Chapter 4© copyright Janson Industries Get an error: why? How fix?

Chapter 4© copyright Janson Industries CustomerFrame1 ▮ Our test in main does not pass the required Customer variable ▮ In main must: ▮ Instantiate a Customer object and assign to a Customer variable ▮ Pass Customer variable to CustomerFrame1

Chapter 4© copyright Janson Industries public static void main(String[] args) { Customer cust = new Customer(); cust.setCustName("Thrifty Food"); cust.setShipToStreet("1 Diet Lane"); cust.setShipToCity("Fasting"); cust.setShipToState("IN"); CustomerFrame1 cf1 = new CustomerFrame1(cust); } Added Changed CustomerFrame1 ▮ Let’s test that customer object is accessible ▮ In CustomerFrame1's constructor, add code to display customer name in the console then run as app System.out.println(customer.getCustName());

Chapter 4© copyright Janson Industries Frame is displayed Name is displayed in the console Make sure window close button works

Chapter 4© copyright Janson Industries CustomerFrame1 ▮ Add labels to hold info ▮ Add button to frame using VE ▮ Define button properties ▮ Including good names ▮ Tie button to ActionListener ▮ Code actionPerformed to retrieve info from Customer object and put in labels

Chapter 4© copyright Janson Industries Copy the Label definition statements from CustFrame into CustomerFrame1

Chapter 4© copyright Janson Industries RAD inserts the import statement

Chapter 4© copyright Janson Industries Delete the System.out.println statement and set the layout to null

Chapter 4© copyright Janson Industries Switch to the design pane Notice the labels are not on the frame

Chapter 4© copyright Janson Industries Drag/drop the four labels Increase the width of the labels and the height of the frame to 250

Chapter 4© copyright Janson Industries Define the button properties so it looks like this Click on the AWT button icon Click on frame to add the button

Chapter 4© copyright Janson Industries Let’s test: add to the actionPerformed method: System.out.println("listener is working"); run the CustomerFrame1, click the Display button For more complicated GUI components, RAD creates a get method that sets all the properties Add the actionListener to the button in the get button method

Chapter 4© copyright Janson Industries Click Display button Text should appear

Chapter 4© copyright Janson Industries CustomerFrame1 ▮ Code actionPerformed to retrieve info from Customer object and put in labels ▮ Use Customer object’s getters and concatenation to build label text ▮ Can cut and paste CustFrame code into actionPerformed to overwrite the println statement

Chapter 4© copyright Janson Industries

Chapter 4© copyright Janson Industries Must change cust. to customer. Can't do Replace All because main uses cust.

Chapter 4© copyright Janson Industries

Chapter 4© copyright Janson Industries Run, click Display Why does data appear like this?

Chapter 4© copyright Janson Industries public static void main(String[] args) { Customer cust = new Customer(); cust.setCustName("Thrifty Food"); cust.setShipToStreet("1 Diet Lane"); cust.setShipToCity("Fasting"); cust.setShipToState("IN"); CustomerFrame1 cf1 = new CustomerFrame1(cust); } Only these were set CustomerFrame1 ▮ In main, the test data wasn’t complete ▮ Only some properties were set

Chapter 4© copyright Janson Industries Change CustApp to create a CustomerFrame1 object Run CustApp

Chapter 4© copyright Janson Industries Click Display button Notice that the JVM automatically redisplays frame after actionPerformed method

Chapter 4© copyright Janson Industries New Customer Example CustApp Customer Object CustomerFrame1 Object Creates Customer object, assigns to variable c, sets object’s properties Creates a CustomerFrame1 object, passes c, assigns to variable cf Retrieves & displays Customer object properties Non-graded assg: Do it!

Chapter 4© copyright Janson Industries Non-graded Assg ▮ Export as one jar file ▮ Customer.java ▮ CustomerFrame.java ▮ CustomerFrame1.java ▮ CustApp.java ▮ the jar file as an attachment to