Canvas Button Event Check Box Choice Label Object Component List

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
Components. Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent –TextArea –TextField.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Using AWT Controls, Layout Managers, and Menus Controls are components that allow a user to interact with your application in various ways A layout manager.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
9.1 Java Packages A collection of classes Allows classes to be grouped arbitrarily Hierarchical structure independent of inheritance Classes can.
Object Oriented Programming.  Interface  Event Handling.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
Csc Basic Graphical User Interface (GUI) Components.
AWT Package. Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program.
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Event and GUI programming 1. Event Handling in Java An object resides in a particular state until it is made to transit to other state. This transition.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Chapter 3: Widgets for a GUI General Component methods Useful widgets classes –Text classes Label TextField TextArea –Active widgets Button Checkbox Choice.
AWT AWT stands for Abstract Windowing Toolkit. It contains all classes to write the program that interface between the user and different windowing toolkits.
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 5
CSC 205 Programming II Lecture 5 AWT - I.
Welcome To java
Fundamental of Java Programming Abstract Window Toolkit
Chapter 12 Event-Driven Programming
Aum Amriteshwaryai Namah
Advanced GUIs II CS Lecture
Lecture 09 Applets.
Abstract Window ToolKit (AWT)
GUI Event Handling Nithya Raman.
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
AWT Components and Containers
UNIT-5.
Programming Graphical User Interface (GUI)
Advanced GUIs II CS Lecture
Advanced GUIs and Graphics
Presentation transcript:

Canvas Button Event Check Box Choice Label Object Component List Scrollbar CheckboxGroup Text Field Textcomponent Container Text Area Window Frame Panel File Dialog Dialog

All the Components of the AWT can be organized into 3 Categories Visual Components : Like Button, Text Field, Checkbox and List Container Components : Like Frame, Applet, Dialog and Panel Menu Components : Like Menu Items, Checkbox menu items and Separators

Containers Container is a class which contain components, on which layout managers act. Panel Applet Object Component Container Frame Window Dialog FileDialog

Container Hierarchy Panel : Difference between Window and Panel is that Panels do not have a graphic representation, not even an outline. Applet is a subclass of Panel. Panels work always inside Windows or Applets. It must be added to some other container. Panel is a container that has no visible boundaries and cannot handle events and is used to hold components collectively as a group. Frame : is a top level window in which components are placed. Normally to create a Java window we extend the Frame class. Dialog : is used for implementing direct application to user feedback or questions. Typical uses include pop-warning dialog windows, quit confirmation dialog boxes, etc.. The dialog class is not a self- contained component FileDialog : is used to do file operations like Open / Save / SaveAs and also we can create directories.

LABEL A Label displays a string on the screen. Labels are used to display the information what user has to do with the components (like “Enter your name” before a text field). BUTTON Button is an important component on the screen with which the user can trigger an action.

Checkbox A checkbox has two states on and of .A checkbox is generally used where user has to choose between two options like male and female. A checkbox is associated with a label indicating for what checkbox is meant. CheckboxGroup It is possible to make the group of checkboxes, so that the user can choose only one out of many choices.

Textfield allows only single line of entry by the user. Textfield is used for user input and button is for generating action as per the user input. Textfield allows only single line of entry by the user. TextField and TextArea are the sub classes of TextComponent. TextArea With TextArea user can enter multiple lines of text,TextArea includes scroll bars so that large amounts of Data can be entered are displayed.

Choice A Choice object is used to present the user with a pop-up list of options, from which user can select exactly one . Choice is preferred in place of radio buttons , when the options are many. List List displays the items like choice, it is used to display much longer list of items. List allows user to choose multiple selection and if set to false , allows single selection.

AWT provides canvas for drawing. Canvas receives mouse events. A canvas has got its own graphics context. Scrollbar The scrollbar component is an up-down or left –right slider that can be used to set a numeric value.

Event Handling An event occurs when a user clicks on a button or a mouse click or when a scroll bar is adjusted etc. An event listener is an object to which a component has delegated the task of handling a particular kind of event. A listener must implement the interface that contains the event-handling method

Java.util.EventObject Java.util.Event ActionEvent AdjustmentEvent Item Event TextEvent ComponentEvent ContainerEvent FocusEvent pointEvent WindowEvent InutEvent KeyEvent MouseEvent

Listener Type Act that results in the event ActionListener User clicks a button, press Return while typing in a text field or choosing a menu item WindowListener User class a frame ( main window ) MouseListener User presses a mouse button while the cursor is on a component MouseMotionListener User moves the mouse over a component ComponentListener Component becomes visible FocusListener Components gets the keyboard focus

