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

Similar documents
Part I: Learn Common Graphics Components

Swing. By Iqtidar Ali

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

Introduction to the JAVA UI classes Advanced HCI IAT351

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.

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CS 251 Intermediate Programming GUIs: Components and Layout

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

Java: Graphical User Interfaces (GUI)

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphical User Interfaces (GUIs)

Chapter 12 GUI Basics

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

Packages: Putting Classes Together

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

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

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

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

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

Graphical User Interface (GUI)

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

(Incomplete) History of GUIs

Lecture 5: Java Graphics

CSE 331 Software Design & Implementation

Lecture 18 Java Graphics and GUIs

1. Swing Note: Most of the stuff stolen from or from the jdk documentation. Most programs are modified or written by me. This section explains the

Window Interfaces Using Swing Objects

Event Driven Programming

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

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

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

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

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

Java Swing Introduction

2110: GUIS: Graphical User Interfaces

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

CSC 1214: Object-Oriented Programming

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

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

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

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

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

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

Java Graphical User Interfaces

Java Programming Lecture 6

Visit for more.

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

Window Interfaces Using Swing Objects

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical User Interfaces. Comp 152

Containers and Components

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

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

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

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

Graphical User Interface (GUI)

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

Summary Chapter 25 GUI Components: Part 2

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

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

Programming Languages and Techniques (CIS120)

Week Chapter Assignment SD Technology Standards. 1,2, Review Knowledge Check JP3.1. Program 5.1. Program 5.1. Program 5.2. Program 5.2. Program 5.

To gain experience using GUI components and listeners.

1.00 Lecture 14. Lecture Preview

Lecture 3: Java Graphics & Events

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

Java IDE Programming-I

INTRODUCTION TO (GUIS)

Assignment 2. Application Development

AP CS Unit 11: Graphics and Events

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

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!

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

Swing from A to Z Some Simple Components. Preface

CSE 331 Software Design & Implementation

SD Module-1 Advanced JAVA

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

SD Module-1 Advanced JAVA. Assignment No. 4

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

Top-Level Containers

Widgets. Widgets Widget Toolkits. User Interface Widget

Programming graphics

Class 14: Introduction to the Swing Toolkit

Chapter 6: Graphical User Interfaces

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

Assignment 1. Application Development

Human-Computer Interaction IS4300

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

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

Contents Introduction 1

Widget Toolkits CS MVC

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

SELF-STUDY. Glossary

Programming Mobile Devices J2SE GUI

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

Unit 6: Graphical User Interface

Graphics User Defined Forms, Part I

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

Swing: Building GUIs in Java

Transcription:

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

GUI versus Graphics Programming Graphical User Interface (GUI) Graphics Programming Purpose is to display info and accept user input Manipulate graphical elements on a display Built using components points, lines, curves, shapes "Forms" applications texture and lighting animation, 3D effect

Simple GUI Application A graphical interface is built using components. Label (read-only) TextField (input) Button

Designing a Simple GUI 1. create a window (a container) 2. create components label = new Label("Username:"); input = new TextField( 12 ); // the width button = new Button("Login"); 3. layout components in a container add( label ); add( input ); add( button ); 4. display the container (frame is the container here): frame.setvisible( true ); 5. wait for user to do something

Software for Graphics Java provides frameworks for building graphical UIs. AWT - Abstract Windowing Toolkit the first Java graphics framework Swing - OS-independent framework. JavaFX - layout is specified in XML (fxml), like Android use SceneBuilder tool can also create layout in code (like Swing) SWT - Standard Widget Toolkit (used in Eclipse, Firefox) Designed for efficiency. An Eclipse project.

Graphics Toolkits Java provides complete "frameworks" for building graphical applications. A framework contains all the components and logic needed to manage a graphical interface. You provide: select components and set their properties write application logic that controls how application responds to user actions you may extend (subclass) existing components to create custom components

AWT Graphics Toolkit - java.awt Java's first GUI toolkit uses graphical UI components of operating system, e.g. Windows, MacOS, X-windows. efficient, low overhead applications look different on each platform difficult to write and test good quality applications different bugs on each OS, hence the slogan... "Write once, debug everywhere"

