Swing: Building GUIs in Java

Similar documents
Swing: Building GUIs in Java

Graphical User Interfaces (GUIs)

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

CS 251 Intermediate Programming More on classes

EPITA Première Année Cycle Ingénieur. Atelier Java - J5

Packages Inner Classes

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

CS108, Stanford Handout #22. Thread 3 GUI

Topic 6: Inner Classes

CS 251 Intermediate Programming GUIs: Event Listeners

Swing from A to Z Some Simple Components. Preface

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287

Part I: Learn Common Graphics Components

Graphical interfaces & event-driven programming

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Another IS-A Relationship

Java Swing Introduction

Interfaces. An interface forms a contract between the object and the outside world.

C30b: Inner Class, Anonymous Class, and Lambda Expression

Timing for Interfaces and Abstract Classes

Introduction to concurrency and GUIs

Final Exam CS 251, Intermediate Programming December 13, 2017

*An nested class is a class that is defined inside another class.

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Graphical User Interface (GUI)

Graphical User Interface (GUI)

CS 11 java track: lecture 3

AP CS Unit 11: Graphics and Events

(Incomplete) History of GUIs

A final method is a method which cannot be overridden by subclasses. A class that is declared final cannot be inherited.

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

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

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

CPS221 Lecture: Threads

CS11 Java. Fall Lecture 4

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

CS Exam 1 Review Suggestions

CITS2210 Object-Oriented Programming. Topic 10. Java: Nested and Inner Classes

User Interface Programming OOP/Java Primer. Step 4 Some Final Checkups

Interfaces (1/2) An interface forms a contract between the object and the outside world.

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

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Programming Language Concepts: Lecture 8

Widget Toolkits CS MVC

Graphical User Interface (GUI)

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Design Patterns: Part 2

Coverage of Part 2. A Brief Introduction to Java for C++ Programmers: Part 2. import: using packages

Swing. By Iqtidar Ali

Widgets. Widgets Widget Toolkits. User Interface Widget

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

[module lab 2.2] GUI FRAMEWORKS & CONCURRENCY

Programming Languages and Techniques (CIS120)

Lecture 5: Java Graphics

CS193j, Stanford Handout #21. Threading 3

Basicsof. JavaGUI and SWING

Lecture 3: Java Graphics & Events

Model-View-Controller

Introduction to the JAVA UI classes Advanced HCI IAT351

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

CSE 331 Software Design & Implementation

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CS 251 Intermediate Programming GUIs: Components and Layout

SD Module-1 Advanced JAVA

FirstSwingFrame.java Page 1 of 1

Sketchpad. Plan for Today. Class 22: Graphical User Interfaces IBM 705 (1954) Computer as Clerk : Augmenting Human Intellect

SD Module-1 Advanced JAVA. Assignment No. 4

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

Java Thread Programming By Paul Hyde

MODEL UPDATES MANIPULATES USER

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

GUI Programming. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies

CS415 Human Computer Interaction

CS11 Java. Fall Lecture 3

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Object-oriented programming in Java (2)

CSC207 Week 4. Larry Zhang

Subtype Polymorphism

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Java, Swing, and Eclipse: The Calculator Lab.

Java: Graphical User Interfaces (GUI)

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

Reflection. Based on

Java Concurrency in practice Chapter 9 GUI Applications

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

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Window Interfaces Using Swing Objects

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Handouts. 1 Handout for today! Recap. Homework #2 feedback. Last Time. What did you think? HW3a: ThreadBank. Today. Small assignment.

Class 34: Introduction to Threads

Java Programming Lecture 6

Transcription:

Swing: Building GUIs in Java ENGI 5895: Software Design Andrew Vardy Faculty of Engineering & Applied Science Memorial University of Newfoundland February 13, 2017

Outline 1 Introduction 2 Aside: Inner Classes 3 Aside: Threads 4 Examples

Introduction Swing is an officially supported GUI toolkit for Java. It was developed to replace the GUI components of the Abstract Window Toolkit (AWT). It has the following features: Pluggable look and feel [DEMO] Numerous standard and specialized widgets (text boxes, buttons, tabbed panels,...) Swing components render themselves in an OS-independent manner Relative layout of components Nice design choices: Easily extensible with heavy use of patterns

Packages In the past the AWT provided the widgets, but it is generally advisable to use only Swing widgets. However, much of the functionality provided by AWT has been retained by Swing: java.awt: Contains superclasses for GUI components, as well as other useful classes such as Color and Point java.awt.event: Classes for event handling javax.swing: Contains the actual GUI components The Swing components all start with J. e.g. JButton, JPanel, JFrame... Avoid the non-j versions of these.