actionPerformed(ActionEvent) AdjustmentListener INTERFACE INTERFACE METHODS ADD METHOD ActionListener actionPerformed(ActionEvent) addActionListener(ActionListener) AdjustmentListener adjustmentValueChanged(AdjustmentEvent) addAdjustmentListener(AdjustmentListener) ComponentListener componentHidden(ComponentEvent) componentMoved(ComponentEvent) componentResized(ComponentEvent) componentShown(ComponentEvent) addComponentListener(ComponentListener) ContainerListener componentAdded(ContainerEvent) componentRemoved(ContainerEvent) addContainerListener(ContainerListener) FocusListener focusGained(FocusEvent) focusLost(FocusEvent) addFocusListener(FocusListener) ItemListener itemStateChanged(ItemEvent) addItemListener(ItemListener)

All these methods return void. KeyListener keyPressed(KeyEvent) addKeyListener(KeyListener) MouseListener mouseClicked(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mousePressed(MouseEvent) mouseReleased(MouseEvent) addMouseListener(MouseListener) MouseMotionListener mouseDragged(MouseEvent) mouseMoved(MouseEvent) addMouseMotionListener(MouseMotionListener) WindowListener windowActivated(WindowEvent) windowDeactivated(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowOpened(WindowEvent) windowClosed(WindowEvent) windowClosing(WindowEvent) addWindowListener(WindowListener) All these methods return void.

ActionListener ItemListener AdjustmentListener TextListener Button TextField MenuItem ListItem ItemListener Choice Checkbox CheckboxMenuItem AdjustmentListener Scrollbar TextListener Textfield TextArea

ComponentListener FocusListener MouseListener MouseMotionListener KeyListener For all Components derived from Component class ContainerListener For all classes derived from Container class WindowListener For all classes derived from Window class

Layout Managers FlowLayout BorderLayout Object GridLayout CardLayout GridBagLayout

This is default layout manager for Applets and Panels FlowLayout This is default layout manager for Applets and Panels Components are arranged from left to right and then overflowing to the next line when the first line is filled.

This is the default layout manager for the Frame. BorderLayout This is the default layout manager for the Frame. It allows the components to be laid out along top, bottom, left or right edges. Any unassigned areas will be recovered by the central component.

GridLayout Components are arranged in a grid fashion. This layout manager breaks up the container into a series of equally sized grid squares.. Each grid square holds exactly one component. The size of all the grid squares defaults to the size of the largest component.

CardLayout This layout works on the concept of keeping a number of cards, one above the other, but the top one only being visible. This layout style is often used to show one page among a number of pages like a worksheet in a MS-Excel workbook. The desired worksheet can be made visible by clicking over the tabs. This is useful for instances where a “slide show” effect needs to be produced.

CardLayout CardLayout() first(Panel p) last(Panel p) next(Panel p) previous(Panel p) showPanel(Panel p)

Create a Panel for which the layout is CardLayout Create sub panels and add the components individually to each of these sub panels ( Layout of these sub panels can be anything ) Add these sub panels to the panel for which the layout is CardLayout Add this main panel to the actual container.

GridBagLayout It supports components of different sizes, and we can specify layout options for each component. It supports a rectangular grid of cells. Each component in a GridBagLayout can occupy one or more cells.

Component void setSize(int w, int h) void setSize(Dimension d) Dimension getSize() void setFont(Font f) Font getFont() void setBackground(Color obj) Color getBackground() void setForeground(Color obj) Color getForeground() void invalidate() void validate() void repaint()

void update(Graphics g) void paint(Graphics g) void requestFocus() Graphics getGraphics() void addComponentListener(ComponentListener obj) void removeComponentListener(ComponentListener obj) void addMouseListener(MouseListener obj) void removeMouseListener(MouseListener obj) void addKeyListener(KeyListener obj) void removeKeyListener(KeyListener obj) void addMouseMotionListener(MouseMotionListener obj) void removeMouseMotionListener(MouseMotionListener obj) void addFocusListener(FocusListener obj) void removeFocusListener(FocusListener obj) void setEnabled(Boolean) Boolean isEnabled() void setVisible(Boolean) Boolean inVisible()

Container void add(Component c) void add(String s, Component c) void add(Component c, ObjectConstraints) void remove(Component c) void remove(int index) void addContainerListener(ContainerListener obj) void removeContainerListener(ContainerListener obj) void setLayout(LayoutManager obj) LayoutManager getLayout()

Button Button() Button(String label) setLabel(String s) String getLabel() void addActionListener(ActionListener obj) void removeActionListener(ActionListener obj)

Window Frame Frame() Frame(String title) Void setMenuBar(MenuBar mb) void addWindowListener(WindowListener obj) void removeWindowListener(WindowListener obj) dispose() void setTitle(String s) String getTitle() Frame Frame() Frame(String title) Void setMenuBar(MenuBar mb) Void setTitle(String s) String getTitle()

TextComponent Void setText(String text) String getText() Void setSelectionStart(int index) int getSelectionStart() Void setSelectionEnd(int index) int getSelectionEnd() Void setCaretPosition(int pos) int getCaretPosition() String getSelectedText() Void addTextListener(TextListener obj) Void removeTextListener(TextListener obj)

TextField TextField() TextField(int cols) TextField(String text) TextField(String text, int cols) void setEchoChar(Char ch) Char getEchoChar() void addActionListener(ActionListener obj) void removeActionListener(ActionListener obj)

TextArea TextArea() TextArea(int rows, int cols) TextArea(String text) TextArea(String text, int rows, int cols) TextArea(String text, int rows, int cols, int Scrollbars) Void append(String str) Void insert(String str, int pos) Void replaceRange(String str, int start, int end)

Checkbox Checkbox() Checkbox(String label) Checkbox(String label, Boolean state) Checkbox(String label, CheckboxGroup x, Boolean state) void addItemListener(Itemlistener obj) void removeItemListener(Itemlistener obj) void setLabel(String label) String getLabel() void setCheckboxGroup(CheckboxGroup cbg) CheckboxGroup getCheckboxGroup() void setState(Boolean state) Boolean getState()

MenuBar Menu MenuBar() void add(Menu m) void remove(MenuComponent mc) void remove(int index) int getMenu(Component () Menu Menu(String item) void add(MenuItem m) void remove(MenuComponent mc) void remove(int index) int getItem(Count c)

MenuItem CheckboxMenuItem MenuItem(String name) MenuItem(String name,MenuShortcut obj) void addActionListener(ActionListener obj) void removeActionListener(ActionListener obj) void setShortcut(MenuShortcut obj) MenuShortcut getShortcut() CheckboxMenuItem CheckboxMenuItem(String item) void addItemListener(ItemListener obj) void removeItemListener(ItemListener obj)

Applets In JAVA, we can develop two types of applications. Applets Applets are small applications that are accessed on an internet server transported over the Net, automatically installed and runs as part of a web document. Applets are not executed by the console-based java runtime interpreter. They are executed by either a java compatible web browser or an appletviewer. Thus execution of an applet does not begin at main method.

appletviewer Appletviewer is a standalone program that we can use to test our applets. This comes freely along with sun’s JDK. Example: >Appletviewer firstApplet.html Note: Every applet program must be extending Applet class

Hierarchy Object Component Container Panel Applets

Applet Life Cycle An Applet life cycle is a cycle which stars with the opening of an HTML file or a web page in which the applet is loaded. The class Applet has four methods which constitute the applet life cycle. These four methods are also called milestones. public void init() public void start() public void stop() public void destroy()

init() to initialize the applet each time it’s loaded ( or reloaded). This is called only once in the life time of an applet. Start() to start the applet’s execution, such as when the applet’s loaded or when the user revisits a page that contains the applet. Stop() To stop the applet’s execution, such as when the user leaves the applet’s page or quits the browser. Destroy() To perform a final cleanup in preparation for unloading.

In the life time of an applet all the 4 methods will get call whether or not you override them. If you have overridden, the overridden method is called or else the function with default functionality coming from the base class is called. Whenever browser comes across the applet tag, it downloads those bytecodes and converts them into local language and then instantiates the applet class. An applet program must be always public, so as to make it available to the JVM of the browser.

Security restriction on an Applet An applet program can not access the local file system. It can’t ordinarily read or write files on the host that’s executing it. It can’t establish server connection with any machine other than the one from where it has been downloaded. It can’t start any program on the host that’s executing it. It can’t read certain system properties. Every browser has a security manager object that implements its security policies. When a security manager detects a violation it throws a security exception.

Calling applets To see our applets on the browser we need create a separate HTML file. After compiling our applet program we should include the applet.class file in the HTML file. Then we have to call the HTML file which contains our applet.class file to call to the applet in the browser.

Graphics The color of Foreground and Background. Graphics classes are defined in java.awt package. With Graphic classes we can manipulate The color of Foreground and Background. The font size, name and type of the characters can be dictated. The Polygon class helps us in drawing different geometrical figures like squares, rectangles, triangles. To draw graphics on the screen the co-ordinates where the graphics are to be displayed should be mentioned in the statements. JAVA’s co-ordinate system supports (x,y) co-ordinate system.

Graphics class is an abstract class, we can’t instantiate the class. The component class paint method ( public void paint(Graphics g) ) takes a graphics object as an argument. Paint method should be overridden in the source code. The paint method is called by the programmer.