🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

PPT - Unit 1.1 - AWT Components.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

ADVANCED JAVA PROGRAMMING AJP UNIT – I AWT [ABSTRACT WINDOWING TOOLKIT] Sunil Prakashrao Emekar Lecturer in Computer Engg. Government Polytechnic Karad E-mail: [email protected] JAVA AWT (ABSTRACT WINDOWING TOOLKIT) ï‚¢ Java AWT (Ab...

ADVANCED JAVA PROGRAMMING AJP UNIT – I AWT [ABSTRACT WINDOWING TOOLKIT] Sunil Prakashrao Emekar Lecturer in Computer Engg. Government Polytechnic Karad E-mail: [email protected] JAVA AWT (ABSTRACT WINDOWING TOOLKIT) ï‚¢ Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based applications in java. ï‚¢ Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. ï‚¢ AWT is heavyweight i.e. its components are using the resources of OS. ï‚¢ The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc. AWT CLASSES ï‚¢ The AWT classes are contained in the java.awt package. It is one of Java’s largest packages. ï‚¢ it is logically organized in a top- down, hierarchical fashion. ï‚¢ The AWT defines windows according to a class hierarchy that adds functionality and specificity with each level. Container: ï‚— A container is responsible for laying out (that is, positioning) any components that it contains. It does this through the use of various layout managers. ï‚— The classes that extends Container class are known as container such as Frame, Dialog and Panel. ï‚¢ Window ï‚— The window is the container that have borders and menu bars. You wont create object of window directly. ï‚— You must use frame, dialog or another window for creating a window. ï‚¢ Panel ï‚— The Panel is the container that doesn't contain title bar and menu bars. (Superclass of Applet) ï‚— It can have other components like button, textfield etc. ï‚¢ Frame ï‚— It is a subclass of Window and has a title bar, menu bar, borders, and resizing corners. ï‚— It can have other components like button, textfield etc. USEFUL METHODS OF COMPONENT CLASS WORKING WITH FRAME WINDOWS ï‚¢ After the applet, the type of window you will most often create is derived from Frame. ï‚¢ You will use it to create child windows within applets, and top-level or child windows for applications. ï‚¢ Frame’s constructors: ï‚— Frame( ) ï‚— Frame(String title) ï‚¢ Setting the Window’s Dimensions: ï‚— The setSize( ) method is used to set the dimensions of the window. ï‚— void setSize(int newWidth, int newHeight) ï‚¢ Hiding and Showing a Window ï‚— After a frame window has been created, it will not be visible until you call setVisible( ). ï‚— void setVisible(boolean visibleFlag) ï‚— The component is visible if the argument to this method is true. Otherwise, it is hidden. ï‚¢ Setting a Window’s Title ï‚— You can change the title in a frame window using setTitle( ), ï‚— void setTitle(String newTitle) import java.awt.*; class Test { public static void main(String args[]) { Frame f = new Frame(“Example"); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } } import java.awt.*; import java.applet.*; public class LabelDemo extends Applet { public void init() { Frame f=new Frame(); f.setSize(300,300); f.setVisible(true); } } USING AWT CONTROLS, LAYOUT MANAGERS, AND MENUS ï‚¢ Controls are components that allow a user to interact with your application in various ways. ï‚¢ The AWT supports the following types of controls: Labels Push buttons Check boxes Choice lists Lists Scroll bars Text editing ï‚¢ These controls are subclasses of Component. ADDING AND REMOVING CONTROLS ï‚¢ To include a control in a window, you must add it to the window. ï‚¢ To do this, you must first create an instance of the desired control and then add it to a window by calling add( ), To include a control in a window, you must add it to the window. ï‚¢ To do this, first create an instance of the desired control and then add it to a window by calling add( ) Component add(Component compObj) compObj is an instance of the control that you want to add. ï‚¢ Sometimes you will want to remove a control from a window when the control is no longer needed. To do this, call remove( ). This method is also defined by Container. ï‚¢ It has this general form: void remove(Component obj) obj is a reference to the control you want to remove. ï‚¢ You can remove all controls by calling removeAll( ) LABELS ï‚¢ The easiest control to use is a 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) ï‚¢ alignment specified by how. The value of how must be one of these three constants: Label.LEFT, Label.RIGHT, or Label.CENTER. ï‚¢ You can set or change the text in a label by using the setText( ) method. ï‚¢ You can obtain the current label by calling getText( ). ï‚— void setText(String str) ï‚— String getText( ) import java.awt.*; class LabelExample{ public static void main(String args[]){ Frame f= new Frame("Label Example"); Label l1,l2; l1=new Label("First Label."); l1.setBounds(50,100, 100,30); l2=new Label("Second Label."); l2.setBounds(50,150, 100,30); f.add(l1); f.add(l2); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } } // Demonstrate Labels import java.awt.*; import java.applet.*; public class LabelDemo extends Applet { public void init() { Label one = new Label("One"); Label two = new Label("Two"); Label three = new Label("Three"); // add labels to applet window add(one); add(two); add(three); } } USING BUTTONS ï‚¢ The most widely used control is the push button. A push button is a component that contains a label and that generates an event when it is pressed. Push buttons are objects of type 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( ). These methods are as follows: ï‚— void setLabel(String str) ï‚— String getLabel( ) EVENT AND LISTENER (JAVA EVENT HANDLING) ï‚¢ Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. ï‚¢ The java.awt.event package provides many event classes and Listener interfaces for event handling. ï‚¢ Registration Methods ï‚¢ For registering the component with the Listener, many classes provide the registration methods. For example: ï‚¢ Button ï‚— public void addActionListener(ActionListener a){} ï‚¢ MenuItem ï‚— public void addActionListener(ActionListener a){} ï‚¢ TextField ï‚— public void addActionListener(ActionListener a){} ï‚— public void addTextListener(TextListener a){} ï‚¢ TextArea ï‚— public void addTextListener(TextListener a){} ï‚¢ Checkbox ï‚— public void addItemListener(ItemListener a){} ï‚¢ Choice ï‚— public void addItemListener(ItemListener a){} ï‚¢ List ï‚— public void addActionListener(ActionListener a){} ï‚— public void addItemListener(ItemListener a){} APPLYING CHECK BOXES ï‚¢ A check box is a control that is used to turn an option on or off. ï‚¢ 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). ï‚¢ Where ï‚— str-label ï‚— on - If on is true, the check box is initially checked; otherwise, it is cleared. ï‚— cbGroup - group is specified by cbGroup ï‚¢ To retrieve the current state of a check box, call getState( ). ï‚— boolean getState( ) ï‚¢ To set its state, call setState( ). ï‚— void setState(boolean on) ï‚¢ You can obtain the current label associated with a check box by calling getLabel( ). ï‚— String getLabel( ) ï‚¢ To set the label, call setLabel( ). ï‚— void setLabel(String str) CHECKBOXGROUP (RADIO BUTTON) ï‚¢ 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. ï‚¢ These check boxes are often called radio buttons. ï‚¢ Check box groups are objects of type CheckboxGroup. Only the default constructor is defined, which creates an empty group. ï‚¢ You can determine which check box in a group is currently selected by calling getSelectedCheckbox( ). ï‚— Checkbox getSelectedCheckbox( ) CHOICE CONTROLS ï‚¢ The Choice class is used to create a pop-up list of items from which the user may choose. ï‚¢ Choice only defines the default constructor, which creates an empty list. Choice() ï‚¢ To add a selection to the list, call add( ). It has this general form: ï‚— void add(String name) ï‚¢ Here, name is the name of the item being added. Items are added to the list in the order in which calls to add( ) occur. ï‚¢ To determine which item is currently selected, you may call either getSelectedItem( ) or getSelectedIndex( ). ï‚— String getSelectedItem( ) ï‚— int getSelectedIndex( ) ï‚¢ To obtain the number of items in the list, call getItemCount( ). ï‚— int getItemCount( ) ï‚¢ You can set the currently selected item using the select( ) method with either a zero-based integer index or a string that will match a name in the list. These methods are shown here: ï‚— void select(int index) ï‚— void select(String name) ï‚¢ Given an index, you can obtain the name associated with the item at that index by calling getItem( ), which has this general form: ï‚— String getItem(int index) Here, index specifies the index of the desired item. TEXTFIELD ï‚¢ The TextField class implements a single-line text- entry area, usually called an edit control. ï‚¢ TextField is a subclass of TextComponent. ï‚¢ TextField defines the following constructors: ï‚— TextField( ) ï‚— TextField(int numChars) :-creates a text field that is numChars characters wide. ï‚— TextField(String str) :- The third form initializes the text field with the string contained in str. ï‚— TextField(String str, int numChars) :- The fourth form initializes a text field and sets its width. ï‚¢ TextField (and its superclass TextComponent) provides several methods that allow you to utilize a text field. ï‚¢ To obtain the string currently contained in the text field, call getText( ). ï‚— String getText( ) ï‚¢ To set the text, call setText( ). ï‚— void setText(String str) //Here, str is the new string. ï‚¢ You can determine editability by calling isEditable( ). These methods are shown here: ï‚— boolean isEditable( ) ï‚— void setEditable(boolean canEdit) SETTING ECHO CHARECTER ï‚¢ You can disable the echoing of the characters as they are typed by calling setEchoChar( ). ï‚— void setEchoChar(char ch) ï‚¢ You can check a text field to see if it is in this mode with the echoCharIsSet( ) method. ï‚— boolean echoCharIsSet( ) ï‚¢ You can retrieve the echo character by calling the getEchoChar( ) method. ï‚— char getEchoChar( ) ï‚¢ Here, ch specifies the character to be echoed. USING LISTS ï‚¢ The List class provides a compact, multiple-choice, scrolling selection list. ï‚¢ List object can be constructed to show any number of choices in the visible window. ï‚— List( ) ï‚— List(int numRows) ï‚— List(int numRows, boolean multipleSelect) ï‚— numRows specifies the number of entries in the list that will always be visible ï‚¢ 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) ï‚¢ For lists that allow only single selection ï‚— String getSelectedItem( ) ï‚— int getSelectedIndex( ) ï‚¢ For lists that allow multiple selection ï‚— String[ ] getSelectedItems( ) ï‚— int[ ] getSelectedIndexes( ) ï‚¢ The first item is at index 0. If more than one item is selected, or if no selection has yet been made, –1 is returned. ï‚¢ To obtain the number of items in the list. ï‚— int getItemCount( ) ï‚¢ You can set the currently selected item by using the select( ) method with a zero-based integer index. ï‚— void select(int index) ï‚¢ Given an index, you can obtain the name associated ï‚— String getItem(int index) MANAGING SCROLL BARS ï‚¢ Scroll bars are used to select continuous values between a specified minimum and maximum. ï‚¢ Scroll bars may be oriented horizontally or vertically. ï‚¢ A scroll bar is actually a composite of several individual parts.(Each end has an arrow & slider box (or thumb)). ï‚¢ Scroll bars are encapsulated by the Scrollbar class. ï‚— Scrollbar( ) ï‚— Scrollbar(int style) ï‚— void setValues(int initialValue, int thumbSize, int min, int max) ï‚— Scrollbar(int style, int initialValue, int thumbSize, int min, int max) ï‚¢ Where ï‚— If style is Scrollbar.VERTICAL, a vertical scrollbar is created. If style is Scrollbar.HORIZONTAL, the scroll bar is horizontal. ï‚— The minimum and maximum values for the scroll bar are specified by min and max ï‚¢ To obtain the current value of the scroll bar. ï‚— int getValue( ) ï‚¢ To set the current value, call setValue( ). ï‚— void setValue(int newValue) ï‚¢ You can also retrieve the minimum and maximum values ï‚— int getMinimum( ) ï‚— int getMaximum( ) ï‚¢ Setting increment ï‚— void setUnitIncrement(int newIncr) //default 1 ï‚— void setBlockIncrement(int newIncr) //default10 TEXTAREA ï‚¢ Sometimes a single line of text input is not enough for a given task. ï‚— TextArea( ) ï‚— TextArea(int numLines, int numChars) ï‚— TextArea(String str) ï‚— TextArea(String str, int numLines, int numChars) ï‚— TextArea(String str, int numLines, int numChars, int sBars) ï‚¢ Where ï‚— numLines specifies the height, in lines, of the text area, ï‚— numChars specifies its width ï‚— Initial text can be specified by str. ï‚— sBars must be one of these values: ï‚¢ SCROLLBARS_BOTH ï‚¢ SCROLLBARS_NONE ï‚¢ SCROLLBARS_HORIZONTAL_ONLY ï‚¢ SCROLLBARS_VERTICAL_ONLY ï‚¢ TextArea is a subclass of TextComponent. Therefore, it supports the ï‚— getText( ) ï‚— setText( ) ï‚— getSelectedText( ) ï‚— select( ) ï‚— isEditable( ) ï‚— setEditable( ) ï‚¢ TextArea adds the following methods: ï‚¢ The append( ) method appends the string specified by str to the end of the current text. ï‚— void append(String str) ï‚¢ insert( ) inserts the string passed in str at the specified index. ï‚— void insert(String str, int index) ï‚¢ To replace text, call replaceRange( ). It replaces the characters from startIndex to endIndex–1, with the replacement text passed in str. ï‚— void replaceRange(String str, int startIndex, int endIndex).

Use Quizgecko on...
Browser
Browser