Generally, the default event handler is to do nothing. q Must import caite.info* and caite.info*. 4. Evolution of GUIs q Java – basic AWT components. Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based applications in java. Java AWT components are platform-dependent i.e. Java Graphics & GUIs. (and Swing/AWT libraries). CSE Software Design & Implementation. Slides contain contributions from: M. Ernst, M. Hotan, R. Mercer.
|Language:||English, Spanish, Hindi|
|Genre:||Health & Fitness|
|ePub File Size:||24.49 MB|
|PDF File Size:||18.53 MB|
|Distribution:||Free* [*Regsitration Required]|
caite.info, which is only used for the Java event model. In Java , event objects are passed from event source components to objects implementing . called AWT, which stands for Abstract Window Toolkit (although people have This book is a complete programmer's guide and reference to the caite.info pack-. Abstract Window Toolkit (AWT) is a set of APIs used by Java programmers to create GUI objects. In this tutorial, we will learn how to use AWT to create.
To create a file dialog box, instantiate an object of type FileDialog. This function returns an Insets object that contains the top, bottom, left, and right inset to be used when the container is displayed. Let's see a simple example of AWT where we are creating instance of Frame class. Items are added to a menu in the order in which the calls to add take place. Some of the activities that cause events to be generated are pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse. We can retrieve the echo character by calling the get EchoChar method.
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 thumb, the user can click to cause the thumb to jump in that direction by some increment larger than 1. Typically, this action translates into some form of page up and page down. Scroll bars are encapsulated by the Scrollbar class. Scrollbar defines the following constructors:.
Scrollbar Scrollbar int style Scrollbar int style, int iValue, int tSize, int min, int max The first form creates a vertical scroll bar. The second and third forms allow us to specify the orientation of the scroll bar. If style is Scrollbar. In the third form of the constructor, the initial value of the scroll bar is passed in iValue. The number of units represented by the height of the thumb is passed in tSize.
The minimum and maximum values for the scroll bar are specified by min and max. If we construct a scroll bar by using one of the first two constructors, then we need to set its parameters by using setValues , shown here, before it can be used:. The parameters have the same meaning as they have in the third constructor just described. In order to obtain the current value of the scroll bar, call getValue.
It returns the current setting. For setting the current value, we can use setValue. When we set a value, the slider box inside the scroll bar will be positioned to reflect the new value.
We can also retrieve the minimum and maximum values via getMinimum and getMaximum , shown here:. By default, 1 is the increment added to or subtracted from the scroll bar each time it is scrolled up or down one line. We can change this increment by calling setUnitIncrement. By default, pageup and page -down increments are You can change this value by calling setBlockIncrement.
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:. 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. TextField and its superclass TextComponent provides several methods that allow us to utilize a text field. In order to obtain the string currently contained in the text field, we can use getText.
For setting the text, we call setText. String getText void setText String str Here, str is the new string. The user can select a portion of the text in a text field.
Also, we can select a portion of text under program control by using select. Our program can obtain the currently selected text by calling the getSelectedText. The select method selects the characters beginning at startIndex and ending at endIndex —1.
We can control whether the contents of a text field may be modified by the user by calling setEditable. We can determine editability by calling isEditable. In set Editable , if can Edit is true, the text may be changed. If it is false, the text cannot be altered. There may be times when we will want the user to enter text that is not displayed, such as a password.
You can disable the echoing of the characters as they are typed by calling set EchoChar. This method specifies a single character that the TextField will display when characters are entered thus, the actual characters typed will not be shown. We can check a text field to see if it is in this mode with the echo CharIsSet method. We can retrieve the echo character by calling the get EchoChar method. Sometimes a single line of text input is not enough for a given task.
Following are the constructors for TextArea:. Initial text can be specified by str. In the fifth form we can specify the scroll bars that we want the control to have.
TextArea is a subclass of TextComponent. Therefore, it supports the getText , setText , getSelectedText , select , isEditable , and setEditable methods described in the preceding section. TextArea adds the following methods:. The insert inserts the string passed in str at the specified index. In order to replace text, we call replaceRange. It replaces the characters from startIndex to endIndex —1, with the replacement text passed in str.
Text areas are almost self-contained controls. All of the components that we have shown so far have been positioned by the default layout manager. A layout manager automatically arranges our controls within a window by using some type of algorithm. If we have programmed for other GUI environments, such as Windows, then we are accustomed to laying out our controls by hand. First, it is very tedious to manually lay out a large number of components. This is a chicken -and -egg situation; it is pretty confusing to figure out when it is okay to use the size of a given component to position it relative to another.
Each Container object has a layout manager associated with it. A layout manager is an instance of any class that implements the LayoutManager interface. The layout manager is set by the setLayout method. If no call to setLayout is made, then the default layout manager is used. Whenever a container is resized or sized for the first time , the layout manager is used to position each of the components within it.
Here, layoutObj is a reference to the desired layout manager. If we wish to disable the layout manager and position components manually, pass null for layoutObj. If we do this, we will need to determine the shape and position of each component manually, using the setBounds method defined by Component. Normally, we will want to use a layout manager. Each layout manager keeps track of a list of components that are stored by their names.
The layout manager is notified each time we add a component to a container. Whenever the container needs to be resized, the layout manager is consulted via its minimumLayoutSize and preferred LayoutSize methods.
Each component that is being managed by a layout manager contains the getPreferredSize and get MinimumSize methods. These return the preferred and minimum size required to display each component. The layout manager will honor these requests if at all possible, while maintaining the integrity of the layout policy. We may override these methods for controls that we subclass.
Default values are provided otherwise. Java has several predefined LayoutManager classes, several of which are described next. We can use the layout manager that best fits our application. FlowLayout is the default layout manager.
This is the layout manager that the preceding examples have used. FlowLayout implements a simple layout style, which is similar to how words flow in a text editor. Components are laid out from the upper-left corner, left to right and top to bottom. When no more components fit on a line, the next one appears on the next line.
A small space is left between each component, above and below, as well as left and right. Here are the constructors for FlowLayout:. FlowLayout FlowLayout int how FlowLayout int how, int horz, int vert The first form creates the default layout, which centers components and leaves five pixels of space between each component.
The second form lets us specify how each line is aligned. Valid values for how are as follows:. The third form allows us to specify the horizontal and vertical space left between components in horz and vert , respectively. Here is a version of the CheckboxDemo applet shown earlier, modified so that it uses left -aligned flow layout.
The BorderLayout class implements a common layout style for top -level windows. It has four narrow, fixed-width components at the edges and one large area in the center.
The four sides are referred to as north, south, east, and west. The middle area is called the center. Here are the constructors defined by BorderLayout:. BorderLayout BorderLayout int horz, int vert The first form creates a default border layout. The second allows us to specify the horizontal and vertical space left between components in horz and vert , respectively.
BorderLayout defines the following constants that specify the regions:. NORTH When adding components, we will use these constants with the following form of add , which is defined by Container:. Here, compObj is the component to be added, and region specifies where the component will be added.
Here is an example of a BorderLayout with a component in each layout area:. Sometimes we will want to leave a small amount of space between the container that holds our components and the window that contains it. For doing this, we have to override the getInsets method that is defined by Container. This function returns an Insets object that contains the top, bottom, left, and right inset to be used when the container is displayed.
These values are used by the layout manager to inset the components when it lays out the window. The constructor for Insets is shown here:. The values passed in top, left, bottom, and right specify the amount of space between the container and its enclosing window. The getInsets method has this general form:.
When overriding one of these methods, we must return a new Insets object that contains the inset spacing we desire. Here is the preceding Border Layout example modified so that it insets its components ten pixels from each border.
The background color has been set to cyan to help make the insets more visible. GridLayout lays out components in a two -dimensional grid. When we instantiate a GridLayout, we define the number of rows and columns. The constructors supported by GridLayout are shown here:. The second form creates a grid layout with the specified number of rows and columns.
Either numRows or numColumns can be zero. Specifying numRows as zero allows for unlimited -length columns. Specifying numColumns as zero allows for unlimited-length rows. A top-level window can have a menu bar associated with it.
A menu bar displays a list of top-level menu choices. Each choice is associated with a dropdown menu. This concept is implemented in Java by the following classes: MenuBar , Menu , and MenuItem. In general, a menu bar contains one or more Menu objects. Each Menu object contains a list of MenuItem objects. Each MenuItem object represents something that can be selected by the user. Since Menu is a subclass of MenuItem, a hierarchy of nested submenus can be created.
It is also possible to include checkable menu items. These are menu options of type Check boxMenu Item and will have a check mark next to them when they are selected.
For creating a menu bar, we first create an instance of MenuBar. This class only defines the default constructor. Next, create instances of Menu that will define the selections displayed on the bar. If removable is true, the pop-up menu can be removed and allowed to float free.
Otherwise, it will remain attached to the menu bar. Removable menus are implementation-dependent. The first form creates an empty menu. Individual menu items are of type MenuItem. It defines these constructors:. We can disable or enable a menu item by using the setEnabled method.
Its form is shown here:. If the argument enabledFlag is true, the menu item is enabled. If false, the menu item is disabled. This method is shown here:. The isEnabled returns true if the menu item on which it is called is enabled.
Otherwise, it returns false. We can change the name of a menu item by calling setLabel. We can retrieve the current name by using getLabel. It has these constructors:. Checkable items operate as toggles.
Each time one is selected, its state changes. In the first two forms, the checkable entry is unchecked. In the third form, if on is true, the checkable entry is initially checked. Otherwise, it is cleared. We can obtain the status of a checkable item by calling getState. We can set it to a known state by using setState. For checking an item, pass true to setState. To clear an item, pass false. Once we have created a menu item, we must add the item to a Menu object by using add , which has the following general form:.
Here, item is the item being added. Items are added to a menu in the order in which the calls to add take place. The item is returned. Once we have added all items to a Menu object, we can add that object to the menu bar by using this version of add defined by MenuBar:. Here, menu is the menu being added.
The menu is returned. They do not generate events when a menu bar is accessed to display a drop -down menu, for example.
Each time a menu item is selected, an ActionEvent object is generated. Each time a check box menu item is checked or unchecked, an ItemEvent object is generated. Thus, we must implement the Action Listener and Item Listener interfaces in order to handle these menu events. The getItem method of ItemEvent returns a reference to the item that generated this event. The general form of this method is shown here:. Following is an example that adds a series of nested menus to a pop -up window.
The item selected is displayed in the window. The state of the two check box menu items is also displayed.
Often, we will want to use a dialog box to hold a set of related controls. Dialog boxes are primarily used to obtain user input. They are similar to frame windows, except that dialog boxes are always child windows of a top -levelwindow.
In other respects, dialog boxes function like frame windows. We can add controls to them, for example, in the same way that we add controls to a frame window. Dialog boxes may be modal or modeless. When a modal dialog box is active, all input is directed to it until it is closed. This means that we cannot access other parts of our program until we have closed the dialog box.
When a modeless dialog box is active, input focus can be directed to another window in our program. Thus, other parts of our program remain active and accessible. Dialog boxes are of type Dialog. Two commonly used constructors are shown here:. If mode is true, the dialog box is modal. Otherwise, it is modeless. The title of the dialog box can be passed in title. Generally, we will subclass Dialog, adding the functionality required by your application.
Java provides a built-in dialog box that lets the user specify a file. To create a file dialog box, instantiate an object of type FileDialog. This causes a file dialog box to be displayed. Usually, this is the standard file dialog box provided by the operating system. FileDialog provides these constructors:. If boxName is omitted, the title of the dialog box is empty. If how is FileDialog. LOAD, then the box is selecting a file for reading.
SAVE, the box is selecting a file for writing. The third constructor creates a dialog box for selecting a file for reading. FileDialog provides methods that allows us to determine the name of the file and its path as selected by the user. Here are two examples:. String getDirectory String getFile These methods return the directory and the filename, respectively. The following program activates the standard file dialog box:. Applets are event-driven programs.
Thus, event handling is at the core of successful applet programming. Most events to which our applet will respond are generated by the user. These events are passed to our applet in a variety of ways, with the specific method depending upon the actual event. There are several types of events.
The most commonly handled events are those generated by the mouse, the keyboard, and various controls, such as a push button. Events are supported by the java. The modern approach to handling events is based on the delegation event model, which defines standard and consistent mechanisms to generate and process events.
Its concept is quite simple: In this scheme, the listener simply waits until it receives an event. Once received, the listener processes the event and then returns. The advantage of this design is that the application logic that processes events is cleanly separated from the user interface logic that generates those events. In the delegation event model, listeners must register with a source in order to receive an event notification.
This provides an important benefit: This is a more efficient way to handle events than the design used by the old Java 1. Java also allows us to process events without using the delegation event model.
This can be done by extending an AWT component. However, the delegation event model is the preferred design for the reasons just cited. In the delegation model, an event is an object that describes a state change in a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface. Some of the activities that cause events to be generated are pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse.
Here, we are showing Button component on the Frame. The setBounds int xaxis, int yaxis, int width, int height method is used in the above example that sets the position of the awt button. Let's see a simple example of AWT where we are creating instance of Frame class. JavaTpoint offers too many high quality services. Mail us on hr javatpoint. Please mail your requirement at hr javatpoint.
Next Topic Event Handling in java.
Verbal A. Compiler D. Web Tech. Cyber Sec. Control S. Javatpoint Services JavaTpoint offers too many high quality services. Container The Container is a component in AWT that can contain another components like buttons , textfields, labels etc.