CSE 8B Intro to CS: Java

Similar documents
More Swing. CS180 Recitation 12/(04,05)/08

Chapter 1 GUI Applications

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Chapter 14. More Swing

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Programming Language Concepts: Lecture 8

Systems Programming Graphical User Interfaces

Programming Languages and Techniques (CIS120e)

Graphical Interfaces

University of Cape Town ~ Department of Computer Science. Computer Science 1016S / 1011H ~ November Exam

Building Graphical User Interfaces. GUI Principles

FORMAS DE IMPLEMENTAR LOS OYENTES. A).- El oyente en una clase independiente con el constructor con un argumento y usando el método getactioncommand.

Building Graphical User Interfaces. Overview

Window Interfaces Using Swing Objects

Graphical Interfaces

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Chapter 13 Lab Advanced GUI Applications

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Swing from A to Z Some Simple Components. Preface

Window Interfaces Using Swing Objects

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Summary Chapter 25 GUI Components: Part 2

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Advanced Java Programming (17625) Event Handling. 20 Marks

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

CS 180 Final Exam Review 12/(11, 12)/08

Swing from A to Z Using Focus in Swing, Part 2. Preface

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

More Swing. Chapter 14. Chapter 14 1

BM214E Object Oriented Programming Lecture 13

GUI Programming: Swing and Event Handling

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

Theory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use

Inheritance (cont) Abstract Classes

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

!"# $ %&# %####' #&() % # # # #&* # ## +, # -

Lecture 5: Java Graphics

Today. cisc3120-fall2012-parsons-lectiii.3 2

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

Dr. Hikmat A. M. AbdelJaber

Programming Languages and Techniques (CIS120)

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

CS 11 java track: lecture 3

Event Dispatch. Review: Events

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Java Programming Unit 6. Inner Classes. Intro to Apples. Installing Apache Tomcat Server.

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

Midterm assessment - MAKEUP Fall 2010

GUI Event Handlers (Part I)

1 of :32:42

Programming Languages and Techniques (CIS120)

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

Functors - Objects That Act Like Functions

OBJECT ORIENTED PROGRAMMING. Java GUI part 1 Loredana STANCIU Room B616

Implementing Graphical User Interfaces

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

Administrivia. CSSS Movie Night: Zombieland & Iron Man Date: Thurs., Mar 11 Time: 6 10 pm Location: DMP 310 Free pop & popcorn for every attendee!

RAIK 183H Examination 2 Solution. November 11, 2013

