PPT - Unit 1.1 - AWT Components.pdf
Document Details
Uploaded by Deleted User
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).