Class 14: Introduction to the Swing Toolkit

Similar documents
1.00 Lecture 14. Lecture Preview

Chapter 12 GUI Basics

Class 16: The Swing Event Model

Introduction to the JAVA UI classes Advanced HCI IAT351

Java Programming Lecture 6

CS 251 Intermediate Programming GUIs: Components and Layout

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

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!

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

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

AP CS Unit 11: Graphics and Events

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

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

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphics and Painting

Agenda. Programming Seminar. By: dr. Amal Khalifa. Coordinate systems Colors Fonts Drawing shapes Graphics2D API

Window Interfaces Using Swing Objects

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

Part I: Learn Common Graphics Components

PIC 20A GUI with swing

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

Graphical User Interface (GUI)

Window Interfaces Using Swing Objects

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

Lecture 5: Java Graphics

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

Graphical User Interfaces (GUIs)

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

Graphical User Interface (GUI) and Object- Oriented Design (OOD)

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

Graphics User Defined Forms, Part I

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

Systems Programming Graphical User Interfaces

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

Swing. By Iqtidar Ali

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

JAVA NOTES GRAPHICAL USER INTERFACES

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

Graphical User Interfaces in Java

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

2110: GUIS: Graphical User Interfaces

Lecture 3: Java Graphics & Events

JAVA NOTES GRAPHICAL USER INTERFACES

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

BASICS OF GRAPHICAL APPS

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

DM503 Programming B. Peter Schneider-Kamp.

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

Lecture 9. Lecture

CS Exam 1 Review Suggestions

IT101. Graphical User Interface

INTRODUCTION TO (GUIS)

CS Fall Homework 5 p. 1. CS Homework 5

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

Programming Language Concepts: Lecture 8

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

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

Chapter 13 Lab Advanced GUI Applications

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

AWT COLOR CLASS. Introduction. Class declaration. Field

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

Example 3-1. Password Validation

Packages: Putting Classes Together

H212 Introduction to Software Systems Honors

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

Agenda. Container and Component

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.

Graphical User Interface (GUI)

Class 27: Nested Classes and an Introduction to Trees

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

(Incomplete) History of GUIs

Computer Science 210: Data Structures. Intro to Java Graphics

CS 2113 Software Engineering

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

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

DEMYSTIFYING PROGRAMMING: CHAPTER TEN REPETITION WITH WHILE-LOOP (TOC DETAILED)

Lecture 18 Java Graphics and GUIs

Swing from A to Z Some Simple Components. Preface

Graphic Interface Programming II Using Swing and OOP more advanced issues. Uppsala universitet

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

Dr. Hikmat A. M. AbdelJaber

Example: Building a Java GUI

2IS45 Programming

Example: Building a Java GUI

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

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

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

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

Unit 6: Graphical User Interface

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

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

Learn Java Programming, Dr.Hashamdar. Getting Started with GUI Programming

Java. GUI building with the AWT

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

G51PRG: Introduction to Programming Second semester Applets and graphics

CSE 331 Software Design & Implementation

Transcription:

Introduction to Computation and Problem Solving Class 14: Introduction to the Swing Toolkit Prof. Steven R. Lerman and Dr. V. Judson Harward 1 Class Preview Over the next 5 lectures, we will introduce you to the techniques necessary to build graphic user interfaces for your applications. Class 14: Introduction to Swing Basic Concepts: Components and containers, fonts, colors, borders, layout Class 15: Constructing Swing interfaces. Class 16: Lab: The Swing Event Model Class 17: Lab: How to do Custom Drawing in Swing, the Graphics 2D API Class 18: Lab: 2D Transformations in Swing 2 1

Swing Package of user interface classes for windows, menus, scroll bars, buttons, etc. Independent of hardware and operating system (as long as they can paint a window) Swing gains independence but loses performance by not relying on native toolkit components Has Windows, Motif, Mac look and feel options Supersedes Java Abstract Window Toolkit (AWT) though it still uses many non-drawing classes from that library import java.awt.*; import javax.swing.*; See http://java.sun.com/docs/books/ tutorial/uiswing/index.html 3 A Simple GUI Application Let s build a simple application with a GUI, a date desk accessory that displays today s date: 4 2

