...that can get messy when we have lots buttons!

Slides:



Advertisements
Similar presentations
Inner Classes. Nested Classes  An nested class is a class that is defined inside another class.  To this point we have only studied top-level classes.
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.
Access to Names Namespaces, Scopes, Access privileges.
COMP 14 Introduction to Programming Miguel A. Otaduy June 8, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 21, 2005.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Inner Classes. Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class Inner classes.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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,
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/20) More Swing Widgets and Listeners Joel Adams and Jeremy Frens Calvin.
Copyright © Curt Hill First Windows Program GUIs in Eclipse.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
Java Inner Classes. Interfaces interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Nested Classes CompSci 230 S Software Construction.
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.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Event Handling CS 21a: Introduction to Computing I First Semester,
Object Oriented Programming in Java Habib Rostami Lecture 10.
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.
Lecture 15.1 Event Delegation.
Modular Event Handling
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
CompSci 230 S Programming Techniques
Inner Classes 27-Dec-17.
CompSci 230 S Software Construction
A First Look at GUI Applications
Android – Event Handling
GUI III IS
CompSci 230 S Programming Techniques
Java Programming: From Problem Analysis to Program Design,
Lambdas ---anonymous functions---
Event Handling CS 21a: Introduction to Computing I
Event-driven programming for GUI
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Introduction to Event Handling
Inner Classes 29-Nov-18.
Web Design & Development Lecture 13
Java Inner Classes.
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
Inner Classes 11-May-19.
CiS 260: App Dev I Chapter 6: GUI and OOD.
Inner Classes 18-May-19.
Inner Classes.
Inner Classes 25-Oct-19.
Presentation transcript:

...that can get messy when we have lots buttons! public void actionPerformed (ActionEvent a) { // Get the button string. String s = a.getActionCommand(); if (s.equalsIgnoreCase ("Bold")) { // ... } else if (s.equalsIgnoreCase ("Italic")) { else if (s.equalsIgnoreCase ("Right Justify")) {

What else can we do? Having one giant event handler function is messy Need to be careful that a change to one button won't break code for another Implementing lots of classes is also undesirable What is a key limitation of using separate classes? What can you do in NewFrame2 but not in a separate class? We want to use OOP principles! Compartmentalize functionality Reuse code instead of copy/pasting Isolate and protect data

Options... We could create custom classes just for handling the events class QuitButtonHandler implements ActionListener { public void actionPerformed (ActionEvent a) { System.out.println ("ActionPerformed!"); } class NewFrame extends JFrame { public NewFrame () { // ... quitButton.addActionListener(new QuitButtonHandler); randButton.addActionListener(new RandomButtonHandler); }

What if the event handler needs access to data from NewFrame? Problem??? We could create custom classes just for handling the events class QuitButtonHandler implements ActionListener { public void actionPerformed (ActionEvent a) { System.out.println ("ActionPerformed!"); } What if the event handler needs access to data from NewFrame? class NewFrame extends JFrame { public NewFrame () { // ... quitButton.addActionListener(new QuitButtonHandler); randButton.addActionListener(new RandomButtonHandler); }

What we really want: To isolate the event handler for each object To allow the event handlers to access the data of the class they are in

What we really want: To isolate the event handler for each object but a single class can only implement the functions in an interface once! To allow the event handlers to access the data of the class they are in but if we use separate classes for each event handler we won't be able to do this! Oh noes! :(

Inner Classes Java to the rescue! Use an Inner Class class myClass { } class myInnerClass { void someFunc() { }

Inner Classes Use an Inner Class class myPanel { } private JLabel myLabel; } class eHandler1 implements ActionListener { myLabel.setText("Handler 1!"); } class eHandler2 implements ActionListener { } class eHandler3 implements ActionListener { }

What can it do? Can an inner class touch its outer's privates? Yes it can! Can an "outer" class call functions in the inner? Can an inner class implement an interface? Can an inner class extend another class? Can an inner class access local variables in outside functions? No it can’t!

Sample Code Check out guis.inner.InnerTest.java Note: Todo: The inner class can have: functions, data, and constructors The inner class can access private data of its outer class The outer class can access private data of the inner class Todo: Make the outer class print out the values of X and Y

Inner Class Event Handlers Look at guis.inner.InnerEvents.java We want to have: Quit: quits Hello: display "hello" World: prints "bye" (in the msg JLabel) The quit button currently uses an inner class Your turn: Add two new inner classes for Hello and Bye Elite Hacker: Combine your two inner classes into a single inner class Bye

Types of Classes in Java A public/private class Must have name equal to file A class with no privacy modifier Only usable within that package An inner class inside of another class Inner can access the outer and vice versa An anonymous inner class Declared as part of a function call quitB.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent a) { System.exit (0); } } ); … and a few other types too!