CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■

Slides:



Advertisements
Similar presentations
Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.
Advertisements

Standard Components and Their Events GUI interface elements are represented by subclasses of component: –Button class –Label class –TextField class –Checkbox.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
12-Jul-15 Components. 2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField.
Chapter 20 Event Handling. Event handling Is the Core of successful applet Programming For the user to interact with a GUI, the underlying operating system.
CST JavaScript Validating Form Data with JavaScript.
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.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
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.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
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.
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.
1 Lesson: Applets with User Input and Output with GUI ICS4M.
1 Lecture 6 Using AWT controls, Layout Managers, and Menus.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
Object Oriented Programming.  Interface  Event Handling.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
May 12, 1998CS102-01Lecture 7-3 Building GUIs in Java I CS Lecture 7-3 A picture's worth a thousand words.
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.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
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.
IMS 3253: Controls 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Check Boxes Radio Buttons Date Time Picker Masked.
Java Swing Controls. JButton One of the most commonly used swing component is Push Button or simply button which is created through JButton class of Swing.
Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.
1 IM103 week 8 (C&K ch17, p412) Advanced graphic programming Learning objectives By the end of this chapter you should be able to:  create dialogue windows.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
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 Fifth Edition Chapter 13 Introduction to Swing Components.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
Chapter 5 Validating Form Data with JavaScript
Aum Amriteshwaryai Namah
TOPICS Labels Using Buttons Applying Check Boxes CheckBox Group
Abstract Window ToolKit (AWT)
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
AWT Components and Containers
OBJECT ORIENTED PROGRAMMING
Programming Graphical User Interface (GUI)
AWT Components Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e.
A picture's worth a thousand words
Presentation transcript:

CSI 3125, Preliminaries, page 1 AWT Control

CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■ Push buttons ■ Check boxes ■ Choice lists ■ Lists ■ Scroll bars ■ Text editing

CSI 3125, Preliminaries, page 3 AWT Control Adding and Removing Controls To add a control, must first create an instance of the desired control and then add it to a window by calling add( ), which is defined by Container. To remove a control from a window when the control is no longer needed, call remove( ). This method is also defined by Container.

CSI 3125, Preliminaries, page 4 AWT Control Label A label is an object of type Label, and it contains a string, which it displays. Labels are passive controls that do not support any interaction with the user. Label defines the following constructors: Label( ) Label(String str) Label(String str, int how) The first version creates a blank label. The second version creates a label that contains the string specified by str. This string is left-justified. The third version creates a label that contains the string specified by str using the alignment specified by how. The value of how must be one of these three constants: Label.LEFT, Label.RIGHT, or Label.CENTER.

CSI 3125, Preliminaries, page 5 AWT Control Label setText( ) :- set the text in a label void setText(String str) getText( ):- obtain the current label String getText( ) setAlignment(int alignment) Sets the alignment for this label to the specified alignment.

