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

Similar documents
Introduction to the JAVA UI classes Advanced HCI IAT351

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphical User Interface (GUI)

Introduction. Introduction

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

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

CS 251 Intermediate Programming GUIs: Components and Layout

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

Part I: Learn Common Graphics Components

Java Graphical User Interfaces

Dr. Hikmat A. M. AbdelJaber

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

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

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

Java. GUI building with the AWT

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

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

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

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

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects

Graphical User Interface (GUI)

User interfaces and Swing

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

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

Top-Level Containers

Overloading Example. Overloading. When to Overload. Overloading Example (cont'd) (class Point continued.)

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

FirstSwingFrame.java Page 1 of 1

Building Graphical User Interfaces. GUI Principles

Programming Mobile Devices J2SE GUI

Java Swing Introduction

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

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

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

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Building Graphical User Interfaces. Overview

State Application Using MVC

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

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!

INTRODUCTION TO (GUIS)

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

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

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Systems Programming Graphical User Interfaces

CHAPTER 2. Java Overview

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

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

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

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

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

Graphical User Interfaces. Comp 152

CS 2113 Software Engineering

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

Graphical User Interfaces in Java - SWING

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

Graphical User Interfaces

Basicsof. JavaGUI and SWING

Packages: Putting Classes Together

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

Using Several Components

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

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

Building Java Programs Bonus Slides

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

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

Java: Graphical User Interfaces (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

BASICS OF GRAPHICAL APPS

H212 Introduction to Software Systems Honors

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

Agenda. Container and Component

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

DM503 Programming B. Peter Schneider-Kamp.

Containers and Components

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

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

MIT AITI Swing Event Model Lecture 17

Chapter 7: A First Look at GUI Applications

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

PIC 20A GUI with swing

Example: Building a Java GUI

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

Lecture 9. Lecture

SINGLE EVENT HANDLING

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

2110: GUIS: Graphical User Interfaces

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Object Oriented Design & Patterns. Part 1

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

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

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

GUI in Java TalentHome Solutions

JLayeredPane. Depth Constants in JLayeredPane

CS11 Java. Fall Lecture 4

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

Chapter 12 Advanced GUIs and Graphics

Chapter 9 Designing Graphical User Interfaces (GUIs)

Window Interfaces Using Swing. Chapter 12

Transcription:

Java GUI (intro) JFC Java Foundation Classes encompass a group of features for building Graphical User Interfaces (GUI). javax.swing.* used for building GUIs. Some basic functionality is already there for you to reuse ready made components, buttons, progress bars, text fields etc. drag and drop support internationalization There is no change in the way you code, compile and run. 1

Things to remember... There is always a hierarchy of components the hierarchy has a root Components can be added to the containers you need to take care of how to place these components layout It's all events and actions from then on! every user interaction with the graphical environment causes an event you need to declare listeners to capture events and perform actions accordingly Finally pack and make visible your graphical environment. 2

Hello World! import javax.swing.*; public class HelloWorldSwing { private static void createandshowgui() { //Make sure we have nice window decorations JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("HelloWorldSwing"); frame.setdefaultcloseoperation(jframe.exit_on_close); //Add the ubiquitous "Hello World" label. JLabel label = new JLabel("Hello World"); frame.getcontentpane().add(label); //Display the window. frame.pack(); frame.setvisible(true); public static void main(string[] args) { createandshowgui(); 3

Hierarchy of Components Top level containers Top of any Swing hierarchy e.g. Applet, Dailog, Frame General purpose containers Intermediate containers for multiple uses e.g. tool bar, tabbed pane, scroll pane Special purpose containers Intermediate containers with a special role e.g Intenal Frame, Layered Pane Basic controls Atomic components get information from the user e.g. slider, list, combo box. 4

Hierarchy of Components Uneditable Information Displays Atomic components solely used to display information to the user e.g. label, progress bar, tool tip Interactive Displays Atomic components for displaying specialized formatted ionformation e.g. File Chooser, table, tree 5

Using Top Level Containers Every GUI component must be part of a containment hierarchy. A containment hierarchy is a tree of components that has a top-level container as its root. Each GUI component can be contained only once. Each top-level container has a content pane that contains (directly or indirectly) the visible components in that top-level container's GUI. As a rule, a standalone application with a Swing-based GUI has at least one containment hierarchy with a JFrame as its root 6

Adding Components to a Content Pane Create Frame set title name and default close operation Create a menu bar JMenuBar - set opaque if it is to be used as a pane Create a label Set menu Bar specialized position Add label to content pane import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TopLevelDemo { private static void createandshowgui() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("TopLevelDemo"); frame.setdefaultcloseoperation( JFrame.EXIT_ON_CLOSE); JMenuBar cyanmenubar = new JMenuBar(); cyanmenubar.setopaque(true); cyanmenubar.setbackground(color.cyan); cyanmenubar.setpreferredsize( new Dimension(200, 20)); JLabel yellowlabel = new JLabel(); yellowlabel.setopaque(true); yellowlabel.setbackground(color.yellow); yellowlabel.setpreferredsize( new Dimension(200, 180)); frame.setjmenubar(cyanmenubar); frame.getcontentpane().add(yellowlabel, BorderLayout.CENTER); frame.pack(); frame.setvisible(true); public static void main(string[] args) { createandshowgui(); 7

Adding to Content Panes A content pane is a Container which provides add methods which takes as arguments some other component instance to be added to the container coordinates (with restrictions) about its placement in the container. The layout of components inside a container can be customized. Java provides some policies GridBagLayout, GridLayout, SpringLayout, BorderLayout, BoxLayout Depending on your needs full screen, space between components, re-sizable as the main window is being resized, etc 8

Using Layout Managers Layout Manager is an object which determines the size and position of components inside a containers. You need to worry about layout managers for JPanel (default layout manager FlowLayout) and content panes (default layout manager BorderLayout) Setting your layout manager JPanel frame = new JPanel(new FlowLayout()); or after creation of a JPanel Conainer contentpane = frame.getconcontentpane(); contentpane.setlayout(new BorderLayout()); 9

FlowLayout Adds components in a row one next to each other. if the total width of the added components is longer than their containers width, successive components are placed in a new row. 10

Border Layout Adds components filling in the whole window allows positioning usign NORTH, WEST, SOUTH, EAST, CENTER import java.awt.*; import java.applet.applet; public class buttondir extends Applet { public void init() { setlayout(new BorderLayout()); add(new Button("North"), BorderLayout.NORTH); add(new Button("South"), BorderLayout.SOUTH); add(new Button("East"), BorderLayout.EAST); add(new Button("West"), BorderLayout.WEST); add(new Button("Center"), BorderLayout.CENTER); 11

Border Layout Adds components filling in the whole window PAGE_START, CENTER, LINE_START, LINE_END, PAGE_END import java.awt.*; import java.applet.applet; public class buttondir extends Applet { public void init() { JButton button = new Jbutton( "Button 1 (PAGE_START)"); pane.add(button, BorderLayout.PAGE_START); button = new JButton("Button 2 (CENTER)"); button.setpreferredsize(new Dimension(200, 100)) pane.add(button, BorderLayout.CENTER); button = new JButton("Button 3 (LINE_START)"); pane.add(button, BorderLayout.LINE_START); button = new Jbutton( "Long Named Button 4 (PAGE_END)"); pane.add(button, BorderLayout.PAGE_END); button = new JButton("5 (LINE_END)"); pane.add(button, BorderLayout.LINE_END); 12

GridBagLayout Very flexible allows components of different sizes spanning ceveral rows and/or columns Every component has to be added with an instance of GridBagLayout an instance of GridBagConstraints Each GridBagLayout object maintains a dynamic, rectangular grid of cells, with each component occupying one or more cells, called its display area. 13

GridBagLayout (cont) import java.awt.*; import java.util.*; import java.applet.applet; public class GridBagEx1 extends Applet { protected void makebutton(string name, GridBagLayout gridbag, GridBagConstraints c) { Button button = new Button(name); gridbag.setconstraints(button, c); add(button); ` public static void main(string args[]) { Frame f = new Frame( "GridBag Layout Example"); GridBagEx1 ex1 = new GridBagEx1(); ex1.init(); f.add("center", ex1); f.pack(); f.setsize(f.getpreferredsize()); f.show(); 14

GridBagLayout (cont) public void init() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setlayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; makebutton("button1", gridbag, c); makebutton("button2", gridbag, c); makebutton("button3", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; makebutton("button4", gridbag, c); c.weightx = 0.0; makebutton("button5", gridbag, c); c.gridwidth = GridBagConstraints.RELATIVE; makebutton("button6", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; makebutton("button7", gridbag, c); c.gridwidth = 1; c.gridheight = 2; c.weighty = 1.0; makebutton("button8", gridbag, c); c.weighty = 0.0; c.gridwidth = GridBagConstraints.REMAINDER; c.gridheight = 1; makebutton("button9", gridbag, c); makebutton("button10", gridbag, c); setsize(300, 100); 15

Giving life to your GUI components Every time the user performs an action on one of the GUI's component it (the action) creates an event Objects can be notified of events, the object has to implement the appropriate interface be registered as an event listener on the appropriate event source User Actions Listener Type click a button, press Enter while typing Close a window press mouse button moves mouse over keyboard focus ActionListener WindowListener MouseListener MouseMotionListener FocusListener 16

Implementing a Listener Two steps (a) the declaration of the event handler class, implement the appropriate interface (b) register an instance of the listener one (or more) components import javax.swing.*; import java.awt.toolkit; import java.awt.borderlayout; import java.awt.event.actionlistener; import java.awt.event.actionevent; public class Beeper extends JPanel implements ActionListener { JButton button; public Beeper() { super(new BorderLayout()); button = new JButton("Click Me"); button.setpreferredsize(new Dimension(200, 80)); add(button, BorderLayout.CENTER); button.addactionlistener(this); public void actionperformed(actionevent e) { Toolkit.getDefaultToolkit().beep(); 17

Multiple Listeners You can add more than one Listeners to componets e.g. to a JButton, check with the API first one event can cause multiple actions public class MultiListener... implements ActionListener {... //where initialization occurs: button1.addactionlistener(this); button2.addactionlistener(this); button2.addactionlistener(new Eavesdropper(bottomTextArea)); public void actionperformed(actionevent e) { toptextarea.append(e.getactioncommand() + newline); class Eavesdropper implements ActionListener {... public void actionperformed(actionevent e) { mytextarea.append(e.getactioncommand() + newline); 18

Inner Classes Java allows the definition of a class inside another class public class ExternalClass {... class InternalClass {...... InternalClass exists inside an instance of an ExternalClass. InternalClass has direct access to members of ExternalClass instance of ExternalClass instance of InternalClass 19

Using inner classes Recall the Stack example we can enumeration capabilities to Stack public class Stack { private Vector items;...//code for Stack's methods and constructors not shown... public Enumeration enumerator() { return new StackEnum(); class StackEnum implements Enumeration { int currentitem = items.size() 1; public boolean hasmoreelements() { return (currentitem >= 0); public Object nextelement() { if (!hasmoreelements()) throw new NoSuchElementException(); else return items.elementat(currentitem ); 20

Anonymous inner classes Inline class definition without giving a name to the class public class SomeGUI extends JFrame{ //button member declarations... protected void buildgui(){ button1 = new JButton(); button2 = new JButton();... button1.addactionlistener( new java.awt.event.actionlistener(){ public void actionperformed(java.awt.event.actionevent e){ // do something ); button2.addactionlistener( new java.awt.event.actionlistener(){ public void actionperformed(java.awt.event.actionevent e){ // do something else ); 21

Compare public class SomeGUI extends JFrame{ //button member declarations... protected void buildgui(){ button1 = new JButton(); button2 = new JButton();... button1.addactionlistener( new java.awt.event.actionlistener(){ public void actionperformed (java.awt.event.actionevent e){ // do something ); button2.addactionlistener( new java.awt.event.actionlistener(){ public void actionperformed (java.awt.event.actionevent e){ // do something else ); public class SomeGUI extends JFrame{ //button member declarations... protected void buildgui(){ button1 = new JButton(); button2 = new JButton();... class Button1Action implements ActionListener{ public void actionperformed(actionevent e){ // do something class Button2Action implements ActionListener{ public void actionperformed(actionevent e){ // do something else button1.addactionlistener(new Button1Action()); button2.addactionlistener(new Button2Action()); 22

Reasons for inner classes More readable code all information for how to handle the event is located in one file for a novice this might be difficult to parse at first Better encapsulation the inner class can be declared private and thus only accessible to its enclosing class e.g. the connection to a database server can be captured as an inner class limiting the classes that can directly connect to the database, enforcing the connection protocol one point of control! 23