Getting the Date If we are going to display the date, we first have to figure out what it is: 5 Converts Date Text import java.text.*; import java.util.*; Millisecond ticks since since public class Today0 { January 1, 1970, 00:00:00 GMT public static void main (String args[]) { DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL); Date now = new Date(); String datestr = formatter.format( now ); System.out.println( datestr ); A readable date Displaying the Date, Try 1 So, we ve got the date. Let s display it. We create an instance of a GUI class containing the String date and we make it visible: public static void main (String args[]) { DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL); Date now = new Date(); String datestr = formatter.format( now ); Today1 today = new Today1( datestr ); today.setvisible( true ); Our GUI class //today.show(); 6 3

The 3 Flavors of GUI Classes JComponents: present information or interact with the user Examples: labels ( JLabel), buttons (JButton), text fields ( JTextField) Containers: some JComponents are designed to hold other components, not to present or interact Examples: JPanel, JScrollPane Top Level Windows: are containers that are not contained by any other containers; they can be iconified or dragged and interact with the native windowing system Example: JFrame, JDialog (not JComponents at all) 7 Anatomy of a JFrame Look and Fee l and platform dependent Interacts with the window system 8 The contentpane is an inner container that holds your content 4

Displaying the Date, Try 1, Part 2 import javax.swing.*; import java.awt.borderlayout; public class Today1 extends JFrame { private JLabel datelabel; public Today1( String dstr ) { setdefaultcloseoperation( EXIT_ON_CLOSE ); datelabel = new JLabel( "Today is " + dstr ); getcontentpane().add( datelabel, BorderLayout.CENTER ); pack(); 9 Displaying the Date, Try 1, Part 3 The simplest way to display a String is a JLabel We need a JFrame to hold our JLabel public class Today1 extends JFrame { private JLabel datelabel; 10 5

Displaying the Date, Try 1, Part 4 Use the ctor argument to build the JLabel public Today1( String dstr ) { datelabel = new JLabel( "Today is " + dstr ); getcontentpane().add( datelabel, BorderLayout.CENTER ); Add the JLabel to the JFrame s contentpane Where to add it; more on this layout manager in the next lecture 11 Displaying the Date, Try 1, Part 5 So closing the window will end the application setdefaultcloseoperation( EXIT_ON_CLOSE ); pack(); //setsize( 300, 100 ); So the JFrame will fit its contents and not shrivel up like a raisin 12 6

Swing Application Termination Any program that makes a Swing component visible, including a dialog created via JOptionPane, must explicitly exit by 1. calling System.exit( int code ) or 2. using setdefaultcloseoperation( EXIT_ON_CLOSE ); to tell Swing what to do when the window system attempts to close the window, (e.g. by clicking the window close box.) the window close box Why? Because as soon as you put up a GUI window, you start a separate GUI thread that won't end when you run off the end of the main() method. 13 Displaying the Date, Try 1, Critique It s too small and the colors don t stand out. We need to choose a custom Font and text (foreground) Color. import java.awt.font; import java.awt.color; 14 7

Fonts Standard constructor: Font myfont = new Font( String name, int style, int size ); Font name: safe approach is to use a logical font name, one of "SansSerif", "Serif", "Monospaced", "Dialog", "DialogInput", "Symbol" Four font styles are present: Font.y where y is PLAIN, BOLD, ITALIC Font.BOLD + Font.ITALIC Size is point size; 12 corresponds to standard printed text 15 Colors 13 predefined colors: Color.x where x is orange, pink, cyan, magenta, yellow, black, blue, white, gray, lightgray, darkgray, red, green You can define your own colors // RGB or (red-green-blue) Color ugly= new Color(30, 90, 120); 16 8

Displaying the Date, Try 2 datefont = new Font( "Serif", Font.BOLD, 24 ); datelabel.setfont( datefont ); datelabel.setforeground( Color.red ); datelabel.setbackground( Color.white ); Why doesn't the background change? Not all JComponents are opaque by default. The JFrame crowds the label. 17 Borders In Swing, borders are objects. The best way to get some space around our label is to give it an empty border. The best way to create a border is to use the factory methods in class BorderFactory: import javax.swing.border.*;... Border empty = BorderFactory.createEmptyBorder( top,left, bottom,right); 18 9

Displaying the Date, Try 3 JLabel background is transparent in some look & feels by default datelabel.setopaque( true ); Border empty = createemptyborder(10,20,10,20); datelabel.setborder( empty ); datelabel.sethorizontalalignment( SwingConstants.CENTER ); Add our empty border to the label and center it 19 Building with Components As simple as our first application is, we can build some very interesting variations with little additional code. JLabels can hold images as well as or instead of text. A contentpane has 5 zones where you can add a component. North West Center South East 20 10

A Simple Image Viewer, 1 import javax.swing.*; import java.awt.borderlayout; public class ImageView2 extends JFrame { public static void main( String [] args ) { String path = "gwcrc10.jpg"; String title = "The Leonard P. Zakim Bridge"; ImageView2 theview = new ImageView2( path, title ); theview.setvisible( true ); 21 A Simple Image Viewer, 2 private JLabel imagelabel; private JLabel titlelabel; public ImageView2( String p, String t ) { setdefaultcloseoperation( EXIT_ON_CLOSE ); // make a label of an image ImageIcon image = new ImageIcon( p ); imagelabel = new JLabel( image, SwingConstants.CENTER ); getcontentpane().add( imagelabel, BorderLayout.CENTER ); // make a 2nd label of the title titlelabel = new JLabel( t, SwingConstants.CENTER ); getcontentpane().add( titlelabel, BorderLayout.SOUTH ); pack(); 22 11

Complex Components A JLabel is just about as simple as a component gets. But using a more complicated component is identical. Components encapsulate their functionality spatially and conceptually. Consider the following example that uses a JColorChooser, a very complicated component. 23 JColorChooser Example, 1 public class FavoriteColor extends JFrame { private ColorLabel colorlabel; private JColorChooser chooser; public static void main(string args[]) { FavoriteColor favor = new FavoriteColor( "Jud", new Color( 255, 200, 100 )); favor.setvisible( true ); 24 12

JColorChooser Example, 2 public FavoriteColor( String person, Color c ) { setdefaultcloseoperation( EXIT_ON_CLOSE ); chooser = new JColorChooser( c ); colorlabel = new ColorLabel( "This is " + person + "'s favorite color.", chooser ); chooser.getselectionmodel().addchangelistener( colorlabel ); getcontentpane().add( chooser, BorderLayout.CENTER ); getcontentpane().add( colorlabel, BorderLayout.SOUTH ); pack(); 25 13