import java.awt.*; public class AwtDemo { Frame frame; AWT Example public AwtDemo( ) { frame = new Frame("Please Login"); // create components Label label = new Label("Username:"); TextField input = new TextField(12); Button button = new Button("Login"); // layout components in the container frame.setlayout(new FlowLayout()); frame.add(label); frame.add(input); frame.add(button); frame.pack(); }

AWT Example (continued) To display the window, call setvisible( true ) } public static void main(string [] args) { AwtDemo demo = new AwtDemo( ); demo.frame.setvisible( true ); }

Swing Graphics Framework Improved, OS-independent GUI framework classes in javax.swing and sub-packages Swing implements all components in software applications look & behave the same on all platforms applications may not look like native GUI apps (but you can change that by applying a LookAndFeel) part of the Java Foundation Classes (JFC). JFC also includes a 2D API and drag-and-drop API

Swing Containers JFrame is the top level container for most applications has a title bar, menu bar, and content pane JFrame is a heavy weight component.

Swing Example import javax.swing.*; public class SwingDemo { JFrame frame; public SwingDemo( ) { frame = new JFrame("Please Login"); // create components JTextField label = new JLabel("Username:"); JTextField input = new JTextField(12); JButton button = new JButton("Login"); // layout components in the container ContentPane pane = frame.getcontentpane(); pane.setlayout(new FlowLayout()); pane.add(label); pane.add(input); pane.add(button); pane.pack(); }

Swing Example (continued) To display the window use setvisible( true ) } public static void main(string [] args) { SwingDemo demo = new SwingDemo( ); demo.frame.setvisible( true ); } This is Bad Programming! We should not directly access the private attributes of an object. We should invoke public behavior instead.

Demo Create the simple "Login" interface.

What is Running My Program? In the main method, setvisible(true) returns immediately. main() exits. But, the application is still running! What is in control? Where is the control of flow? } public static void main(string [] args) { SwingDemo demo = new SwingDemo( ); demo.frame.setvisible( true ); System.out.println("UI started. Bye."); }

Swing creates its own Thread Swing runs in a separate thread. Or more than one thread. One thread is special: the Event Dispatcher Thread The Event Dispatcher Thread receives and "dispatches" all events involving the UI. You should perform all UI updates in this thread.

Top Level Containers A window that be displayed on screen: JFrame - title bar, menu, and content pane JWindow - no title bar or menu. JDialog - a dialog window, has title bar JApplet - for Applets (run in web browser). Deprecated now.

What's a Component? Also called "widgets". Label Textbox Button Slider Checkbox ComboBox RadioButton ProgressBar xxx

Know your components You need to know the available components... and what they can do. Visual Guide to Components Sun's Java Tutorial /tutorial/ui/features/components.html for Windows: tutorial/ui/features/compwin.html

What Can a Component Do? Common Behavior setlocation( x, y ) seticon( imageicon ) setbackground( color ) setforeground( color ) setenabled( boolean ) setvisible( boolean ) settooltiptext( string )

Important Components to Know Component and JComponent provide most of the behavior that all components have! Object Component Container AWT JComponent Window Panel Swing JPanel JTextBox JLabel JSlider JList JButton Frame Applet JComboBox JMenuItem JFrame JApplet

Hierarchy of Graphics Components Component paint( graphics ) resize(... ) JComponent Liskov Substitution Principle: Any place where the program is expecting a Component you can use a JButton, JLabel, etc. and the program will work correctly. JButton JLabel MyComponent... JTextbox

Playing with a JButton import java.awt.*; import javax.swing.*; JButton button = new JButton( "Press Me" ); //TODO: add button to a frame and pack button.setbackground( Color.YELLOW ); button.setforeground( Color.BLUE ); button.settooltiptext( "Make my day." ); button.setfont( new Font( "Arial", Font.BOLD, 24 ) ); button.setenabled( true );

Components don't have to be boring filename, URL, or InputStream. Can by GIF, JPEG, PNG ImageIcon icon = new ImageIcon("d:/images/fish.png"); button.seticon( icon );

Containers and Components container A GUI has many components in containers. Use add to put component in a container. A container is also a component; so a container may contain other containers. component add(component) component container component add(...) container component component add(component) component

Lightweight Containers A lightweight container is one that is not a window. You must place it inside another container. Cannot be drawn on screen by itself. JPanel simple rectangular area - most common JTabbedPane - multiple panels with a tab on top JSplitPane JInternalFrame - like a JFrame inside a JFrame

Learning Java Graphics Java Tutorial: Creating a GUI with JFC/Swing The section "Using Swing Components" contains "How To..." examples for many components. Good explanation and examples of Layout Managers. "JDK Demos and Samples" jdk_dir/demo/jfc/* Great demos with jar files (run) and source code. http://www.oracle.com/technetwork/java/javase/download s/index.html Big Java, Chapter 19.

WindowBuilder for Eclipse A graphical editor for creating UI with Swing or SWT. Windowbuilder is included in Eclipse, but you may need to download & install extra components. To find the Window Builder "Update Site" for your version of Eclipse, see: http://www.eclipse.org/windowbuilder/download.php