CSI 3125, Preliminaries, page 6 AWT Control Label import java.applet.Applet; import java.awt.Label; /* */ public class CreateLable extends Applet{ public void init(){ Label label1 = new Label("popo"); Label label2 = new Label("Right Aligned POPO", Label.RIGHT); Label label3 = new Label("Center Aligned popo", Label.CENTER); add(label1); add(label2); add(label3); }

CSI 3125, Preliminaries, page 7 AWT Control Button Button is a control component that has a label and generates an event when pressed. When a button is pressed and released, AWT sends an instance of ActionEvent to the button, by calling processEvent on the button. Button defines these two constructors: Button( ) Button(String str) The first version creates an empty button. The second creates a button that contains str as a label. After a button has been created, you can set its label by calling setLabel( ). You can retrieve its label by calling getLabel( ). void setLabel(String str) String getLabel( )

CSI 3125, Preliminaries, page 8 AWT Control Button import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* */ public class Button1 extends Applet{ String s; public void init(){ Button b1=new Button("Press Me"); add(b1); b1.addMouseListener(new m()); } public class m extends MouseAdapter { public void mousePressed(MouseEvent e) { s="MousePressed";repaint(); } public void paint(Graphics g) { g.drawString(s,50,100); }

CSI 3125, Preliminaries, page 9 AWT Control Check box Checkbox control is used to turn an option on(true) or off(false). There is label for each checkbox representing what the checkbox does. The state of a checkbox can be changed by clicking on it.

CSI 3125, Preliminaries, page 10 AWT Control Check box Checkbox supports these constructors: Checkbox( ) Checkbox(String str) Checkbox(String str, boolean on) Checkbox(String str, boolean on, CheckboxGroup cbGroup) Checkbox(String str, CheckboxGroup cbGroup, boolean on) The first form creates a check box whose label is initially blank. The state of the check box is unchecked. The second form creates a check box whose label is specified by str. The state of the check box is unchecked. The third form allows you to set the initial state of the check box. The fourth and fifth forms create a Checkbox with the specified label, set to the specified state, and in the specified check box group

CSI 3125, Preliminaries, page 11 AWT Control Check box import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* */ public class chk extends Applet{ public void init(){ Checkbox ch1 = new Checkbox("Apple"); Checkbox ch2 = new Checkbox("Mango"); Checkbox ch3 = new Checkbox("Peer"); add(ch1); add(ch2); add(ch3); } }

CSI 3125, Preliminaries, page 12 AWT Control Check box with Itemlistener import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* */ public class chk extends Applet implements ItemListener{ String s; Checkbox ch1 = new Checkbox("Apple"); Checkbox ch2 = new Checkbox("Mango“, true); public void init(){ add(ch1); add(ch2); ch1.addItemListener(this); ch2.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) {s="current state of Apple "+ch1.getState(); g.drawString(s,100,100); s="current state of Mango "+ch2.getState(); g.drawString(s,100,150); } }

CSI 3125, Preliminaries, page 13 AWT Control CheckboxGroup It is possible to create a set of mutually exclusive check boxes in which one and only one check box in the group can be checked at any one time.

CSI 3125, Preliminaries, page 14 AWT Control CheckboxGroup import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* */ public class chk2 extends Applet implements ItemListener{ String s; CheckboxGroup cbg=new CheckboxGroup(); Checkbox ch1 = new Checkbox("Apple",cbg,true); Checkbox ch2 = new Checkbox("Mango",cbg,false); public void init(){ add(ch1); add(ch2); ch1.addItemListener(this); ch2.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) {s="current state of Apple "+ch1.getState(); g.drawString(s,100,100); s="current state of Mango "+ch2.getState(); g.drawString(s,100,150); }

CSI 3125, Preliminaries, page 15 AWT Control Choice Choice control is used to show pop up menu of choices. Selected choice is shown on the top of the menu. Choice only defines the default constructor, which creates an empty list. Choice() To add a selection to the list, call add( ). void add(String name) To determine which item is currently selected, call either getSelectedItem( )or getSelectedIndex( ). String getSelectedItem( ) int getSelectedIndex( ) To obtain the number of items in the list, call getItemCount( ).

CSI 3125, Preliminaries, page 16 AWT Control Choice import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* */ public class cho1 extends Applet implements ItemListener{ String s; Choice c1=new Choice(); public void init(){ c1.add("Elsin "); c1.add("Gloriya"); c1.add("Jinson"); c1.add("Ajith"); c1.add("Dilu"); c1.select("Ajith"); add(c1); c1.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { s="Item Selected "+c1.getSelectedItem()+" Index "+c1.getSelectedIndex( ); g.drawString(s,100,100); }

CSI 3125, Preliminaries, page 17 AWT Control List List represents a list of text items. The list can be configured to that user can choose either one item or multiple items. List provides these constructors: List( ) List(int numRows) List(int numRows, boolean multipleSelect) The first version creates a List control that allows only one item to be selected at any one time. In the second form, the value of numRows specifies the number of entries in the list that will always be visible In the third form, if multipleSelect is true, then the user may select two or more items at a time. If it is false, then only one item may be selected.

CSI 3125, Preliminaries, page 18 AWT Control List To add a selection to the list, call add( ). It has the following two forms: void add(String name) void add(String name, int index) Here, name is the name of the item added to the list. The first form adds items to the end of the list. The second form adds the item at the index specified by index. Indexing begins at zero. –1 to add the item to the end of the list. can determine which item is currently selected by calling either getSelectedItem( ) or getSelectedIndex( ).

CSI 3125, Preliminaries, page 19 AWT Control List Prog import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* */ public class list1 extends Applet implements ItemListener{ String s; List c1=new List(); public void init(){ c1.add("Elsin "); c1.add("Gloriya"); c1.add("Jinson"); c1.add("Ajith",0); add(c1); c1.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) {s="Item Selected "+c1.getSelectedItem()+" Index "+c1.getSelectedIndex( ); g.drawString(s,100,100); }

CSI 3125, Preliminaries, page 20 AWT Control Scrollbar Scrollbar control represents a scroll bar component in order to enable user to select from range of values. Scrollbar defines the following constructors: Scrollbar( ) Scrollbar(int style) Scrollbar(int style, int initialValue, int thumbSize, int min, int max) The first form creates a vertical scroll bar. The second and third forms allow you to specify the orientation of the scroll bar. If style is Scrollbar.VERTICAL, a vertical scroll bar is created. If style is Scrollbar.HORIZONTAL, the scroll bar is horizontal. In the third form of the constructor, the initial value of the scroll bar is passed in initialValue. The number of units represented by the height of the thumb is passed in thumbSize. The minimum and maximum values for the scroll bar are specified by min and max To obtain the current value of the scroll bar, call getValue( ).

CSI 3125, Preliminaries, page 21 AWT Control Scrollbar prog import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* */ public class scr1 extends Applet implements AdjustmentListener{ String s; Scrollbar s1=new Scrollbar(Scrollbar.HORIZONTAL,0,20,1,500); public void init(){ add(s1); s1.addAdjustmentListener(this); } public void adjustmentValueChanged(AdjustmentEvent ae) { repaint();} public void paint(Graphics g) {s="Item Selected "+s1.getValue( ); g.drawString(s,100,100); setBackground(new Color(s1.getValue( ),100,230) ); }

CSI 3125, Preliminaries, page 22 AWT Control TextField The textField component allows the user to edit single line of text. When the user types a key in the text field the event is sent to the TextField. TextField defines the following constructors: TextField( ) TextField(int numChars) TextField(String str) TextField(String str, int numChars) The first version creates a default text field. The second form creates a text field that is numChars characters wide. The third form initializes the text field with the string contained in str. The fourth form initializes a text field and sets its width. To obtain the string currently contained in the text field, call getText( ). To set the text, call setText( ).

CSI 3125, Preliminaries, page 23 AWT Control TextField prog import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* */ public class text1 extends Applet implements ActionListener{ String s; TextField t1=new TextField(20); public void init(){ add(t1); t1.addActionListener(this); } public void actionPerformed(ActionEvent ae) { repaint();} public void paint(Graphics g) {s="Text content "+t1.getText( ); g.drawString(s,100,100); }

CSI 3125, Preliminaries, page 24 AWT Control

CSI 3125, Preliminaries, page 25 AWT Control

CSI 3125, Preliminaries, page 26 AWT Control

CSI 3125, Preliminaries, page 27 AWT Control