1.1 GUI. JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { // XXX. init() main() public static

Lecture 3: Java Graphics & Events

Inheritance (continued) Inheritance

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

8. Polymorphism and Inheritance

Lecture 19 GUI Events

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

JLayeredPane. Depth Constants in JLayeredPane

Handling Mouse and Keyboard Events

CSE 331 Software Design & Implementation

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Event Binding. Different Approaches Global Hooks. 2.5 Event Binding 1

GUI. Overview: JFC - Java Foundation Classes. JFC: Java Foundation Classes. Graphical User Interfaces. GUI-based Applications. containment hierarchy:

Attempt FOUR questions Marking Scheme Time: 120 mins

The AWT Event Model 9

Part 3: Graphical User Interface (GUI) & Java Applets

(Incomplete) History of GUIs

MIT AITI Swing Event Model Lecture 17

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Graphical User Interfaces in Java - SWING

1005ICT Object Oriented Programming Lecture Notes

Programming Languages and Techniques (CIS120)

RAIK 183H Examination 2 Solution. November 10, 2014

Based on slides by Prof. Burton Ma

encompass a group of features for building Graphical User Interfaces (GUI).

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Swing: Building GUIs in Java

A Simple Text Editor Application

Eclipsing Your IDE. Figure 1 The first Eclipse screen.

PIC 20A GUI with swing

II 12, JFileChooser. , 2. SolidEllipse ( 2), PolyLine.java ( 3). Draw.java

Swing: Building GUIs in Java

Class 16: The Swing Event Model

Transcription:

CSE 8B Intro to CS: Java Winter, 2006 February 23 (Day 14) Menus Swing Event Handling Inner classes Instructor: Neil Rhodes JMenuBar container for menus Menus associated with a JFrame via JFrame.setMenuBar JMenu container for menu items has a title JMenuItem Has a title Can have an icon, checkmark, shortcut 2

Menu Sample public class MenuTest extends JFrame { public static void main(string[] args) { MenuTest window = new MenuTest(); window.setvisible(true); MenuTest() { super("menutest"); setsize(300, 100); JButton button1 = new JButton("Button 1"); add(button1); JMenu menu1 = new JMenu("File"); menu1.add(new JMenuItem("New")); menu1.add(new JMenuItem("Open")); menu1.add(new JMenuItem("Close")); JMenu menu2 = new JMenu("Edit"); menu2.add(new JMenuItem("Cut")); menubar.add(menu2); 3 Using Inner Classes We ve seen using The subclass of JFrame as a listener Another class in a different file as a listener Sometimes, want a class dedicated as a listener, but not in a different file Different file means listener doesn t have access to private variables in the subclass of JFrame Different file means trudging across files to try to figure out GUI logic Inner classes Defined in another (outer) class Have access to instance variables of outer class 4

Example of Inner class public class MenuTest2 extends JFrame { private class CloseOnActionListener implements ActionListener { dispose(); // executes dispose on JFrame, since no dispose // is defined for CloseOnAction public static void main MenuTest2() { menu1.add(new JMenuItem("Open")); JMenuItem close = new JMenuItem("Close"); close.addactionlistener(new CloseOnActionListener()); menu1.add(close); 5 Inner class can be local to a method Class still has access to all instance variables of the outer class Local variables are limited: only access to local variables is final ones. Advantage: Moving code of class even closer to where it is needed Always an advantage to make things as local as possible Disadvantage: Can t reuse class outside of the method it s in 6

Example of local inner class public class MenuTest4 extends JFrame { public static void main(string[] args) { MenuTest4 window = new MenuTest4(); window.setvisible(true); MenuTest4() { super("menutest4"); final Button button = new Button("Testing"); add(button); JMenu menu1 = new JMenu("File"); JMenuItem close = new JMenuItem("Disable Button"); class DisableButton implements ActionListener { button.setenabled(false); close.addactionlistener(new DisableButton()); menu1.add(close); 7 Getting rid of unnecessary names We can do either: Person p = new Person( John, Smith ); addperson(p); or: addperson(new Person( John, Smith )); In one case, we have a named variable containing the person. In the other case, we have an anonymous person. Java allows the same idea with classes: anonymous classes Define a new class with no name Can only use it to instantiate an object of that type Can t instantiate more than one object or reuse the class 8

Syntax Anonymous classes new SuperClass(args to SuperClass constructr){ non-static methods non-static instance variables Usage: Use it wherever you can provide an object: addperson(new Person( Joe, Jones ) { public static getage() { return super.getage() / 2; ); Limitations: No constructors No static methods/variables No extends or implements 9 Very useful with listeners public class MenuTest5 extends JFrame { public static void main(string[] args) { MenuTest5 window = new MenuTest5(); window.setvisible(true); MenuTest5() { super("menutest5"); final Button button = new Button("Testing"); add(button); JMenu menu1 = new JMenu("File"); JMenuItem close = new JMenuItem("Disable Button"); close.addactionlistener(new ActionListener() { button.setenabled(false); ); menu1.add(close); 10

Anonymous interfaces If the specified SuperClass is actually an interface, the class created is a subclass of Object that implements the given interface new ActionListener() { button.setenabled(false); 11 Consider WindowListener Has 7 methods: windowopened windowclosing windowclosed windowiconified windowdeiconified windowactivated windowdeactivated Adapters What if you want to listen just for the window about to close? Requires 6 empty methods and one real one: - public class MyListener implements WindowListener { // six empty methods public void windowclosing(windowevent e) { dispose(); if (--numwindows == 0) System.exit(0); //close the JFrame 12

WindowAdapter An adapter is a class that implements a do-nothing listener interface. Swing provides a WindowAdapter: public class WindowAdapter implements WindowListener { public void windowopened(windowevent e) { public void windowclosing(windowevent e) { public void windowclosed(windowevent e) { public void windowiconified(windowevent e) { public void windowactivated(windowevent e) { public void windowdeactivated(windowevent e) { Your class now doesn t need 6 empty methods: public class MyListener extends WindowAdapter { public void windowclosing(windowevent e) { dispose(); //close the JFrame if (--numwindows == 0) System.exit(0); 13 Coding Style Make small, self-contained methods 25-line methods are almost always too long Use naming to self-document Fewer comments Better structure and naming Compare public void doit() { // prep the input and output Scanner // read the input while (scanner.hasnextline()) // sort the array Arrays.sort() // write the output for () public void doit() { prepinputandoutput(); readinput(); sortarray(); writeoutput(); private void prepinputandoutput() { private void readinput() { private void sortarray() { private void writeoutput() { 14