Java AWT

Add to Favourites
Post to:

Description
 Controls are components that allow a user to interact with your application in various ways—for example, a commonly used control is the push button.
 A layout manager automatically positions components within a container. Thus, the appearance of a window is determined by a combination of the controls that it contains and the layout manager used to position them.In addition to the controls, a frame window can also include a standard-style menu bar.
 Each entry in a menu bar activates a drop-down menu of options from which the user can choose. A menu bar is always positioned at the top of a window. Although different in appearance, menu bars are handled in much the same way as are the other controls. While it is possible to manually position components within a window, doing so is quite tedious. The layout manager automates this task.
Control Fundamentals
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. Add a control to a window by calling add( ), which is defined by Container. The add( ) method has several forms.
 Component add(Component compObj)
compObj :- is an instance of the control that you want to add. A reference to compObj is returned. Once a control has been added, it will automatically be visible whenever its parent window is displayed.


Type: ppt

Discussion
Presentation Transcript Presentation Transcript

AWT CONTROL : AWT CONTROL NEENU

AWT Controls : AWT Controls Controls: components that allow a user to interact with your application. layout manager: automatically positions components within a container menu bar : Each entry in a menu bar activates a drop-down menu of options from which the user can choose. A menu bar is always positioned at the top of a window

Control Fundamentals : Control Fundamentals 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 : Adding and Removing Controls To include a control in a window, you must add it to the window. The add( ) method has several forms. Component add(Component compObj ) compObj :- is an instance of the control that you want to add. To remove a control void remove(Component obj ) Here, obj is a reference to the control you want to remove

Labels : Labels A label is an object of type Label, and it contains a string, which it displays. Label defines the following constructors: Label( ) :Creates blank label Label(String str ) : Creates a label that contains string specified by str Label(String str , int how ):Creates a label that contains 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 , Label.CENTER .

PowerPoint Presentation : METHODS void setText(String str ) : str specifies the new label. String getText( ) : the current label is returned. void setAlignment(int how ): set the alignment of the string within the label. int getAlignment( ) : To obtain the current alignment.

PowerPoint Presentation : 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); } }

Buttons : Buttons 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( ) : Creates an empty button Button(String str ) : Creates a button that contains str as a label. These methods are as follows: void setLabel(String str ) : , str becomes the new label for the button String getLabel( ):GET the label name for button

Handling Buttons : Handling Buttons Each time a button is pressed, an action event is generated. This is sent to any listeners that previously registered an interest in receiving action event notifications from that component. Each listener implements the ActionListener interface. That interface defines the actionPerformed( ) method, which is called when an event occurs.

program : program import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class ButtonDemo extends Applet implements ActionListener { String msg = ""; Button yes, no, maybe; public void init() { yes = new Button("Yes"); no = new Button("No"); maybe = new Button("Undecided"); add(yes); add(no); add(maybe); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str = ae.getActionCommand(); if(str.equals("Yes")) { msg = "You pressed Yes."; } else if(str.equals("No")) { msg = "You pressed No."; } else { msg = "You pressed Undecided."; } repaint(); } public void paint(Graphics g) { g.drawString(msg, 6, 100); } }

Check Boxes : Check Boxes A check box is a control that is used to turn an option on or off. It consists of a small box that can either contain a heck mark or not. There is a label associated with each check box that describes what option the box represents. You change the state of a check box by clicking on it. Check boxes can be used individually or as part of a group. Check boxes are objects of the Checkbox class. The methods are as follows: boolean getState( ) : To retrieve the current state of a check box void setState(boolean on ) : To set its state String getLabel( ) : obtain the current label associated with a check box void setLabel(String str ) : To set the label if on is true , the box is checked. If it is false , the box is cleared. The string passed in str becomes the new label associated with the invoking check box.

PowerPoint Presentation : Checkbox supports these constructors: Checkbox( ) : Creates a check box whose label is initially blank, The state of the check box is unchecked Checkbox(String str ): Creates a check box whose label is specified by str. The state of the check box is unchecked. Checkbox(String str , boolean on ) : to set the initial state of the check box. If on is true , the check box is initially checked; otherwise, it is cleared. Checkbox(String str , boolean on , CheckboxGroup cbGroup ): Checkbox(String str , CheckboxGroup cbGroup , boolean on ): both these create a check box whose label is specified by str and whose group is specified by cbGroup. If this check box is not part of a group, then cbGroup must be null. The value of on determines the initial state of the check box.

Handling Check Boxes : Handling Check Boxes Each time a check box is selected or deselected, an item event is generated. Each listener implements the ItemListener interface. That interface defines the itemStateChanged( ) method. An ItemEvent object is supplied as the argument to this method. It contains information about the event (for example, whether it was a selection or deselection).

