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

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

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

Lecture 18 Java Graphics and GUIs

CSE 331 Software Design & Implementation

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation

Introduction to the JAVA UI classes Advanced HCI IAT351

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

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!

Graphical User Interfaces. Comp 152

CSE 331 Software Design & Implementation

Building Java Programs Bonus Slides

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

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

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

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

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Window Interfaces Using Swing Objects

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

Swing. By Iqtidar Ali

Systems Programming Graphical User Interfaces

Chapter 12 GUI Basics

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Basicsof. JavaGUI and SWING

CS 251 Intermediate Programming GUIs: Components and Layout

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

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Chapter 6: Graphical User Interfaces

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Packages: Putting Classes Together

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Graphical User Interfaces

CS11 Java. Fall Lecture 4

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

COMPSCI 230. Software Design and Construction. Swing

Window Interfaces Using Swing Objects

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

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

Java Programming Lecture 6

Graphical interfaces & event-driven programming

Programming graphics

Java. GUI building with the AWT

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

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

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

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

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

BASICS OF GRAPHICAL APPS

Java: Graphical User Interfaces (GUI)

INTRODUCTION TO (GUIS)

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

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Chapter 13 GUI Basics. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved.

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

CS11 Java. Fall Lecture 3

Java IDE Programming-I

2110: GUIS: Graphical User Interfaces

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

GUI in Java TalentHome Solutions

Graphical User Interfaces (GUIs)

Part I: Learn Common Graphics Components

Chapter 12 Advanced GUIs and Graphics

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS

Computer Science 210: Data Structures. Intro to Java Graphics

Chapter 12 GUI Basics. Motivations. The design of the API for Java GUI programming

PIC 20A GUI with swing

user-friendly and easy to use.

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

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

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

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

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

Graphical User Interface (GUI)

Supporting Materials

Swing from A to Z Some Simple Components. Preface

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

G51PRG: Introduction to Programming Second semester Applets and graphics

User interfaces and Swing

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

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

AP CS Unit 11: Graphics and Events

Jonathan Aldrich Charlie Garrod

Lecture 5: Java Graphics

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

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

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

CHAPTER 2. Java Overview

Graphical User Interface

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Graphical User Interfaces in Java - SWING

Programming Mobile Devices J2SE GUI

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

Transcription:

CSE 331 Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT Lecturer: Michael Hotan slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia hnp://www.cs.washington.edu/331/ 1

Why learn GUIs? Learn about event- driven programming techniques PracSce learning and using a large, complex API A chance to see how it is designed and learn from it: model- view separason design panerns refactoring vs. reimplemensng an ailing API Because GUIs are neat! Cau1on: There is way more here than you can memorize. Part of learning a large API is "leyng go." You won't memorize it all; you will look things up as you need them. But you can learn the fundamental concepts and general ideas. 2

Java GUI History Abstract Windowing Toolkit (AWT): Sun's inisal effort to create a set of cross- pla^orm GUI classes. (JDK 1.0-1.1) Maps general Java code to each operasng system's real GUI system. Problems: Limited to lowest common denominator; clunky to use. Swing: A newer GUI library wrinen from the ground up that allows much more powerful graphics and GUI construcson. (JDK 1.2+) Paints GUI controls itself pixel- by- pixel rather than handing off to OS. Benefits: Features; compasbility; OO design. Problem: Both exist in Java now; easy to get them mixed up; ssll have to use both in various places. 3

GUI terminology window: A first- class ciszen of the graphical desktop. Also called a top- level container. examples: frame, dialog box, applet component: A GUI widget that resides in a window. Also called controls in many other languages. examples: bunon, text box, label container: A logical grouping for storing components. examples: panel, box 4

Components 5

6 Swing inheritance hierarchy Component (AWT) Window Frame JFrame (Swing) JDialog import java.awt.*; import javax.swing.*; Container JComponent (Swing) JButton JColorChooser JFileChooser JComboBox JLabel JList JMenuBar JOptionPane JPanel JPopupMenu JProgressBar JScrollbar JScrollPane JSlider JSpinner JSplitPane JTabbedPane JTable JToolbar JTree JTextArea JTextField...

Component properties Each has a get (or is) accessor and a set modifier method. examples: getcolor, setfont, setenabled, isvisible name type descrip?on background Color background color behind component border Border border line around component enabled boolean whether it can be interacted with focusable boolean whether key text can be typed on it font Font font used for text in component foreground Color foreground color of component height, width int component's current size in pixels visible boolean whether component can be seen toolsp text String text shown when hovering mouse size, minimum / maximum / preferred size Dimension various sizes, size limits, or desired sizes that the component may take 7

8 JFrame a graphical window to hold other components public JFrame() public JFrame(String title) Creates a frame with an opsonal Stle. Call setvisible(true) to make a frame appear on the screen ader creasng it. public void add(component comp) Places the given component or container inside the frame.

