Introduction to Computation and Problem Solving Class 14: Introduction to the Swing Toolkit Prof. Steven R. Lerman and Dr. V. Judson Harward 1.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Graphical User Interfaces
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 13 GUI Basics.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
1 GUI Basics First three slides are repeated from previous lecture notes! You will probably (in fact should) never write Java codes like the program examples.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
Introduction to Computation and Problem Solving Class 15: Constructing Interfaces with Swing Prof. Steven R. Lerman and Dr. V. Judson Harward 1.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
GUI Basics.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CS Lecture 00 Swing overview and introduction Lynda Thomas
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
MIT-AITI 2004 – Lecture 16 Introduction to Swing.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
MIT AITI 2003 Lecture 16. Swing Part I Let’s swing and have some fun !!!
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
1 IM103 week 10 Enhancing the user interface By the end of lecture you should be able to:  distinguish between heavyweight and lightweight components;
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Java Applets.
Graphical User Interfaces
Java Applets.
Lecture 27 Creating Custom GUIs
A Quick Java Swing Tutorial
Java Applets.
Java Applets.
Week 8 Swing NetBeans GUI Builder
A Quick Java Swing Tutorial
Chapter 12 GUI Basics.
Graphical User Interface
Presentation transcript:

Introduction to Computation and Problem Solving Class 14: Introduction to the Swing Toolkit Prof. Steven R. Lerman and Dr. V. Judson Harward 1

2 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: 2D Transformations in Swing and a Graphing Case Study.

3 Swin g 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

4 A Simple GUI Application Let’s build a simple application with a GUI, a date desk accessory that displays today’s date:

Converts Date Text Millisecond ticks since since January 1, 1970, 00:00:00 GMT 5 Getting the Date If we are going to display the date, we first have to figure out what it is: import java.text.*; import java.util.*; public class Today0 { 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

6 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 ); //today.show(); } Our GUI class

7 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 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)

8 Anatomy of a JFrame Look and Feel and platform dependent The contentPane is an inner container that holds your content Interacts with the window system

9 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(); }

10 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;

11 Displaying the Date, Try 1, Part 4 public Today1( String dStr ) { dateLabel = new JLabel( "Today is " + dStr ); getContentPane( ).add( dateLabel, BorderLayout.CENTER ); Use the ctor argument to build the JLabel Add the JLabel to the JFrame’s contentPane Where to add it; more on this layout manager in the next lecture

12 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

13 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.

14 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;

15 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

16 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);

17 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.

18 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);

19 Displaying the Date, Try 3 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 JLabel background is transparent in some look & feels by default

20 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 East South

21 A Simple Image Viewer, 1 import javax.swing.*; import java.awt.BorderLayout; import java.net.*; public class ImageView1 extends JFrame { public static void main( String [] args ) { String url = " String title = "The New Stata Center"; ImageView1 theView = new ImageView1( url, title ); theView.setVisible( true ); }

22 A Simple Image Viewer, 2 private URL source; private String title; private JLabel imageLabel; private JLabel titleLabel; public ImageView1( String u, String t ) { setDefaultCloseOperation( EXIT_ON_CLOSE ); try { source = new URL( u ); } catch ( MalformedURLException e ) { System.out.println( "Bad URL " + source ); System.exit( 1 ); } title = t; An incantation explained in a later lecture

23 A Simple Image Viewer, 3 // make a label of an image ImageIcon image = new ImageIcon( source ); imageLabel = new JLabel( image, SwingConstants.CENTER ); getContentPane().add( imageLabel, BorderLayout.CENTER ); // make a 2 nd label of the title titleLabel = new JLabel( title SwingConstants.CENTER ); // set it below getContentPane().add( titleLabel, BorderLayout.SOUTH ); pack( ); }

24 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.

25 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 ); }

26 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.setPreviewPanel( colorLabel ); chooser.getSelectionModel().addChangeListener ( colorLabel ); etContentPane().add( chooser, BorderLayout.CENTER ); pack( ); }