Window Interfaces Using Swing. Chapter 12

Similar documents
Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects

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

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

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

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

Agenda. Container and Component

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

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Introduction. Introduction

Systems Programming Graphical User Interfaces

Graphical User Interfaces. Comp 152

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Java Programming Lecture 6

CS 251 Intermediate Programming GUIs: Event Listeners

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

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

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!

Example: Building a Java GUI

Example: Building a Java GUI

CS180 Recitation. More about Objects and Methods

PIC 20A GUI with swing

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921

Part I: Learn Common Graphics Components

To gain experience using GUI components and listeners.

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946

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

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

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

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

More About Objects and Methods

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

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

Class 16: The Swing Event Model

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

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

Introduction to the JAVA UI classes Advanced HCI IAT351

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

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

CS 251 Intermediate Programming GUIs: Components and Layout

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

AP CS Unit 11: Graphics and Events

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

MIT AITI Swing Event Model Lecture 17

Swing/GUI Cheat Sheet

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

Graphical User Interfaces in Java

Java IDE Programming-I

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

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

COMP16121 Sample Code Lecture 1

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

Unit 6: Graphical User Interface

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Java. GUI building with the AWT

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphical User Interface (GUI)

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

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

JAVA NOTES GRAPHICAL USER INTERFACES

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

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

Graphical User Interfaces (GUIs)

GUI Forms and Events, Part II

Chapter 6: Graphical User Interfaces

CS 209 Programming in Java #10 Exception Handling

Object-Oriented Programming Design. Topic : User Interface Components with Swing GUI Part III

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

More Swing. Chapter 14. Chapter 14 1

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Laying Out Components. What is Widget Layout?

Chapter 7: A First Look at GUI Applications

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

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Dr. Hikmat A. M. AbdelJaber

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

1005ICT Object Oriented Programming Lecture Notes

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

Jonathan Aldrich Charlie Garrod

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Building Graphical User Interfaces. Overview

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Design patterns for graphical user interface applications

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

CS 2113 Software Engineering

Object Oriented Design & Patterns. Part 1

CSE 1325 Project Description

Building Graphical User Interfaces. GUI Principles

Swing from A to Z Some Simple Components. Preface

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

More About Objects and Methods

CMP-326 Exam 2 Spring 2018 Solutions Question 1. Version 1. Version 2

Transcription:

Window Interfaces Using Swing 1

Reminders Project 7 due Nov 17 @ 10:30 pm Project 6 grades released: regrades due by next Friday (11-18-2005) at midnight 2

GUIs - Graphical User Interfaces Windowing systems that interact with users often are called GUIs. A GUI accepts information from a user and makes it available to the program for processing. Most of the interaction is graphical in nature. 3

Event-Driven Programming Most GUI programs involve events and event handlers. A GUI event is an object that represents some action such as clicking the mouse, dragging the mouse, pressing a keyboard key, clicking the close-window button on a window, etc. When an object generates an event, it is said to fire the event. 4

Event-Driven Programming, cont. Objects that can fire events have one or more listener objects. The programmer chooses which event-firing objects have listeners. Event handlers are programmer-defined methods associated with the listener objects that determine what happens when events are detected by their associated listener(s). 5

Layout Managers The objects that you add to a container class are arranged by an object known as a layout manager. A layout manager is added using method setlayout, which is a method of every container class. syntax Container_Object.setLayout(new Layout_Manager_Class(Any_Parameters)); 6

Border Layout, cont. A BorderLayout manager can place a component into any of five regions. Regions which are unused give up their space to BorderLayout.CENTER. 7

Flow Layout The simplest layout manager is the FlowLayout manager. Components are added and arranged one after another, left to right, until a row is filled. Then components are added to the next row in the same manner. Each row is centered in its container. 8

Grid Layout A GridLayout manager arranges components in a grid of rows and columns. example acontainer.setlayout(new GridLayout(2,3)); 9

Grid Layout, cont. Each entry has the same size. Rows are filled one at a time, top to bottom, and from left to right within each row. 10

Default Layout Managers When a layout manager is not added explicitly, a default layout manager is provided. The default manager for the content pane of a JFrame is BorderLayout. The default manager for a JPanel is FlowLayout. 11

Buttons A button is a GUI component that looks like a button and does something when it is clicked using a mouse. Like a label, a button is created and added to a container. Unlike a label, a button can fire an event and the event can cause a GUI to perform some action. 12

Adding Buttons A button is created using JButton Button_Name = new JButton( Button_Label ); A button is added to a container using Container_Name.add(Button_Name); 13

14

Adding Buttons, cont. 15

Action Listeners and Action Events For each button, the GUI needs to register (specify) the listener object(s). define the methods to be invoked when an event is fired. For a statement such as stopbutton.addactionlistener(this); the class ButtonDemo is itself the listener class. 16

Action Listeners and Action Events, cont. Different kinds of components requite different kinds of listener classes. Buttons fire action events which are handled by action listeners. An action listener is an object of type ActionListener, and ActionListener is an interface (discussed later). 17

Action Listeners and Action Events, cont. To make a class into an ActionListener add implements ActionListener to the heading of the class definition define a method named ActionPerformed. register the ActionListener object with the component that will fire the event using the method addactionlistener (A component may register with more than one listener.) 18