More JFrame public void setdefaultcloseoperation(int op) Makes the frame perform the given acson when it closes. Common value passed: JFrame.EXIT_ON_CLOSE If not set, the program will never exit even if the frame is closed. public void setsize(int width, int height) Gives the frame a fixed size in pixels. public void pack() Resizes the frame to fit the components inside it snugly. 9

10 GUI example SimpleWindow.java

11 JButton a clickable region for causing ac1ons to occur public JButton(String text) Creates a new bunon with the given string as its text. public String gettext() Returns the text showing on the bunon. public void settext(string text) Sets bunon's text to be the given string.

12 GUI example MagicButton.java

Sizing and positioning How does the programmer specify where each component appears, how big each component should be, and what the component should do if the window is resized / moved / maximized / etc.? Absolute posi?oning (C++, C#, others): Programmer specifies exact pixel coordinates of every component. "Put this bunon at (x=15, y=75) and make it 70x31 px in size." Layout managers (Java): Objects that decide where to posison each component based on some general rules or criteria. "Put these four bunons into a 2x2 grid and put these text boxes in a horizontal flow in the south part of the frame." 13

14 Containers and layout Place components in a container; add the container to a frame. container: An object that stores components and governs their posisons, sizes, and resizing behavior.

15 JFrame as container A JFrame is a container. Containers have these methods: public void add(component comp) public void add(component comp, Object info) Adds a component to the container, possibly giving extra informason about where to place it. public void remove(component comp) public void setlayout(layoutmanager mgr) Uses the given layout manager to posison components. public void validate() Refreshes the layout (if it changes ader the container is onscreen).

Preferred sizes Swing component objects each have a certain size they would "like" to be: Just large enough to fit their contents (text, icons, etc.). This is called the preferred size of the component. Some types of layout managers (e.g. FlowLayout) choose to size the components inside them to the preferred size. Others (e.g. BorderLayout, GridLayout) disregard the preferred size and use some other scheme to size the components. BuOons at preferred size: Not preferred size: 16

FlowLayout public FlowLayout() treats container as a led- to- right, top- to- bonom "paragraph". Components are given preferred size, horizontally and verscally. Components are posisoned in the order added. If too long, components wrap around to the next line. myframe.setlayout(new FlowLayout()); myframe.add(new JButton("Button 1")); The default layout for containers other than JFrame (seen later). 17

18 BorderLayout public BorderLayout() Divides container into five regions: NORTH and SOUTH regions expand to fill region horizontally, and use the component's preferred size verscally. WEST and EAST regions expand to fill region verscally, and use the component's preferred size horizontally. CENTER uses all space not occupied by others. myframe.setlayout(new BorderLayout()); myframe.add(new JButton("Button 1"), BorderLayout.NORTH); This is the default layout for a JFrame.

GridLayout public GridLayout(int rows, int columns) Treats container as a grid of equally- sized rows and columns. Components are given equal horizontal / verscal size, disregarding preferred size. Can specify 0 rows or columns to indicate expansion in that direcson as needed. 19

20 GUI example LMExample.java

Event Listeners 21

Graphical events event: An object that represents a user's interacson with a GUI component; can be "handled" to create interacsve components. listener: An object that waits for events and responds to them. To handle an event, anach a listener to a component. The listener will be nosfied when the event occurs (e.g. bunon click). 22

Event-driven programming event- driven programming: A style of coding where a program's overall flow of execuson is dictated by events. Rather than a central "main" method that drives execuson, the program loads and waits for user input events. As each event occurs, the program runs parscular code to respond. The overall flow of what code is executed is determined by the series of events that occur, not a pre- determined order. 23

24 Event hierarchy import java.awt.event.*; EventObject AWTEvent (AWT) ActionEvent TextEvent ComponentEvent FocusEvent WindowEvent InputEvent KeyEvent MouseEvent EventListener AWTEventListener ActionListener TextListener ComponentListener FocusListener WindowListener KeyListener MouseListener

Action events ac?on event: An acson that has occurred on a GUI component. The most common, general event type in Swing. Caused by: bunon or menu clicks, check box checking / unchecking, pressing Enter in a text field,... Represented by a class named ActionEvent Handled by objects that implement interface ActionListener 25

26 Implementing a listener public class name implements ActionListener { public void actionperformed(actionevent event) { code to handle the event; } } JButton and other graphical components have this method: public void addactionlistener(actionlistener al) ANaches the given listener to be nosfied of clicks and events that occur on this component.

Nested classes nested class: A class defined inside of another class. Usefulness: Nested classes are hidden from other classes (encapsulated). Nested objects can access/modify the fields of their outer object. Event listeners are oden defined as nested classes inside a GUI. 27

Nested class syntax // enclosing outer class public class name {... } // nested inner class private class name {... } Only the outer class can see the nested class or make objects of it. Each nested object is associated with the outer object that created it, so it can access/modify that outer object's methods/fields. If necessary, can refer to outer object as OuterClassName.this 28

29 GUI example LMExample.java