Class Diagram This is a partial class diagram for AWT and Swing: In orange are the commonly used containers for other components. In blue are some of the commonly used components. What design patterns do you see here?

Aside: Inner Classes You can create classes within classes. These are known as Inner Classes. Why would you want anything so strange? Like helper methods you can create helper classes which are embedded within a larger class Inner classes have priviledged access to private members of the outer class Similar to the friend feature of C++, only more contained Having small classes declared close to their point of use helps readibility and maintainability

Life of an Inner Class Each inner class instance is associated with an outer class instance. Consider this example: class Outer { private int outvalue = 6; public int getvalue () { return outvalue ; class Inner1 { private int value ; Inner1 () { value = outvalue + 1; public int getvalue () { return value ;

To create an instance of an inner class within the outer class, the syntax is straightforward: class Outer2 { int outvalue = 14; Outer2 () { Inner2 inner2 = new Inner2 (); class Inner2 { Inner2 () { outvalue++; public class TestOuter2 { public static void main( String [] args) { Outer2 outer2 = new Outer2 (); System. out. println( outer2. outvalue );

We can also create instances of an inner class from OUTSIDE the outer class, but the syntax is weird: OuterClass. InnerClass innerobject = outerobject. new InnerClass (); Notice how new is treated like a field of the outer class. Here we test our previous example: public class TestOuter { public static void main( String [] args) { Outer outer = new Outer (); System. out. println( outer. getvalue ()); Outer. Inner1 inner = outer. new Inner1 (); System. out. println( inner. getvalue ());

Nested Classes The general category to which inner classes belong is nested classes. There are two types of nested classes: inner classes (as we have seen) and static nested classes. A static nested class is like an inner class but does not have access to the members of the enclosing class: class OuterClass {... static class StaticNestedClass {... class InnerClass {... Example from The Java Tutorials: http:download.oracle. com/javase/tutorial/java/javaoo/nested.html Static nested classes are basically top-level classes that are packaged within another class for convenience.

Local Classes Alocalclassisanestedclassdeclaredwithinthebodyofa method. Note that it still has access to outer class members: import java. util. ; class Celebrity extends Observable { / / class MyOuter1 { int i = 0; public void dostuff () { Celebrity britney = new Celebrity (); Declare a local class as an Oberver class MyLocalObserver implements Observer { public void update( Observable o, Object arg) {... i++; britney. addobserver( new MyLocalObserver () );

Anonymous Classes Anonymous classes can be declared and instantiated at the same time. Note that they still have access to outer class members: import java. util. ; class Celebrity extends Observable { / / class MyOuter2 { int i = 0; public void dostuff () { Celebrity britney = new Celebrity (); Create an anonymous inner class and add as an observer of britney. britney. addobserver( new Observer () { public void update( Observable o, Object arg) {... i++; );

Aside: Threads A process is a self contained program with its own resources. A thread is a lightweight process. Threads share the memory space of the process that spawned them. Aprocesswithmultiplethreadsisusefulinsituationswherevarious clients (e.g. the user, network connections) must be continually addressed. However, they introduce a number of potential problems. Concurrency is a big topic (the subject of a whole course 8893). In this course you are advised to follow the guidelines for using threads in Swing. If you are sure your project needs multiple threads, read the following tutorial: http:download.oracle.com/javase/tutorial/essential/ concurrency/index.html

Guideline for Threads in Swing Swing is not thread-safe. Willy-nilly usage of threads will create problems! Possible problems range from unresponsive user interfaces to bizarre crashes and exceptions. There are three types of threads to be concerned with: Initial thread(s): Executes the initial code of your application The event dispatch thread: Handlesallevents.Almostallof your Swing code will run in this thread. Computationally expensive tasks should not be executed here. Worker or background threads: Handle expensive tasks running in the background.

The most important rule is that Swing code should almost always should be executed in the event-dispatch thread. The code in main can be considered the initial thread. In Swing applications the main job of the initial thread is to create a Runnable object and schedule it for execution on the event dispatch thread. Runnable is an interface that consists of nothing but public void run(). The Runnable created should be passed to SwingUtilities.invokeLater or SwingUtilities.invokeAndWait. In the following, we create an anonymous Runnable class and call invokelater on it: SwingUtilities. invokelater( new Runnable () { public void run () { createandshowgui (); ); For more details see the Java tutorial on Concurrency in Swing : http:download.oracle.com/javase/tutorial/uiswing/ concurrency/index.html

Examples Code for the following examples will be posted on the course website. FirstSwingFrame SecondSwingFrame LayoutPlay Paint1 Paint2 GameOfLife