Action Listeners and Action Events, cont. Any class can be an action listener class. 19

Buttons and an Action Listener 20

The actionperformed Method An actionlistener class must have a method named actionperformed that has one parameter of type ActionEvent. syntax public void actionperformed(actionevent e) { Code_for_Actions_Performed } 21

Interfaces An interface is a property of a class that states what methods the class must define. ActionListener is an interface. A class which satisfies the requirements of an interface implements the interface. A class can define methods in addition to the methods required by the interface. 22

Interfaces, cont. To implement an interface, a class must include the phrase implements Interface_Name at the start of the class definition implement all the method headings listed in the definition of the interface. 23

Multiple Interfaces A class which implements multiple interfaces lists the names of all the interfaces, separated by commas. implements First_Interface_Name, Second_Interface_Name,, Last_Interface_Name The class must implement all the methods of all the listed interfaces. 24

Method setactioncommand We can think of the method invocation e.getactioncommand() as returning the string written on the button. In fact, this method invocation returns a string known as the action command for the button. A different action command can be specified for the button. 25

Method setactioncommand, cont. example JButton stopbutton = new JButton( Red ); stopbutton.setactioncommand( Stop ); This permits the same string to be written on two different buttons, but with the two buttons distinguished from one another by the program. 26

Method setactioncommand, cont. Every object that fires an action event has an associated string known as the action command for that component. e.getactioncommand() returns the action command for the component that fired e. The default action command for a button is the string written on it. Method setactioncommand can be used to change the action command for the object. 27

The JPanel Class A GUI can be organized hierarchically, with window-like containers inside other windowlike containers. Class JPanel is a simple container that does little more than hold components. Components can be placed in a JPanel which can be placed in another JPanel, which can be placed in a JFrame. 28

The JPanel Class, cont. To place two components in BorderLayout.SOUTH for example, simply place the two components in a panel and place the panel in the BorderLayout.SOUTH position. The panel has its own layout manager. 29

30

The JPanel Class, cont. 31

The Container Class Class Container is a predefined class. An object of a class which descends from class Container is called a container class and can have components added to it. Class JFrame is a descendent of class Container, permitting any JFrame object to hold labels, buttons, panels, and other components. 32

Adding Components To add a component to a JFrame, use method getcontentpane to obtain the content pane, and the use method add with the content pane as the calling object. example Container contentpane = getcontentpane(); Jlabel label = new Jlabel( Click Here ); contentpane.add(label); 33

Adding Components, cont. For other container classes, add components by using method add directly with an object of the container class. example JPanel buttonpanel = new JPanel(); JButton stopbutton = new JButton( Stop ); buttonpanel.add(stopbutton); 34

Text Areas and Text Fields class MemoSaver 35

Text Areas and Text Fields, class MemoSaver, cont. 36

Text Areas and Text Fields, cont. 37

Text Areas and Text Fields, cont. An object of class JTextArea can have a size consisting of a specified number of lines and a specified number of characters per line. example JTextArea sometext = new JTextArea(10,30); 38

Text Areas and Text Fields, cont. An object of class JTextField can have a size consisting of a specified number of characters. example JTextField name = new JTextField(10); 39

Text Areas and Text Fields, cont. The number of characters (per line) is not absolute, but represents the space needed for one m character. 40

Line Wrapping in Text Areas Method setlinewrap sets the line wrapping policy for a JTextArea object. example thetext.setlinewrap(true); If the argument is set to false, extra characters will be on the same line, but will not be visible. 41

Read-Only Text Components To specify that a user cannot write in a JTextArea or a JTextField, use method seteditable. example thetext.seteditable(false); A JTextArea or a JTextField can be made editable subsequently using, for example thetext.seteditable(true); 42

Inputting and Outputting Numbers Input provided using a JTextArea object or JTextField object is received as a string. When numeric input is needed, the string must be converted to a number. To output a number using a GUI constructed with Swing, the number must be converted to a string. All input typed by the user is string output, and all displayed output is string output. 43

Inputting and Outputting Numbers, cont. To convert a string to an integer, use, for example Integer.parseInt( 42 ); or Integer.parseInt(ioField.getText()); eliminate whitespace before or after the input, use or, to Integer.parseInt (iofield.gettext().trim()); 44

Inputting and Outputting Numbers, cont. To input numbers of type double, use Double.parseDouble (iofield.gettext().trim()); Analogous conversions can be done with classes Long and Float. 45

Inputting and Outputting Numbers, cont. To write numeric output to a JTextArea or a JTextField, use method tostring. examples Integer.toString(sum); Double.toString(average); iofield.settext(integer.tostring(sum)); 46

Catching a NumberFormatException A GUI, such as class Adder, has no control over what the user enters in the text field. The user might enter commas, or even alphabetic characters, resulting an a NumberFormatException, which leaves the GUI in an unpredictable state. 47

Catching a NumberFormatException, A NumberFormatException can be caught, and the user can be asked to reenter the number. 48

Summary You have learned the basics of event-driven programming. You have designed and coded a simple GUI with buttons and text. You have learned about several Swingrelated classes. 49