PowerPoint Presentation : import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class CheckboxDemo extends Applet implements ItemListener { String msg = ""; Checkbox Win98, winNT, solaris, mac; public void init() { Win98 = new Checkbox("Windows 98/XP", null, true); winNT = new Checkbox("Windows NT/2000"); solaris = new Checkbox("Solaris"); mac = new Checkbox("MacOS"); add(Win98); add(winNT); add(solaris); add(mac); Win98.addItemListener(this); winNT.addItemListener(this); solaris.addItemListener(this); mac.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } // Display current state of the check boxes. public void paint(Graphics g) { msg = "Current state: "; g.drawString(msg, 6, 80); msg = " Windows 98/XP: " + Win98.getState(); g.drawString(msg, 6, 100); msg = " Windows NT/2000: " + winNT.getState(); g.drawString(msg, 6, 120); msg = " Solaris: " + solaris.getState(); g.drawString(msg, 6, 140); msg = " MacOS: " + mac.getState(); g.drawString(msg, 6, 160); }

CheckboxGroup/Radio buttons : CheckboxGroup/Radio buttons 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, because they act like the station selector on a car radio—only one station can be selected at any one time. These methods are as follows: Checkbox getSelectedCheckbox( ) void setSelectedCheckbox(Checkbox which ) Here, which is the check box that you want to be selected

PowerPoint Presentation : import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class CBGroup extends Applet implements ItemListener { String msg = ""; Checkbox Win98, winNT, solaris, mac; CheckboxGroup cbg; public void init() { cbg = new CheckboxGroup(); Win98 = new Checkbox("Windows 98/XP", cbg, true); winNT = new Checkbox("Windows NT/2000", cbg, false); solaris = new Checkbox("Solaris", cbg, false); mac = new Checkbox("MacOS", cbg, false ); add(Win98); add( winNT ); add( solaris ); add( mac ); Win98.addItemListener(this); winNT.addItemListener (this); solaris.addItemListener (this); mac.addItemListener (this); } public void itemStateChanged ( ItemEvent ie ) { repaint(); } // Display current state of the check boxes. public void paint(Graphics g) { msg = "Current selection: "; msg += cbg.getSelectedCheckbox (). getLabel (); g.drawString ( msg , 6, 100); } }

Choice Controls/Dropdown Menu : Choice Controls/Dropdown Menu The Choice class is used to create a pop-up list of items from which the user may choose. Thus, a Choice control is a form of menu. When inactive, a Choice component takes up only enough space to show the currently selected item. When the user clicks on it, the whole list of choices pops up, and a new selection can be made. Items are added to the list in the order in which calls to add( ) occur.

PowerPoint Presentation : These methods are shown here: String getSelectedItem( ) : To determine which item is currently selected, returns a string containing the name of the item Handling Choice Lists Each time a choice is selected, an item event is generated. This is sent to any listeners that previously registered an interest in receiving item event notifications from that component. Each listener implements the ItemListener interface. That interface defines the itemStateChanged( ) method. An ItemEvent object is supplied as the argument to this method.

PowerPoint Presentation : import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class ChoiceDemo extends Applet implements ItemListener { Choice os, browser; String msg = ""; public void init() { os = new Choice(); browser = new Choice(); // add items to os list os.add("Windows 98/XP"); os.add("Windows NT/2000"); os.add("Solaris"); os.add("MacOS"); // add items to browser list browser.add("Netscape 3.x"); browser.add("Netscape 4.x"); browser.add("Netscape 5.x"); browser.add("Netscape 6.x"); browser.add("Internet Explorer 4.0"); browser.add("Internet Explorer 5.0"); browser.add("Internet Explorer 6.0"); browser.add("Lynx 2.4"); browser.select("Netscape 4.x"); // add choice lists to window add(os); add(browser); // register to receive item events os.addItemListener(this); browser.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } // Display current selections. public void paint(Graphics g) { msg = "Current OS: "; msg += os.getSelectedItem(); g.drawString(msg, 6, 120); msg = "Current Browser: "; msg += browser.getSelectedItem(); g.drawString(msg, 6, 140); } }

Lists : Lists Provides a compact, multiple-choice, scrolling selection list. Created to allow multiple selections. List provides these constructors: List( ) : creates a List control that allows only one item to be selected at any one time List(int numRows ) : the value of numRows specifies the number of entries in the list that will always be visible. List(int numRows , boolean multipleSelect ): 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.

Handling Lists : Handling Lists import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class ListDemo extends Applet implements ActionListener { List os, browser; String msg = ""; public void init() { os = new List(4, true); browser = new List(4, false); // add items to os list os.add("Windows 98/XP"); os.add("Windows NT/2000"); os.add("Solaris"); os.add("MacOS"); // add items to browser list browser.add("Netscape 3.x"); browser.add("Netscape 4.x"); browser.add("Netscape 5.x"); browser.add("Netscape 6.x"); browser.add("Internet Explorer 4.0"); browser.add("Internet Explorer 5.0"); browser.add("Internet Explorer 6.0"); browser.add("Lynx 2.4"); browser.select(1); // add lists to window add(os); add(browser); // register to receive action events os.addActionListener(this); browser.addActionListener(this); } public void actionPerformed(ActionEvent ae) { repaint(); } // Display current selections. public void paint(Graphics g) { int idx[]; msg = "Current OS: "; idx = os.getSelectedIndexes(); for(int i=0; i

Scroll Bars : Scroll Bars Scroll bars are used to select continuous values between a specified minimum and maximum. Scroll bars may be oriented horizontally or vertically. Each end has an arrow that you can click to move the current value of the scroll bar one unit in the direction of the arrow. The current value of the scroll bar relative to its minimum and maximum values is indicated by the slider box (or thumb ) for the scroll bar. The slider box can be dragged by the user to a new position.The scroll bar will then reflect this value. In the background space on either side of the larger than 1

PowerPoint Presentation : Scrollbar defines the following constructors: Scrollbar( ) : creates a vertical scroll bar Scrollbar(int style ): Scrollbar(int style, int initialValue , int thumbSize , int min , int max ) 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. These methods are as follows: int getValue( ) void setValue(int newValue ) When you set a value, the slider box inside the scroll bar will be positioned to reflect the new value. You can also retrieve the minimum and maximum values via getMinimum( ) and getMaximum( ) , shown here: int getMinimum( ) int getMaximum( )

PowerPoint Presentation : The types of adjustment events are as follows: BLOCK_DECREMENT: A page-down event has been generated. BLOCK_INCREMENT : A page-up event has been generated. TRACK : An absolute tracking event has been generated. UNIT_DECREMENT : The line-down button in a scroll bar has been pressed. UNIT_INCREMENT : The line-up button in a scroll bar has been pressed.

PowerPoint Presentation : import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class SBDemo extends Applet implements AdjustmentListener, MouseMotionListener { String msg = ""; Scrollbar vertSB, horzSB; public void init() { int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height"); vertSB = new Scrollbar(Scrollbar.VERTICAL,0, 1, 0, height); horzSB = new Scrollbar(Scrollbar.HORIZONTAL,0, 1, 0, width); add(vertSB); add(horzSB); // register to receive adjustment events vertSB.addAdjustmentListener(this); horzSB.addAdjustmentListener(this); addMouseMotionListener(this); } public void adjustmentValueChanged(AdjustmentEvent ae) { repaint(); } // Update scroll bars to reflect mouse dragging. public void mouseDragged(MouseEvent me) { int x = me.getX(); int y = me.getY(); vertSB.setValue(y); horzSB.setValue(x); repaint(); } // Necessary for MouseMotionListener public void mouseMoved(MouseEvent me) { } // Display current value of scroll bars. public void paint(Graphics g) { msg = "Vertical: " + vertSB.getValue(); msg += ", Horizontal: " + horzSB.getValue(); g.drawString(msg, 6, 160); // show current mouse drag position g.drawString("*", horzSB.getValue(), vertSB.getValue()); } }

TextField : TextField The TextField class implements a single-line text-entry area, usually called an edit control. Text fields allow the user to enter strings and to edit the text using the arrow keys, cut and paste keys, and mouse selections. TextField is a subclass of TextComponent . TextField defines the following constructors: TextField( ) : creates a default text field. TextField(int numChars ): creates a text field that is numChars characters wide TextField(String str ): initializes the text field with the string contained in str TextField(String str , int numChars ): 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( ) . These methods are as follows: String getText( ) void setText(String str )

PowerPoint Presentation : import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class TextFieldDemo extends Applet implements ActionListener { TextField name, pass; public void init() { Label namep = new Label("Name: ", Label.RIGHT); Label passp = new Label("Password: ", Label.RIGHT); name = new TextField(12); pass = new TextField(8); pass.setEchoChar('?'); add(namep); add(name); add(passp); add(pass); // register to receive action events name.addActionListener(this); pass.addActionListener(this); } // User pressed Enter. public void actionPerformed(ActionEvent ae) { repaint(); } public void paint(Graphics g) { g.drawString("Name: " + name.getText(), 6, 60); g.drawString("Selected text in name: "+ name.getSelectedText(), 6, 80); g.drawString("Password: " + pass.getText(), 6, 100); } }

TextArea : TextArea a single line of text input is not enough for a given task. To handle these situations, the AWT includes a simple multiline editor called TextArea . Following are the constructors for TextArea : TextArea( ) TextArea(int numLines, int numChars ): numLines specifies the height, in lines, of the text area, and numChars specifies its width, in characters. TextArea(String str ): Initial text can be specified by str TextArea(String str , int numLines , int numChars ) TextArea(String str , int numLines , int numChars , int sBars ): specify the scroll bars that you want the control to have sBars must be one of these values: SCROLLBARS_BOTH SCROLLBARS_NONE SCROLLBARS_HORIZONTAL_ONLY SCROLLBARS_VERTICAL_ONLY

Neenu Prasad
Looking to Learn for computer subjects
User
25 Followers

Your Facebook Friends on WizIQ