Swing Threading Nested Classes COMP 401 Fall 2014 Lecture 23 11/25/2014.

Slides:



Advertisements
Similar presentations
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Nested Classes Yoshi Modified from:
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Java Threads Part II. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes, Encapsulation, Methods and Constructors
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
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.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 8: Inner Classes. You're an OO programmer, so you know that for reuse and flexibility/extensibility you need to keep your classes specialized.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Encapsulation COMP 401, Fall 2014 Lecture 06 9/4/2014.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Parts of JAVA 1www.gowreeswar.com. Features of JAVA 2www.gowreeswar.com.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
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.
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.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Nested Classes CompSci 230 S Software Construction.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Generic Programming and Inner classes ge·ner·ic 1a : relating or applied to or descriptive of all members of a genus, species, class, or group : common.
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 in Java Habib Rostami Lecture 10.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
Review IS Overview: Data  Inside the application Collections  Outside the application Database XML  Getting/displaying Swing  Communicating.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Inner Classes 27-Dec-17.
CompSci 230 S Software Construction
Classes (Part 1) Lecture 3
CompSci 230 S Programming Techniques
Introduction to Object-oriented Program Design
Nested class.
Anonymous Classes A short-cut for defining classes when you want to create only one object of the class.
Inner Classes 29-Nov-18.
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
Chapter 8 Classes User-Defined Classes and ADTs
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Using threads for long running tasks.
Inner Classes 1-May-19.
CSE 341 Lecture 11 b closures; scoping rules
Inner Classes 11-May-19.
Inner Classes 18-May-19.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Inner Classes.
Creating and Using Classes
Inner Classes 25-Oct-19.
Threads and concurrency / Safety
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Swing Threading Nested Classes COMP 401 Fall 2014 Lecture 23 11/25/2014

Swing and multithreading Swing is NOT multithreaded – After being set up, the only thread that can safely interact with Swing components is the Swing event thread. lec23.v1 – Skeleton for timer application lec23.v2 – Badly written timer application – Creates a background thread that is supposed to periodically update user interface. – Seems to work OK But may not always work. And Swing documentation specifically says don’t do this.

SwingUtilities.invokeLater() Static helper method for properly asking Swing event thread to do something. – Takes a Runnable object as parameter – Does not create a new thread. Schedules execution with existing Swing event thread. lec23.v3

Nested Classes Think about role of WidgetUpdater in previous example. – Class exists only to be a helper for BackgroundTimer Java provides “nested” classes in order to support this kind of programming need. – Inner classes Class defined within another class. – Local classes Class defined within a method. – Anonymous classes Unnamed implementation of an interface that can be used as an expression.

Inner Class A class within a class. – If “public”, then instances can be made outside of the outer class. – If “private”, then instances can only be made within the outer class. This is a good way to create helper classes that you don’t want to expose outside of the class. lec23.v4

Local Class Class declared within a instance method of the outer class. Later in the method, you can create instances of the local class. Instances of local class has access to the encapsulated fields of the outer class instance. lec23.v5

Anonymous Classes Define and create an object instance that implements an interface without having to formally define a class. – Special syntax evaluates as an expression – Resulting instance can not have a constructor or any encapsulated state. Just implementation of interface But does have access to any local variables declared as final as well as any encapsulated state of the current object when used. lec23.v6