Java/Awt

From Meshplex

Jump to: navigation, search
Image:Java_programming.jpg
Introduction to Java
Overview of Java
Concepts of Object Oriented Programming
How to Setup Java on your PC
Introduction to Applets
View Applet in a web browser
Data Types, Variables,and Arrays
Operators
Control Statements
Packages, Classes, and Interfaces
Inheritance and Polymorphism
Strings
Recursion
Random Numbers
Exception Handling
Java IO and file Handling
Java Thread and Multithreading
Java Collections
Java Network Programming
Image, Audio, Video
Advanced Java
Generics
AWT
Swing
Regular Expression
JDBC
XML and Java
The full form of AWT is “Abstract Window Toolkit”. It basically contains various methods and classes which are used to create and implement widows successfully. It can be defined as a collection set of application program interfaces which is used by the programmers in Java for the creation of the various types of GUI objects like the buttons, scroll bars, windows etc. It consists of the different GUI class libraries which make it very easy and simple to be implemented by the programmers.

As we have mentioned in the definition above the AWT has different types of classes with the help of which it helps in creating these GUI objects to the programmers. Now, we will take a quick look on the few classes of the AWT.

1. Awtevent: This class contains all the AWT events.

2. BorderLayout: This as defined by its term takes care of the bordering with five components. Those five components are North, South, East, West and center.

3. Button: this helps in creating a push control button.

4. Color: It controls and manages the colours in a platform independent fashion.

5. Component: It is quite an important class as it takes the responsibility for various AWT components as an abstract superclass.

6. Checkbox: It creates a check box control.

7. Container: It holds various other components. It is a subclass of Component.

8. Dialog: It is used to create a dialog window.

9. Event: its basic purpose is the encapsulation of all the events.

10. FileDialog: This class helps the user to create and implement a window with which the user can select a file which he wants to access.

11. Flowlayout: it helps to set the position of the components from left to right or top to bottom.

12. Font: It serves the purpose of font encapsulation.

13. Images : It does the same thing as font does. The only difference is that it is used for font and this is used for graphical images.

14. Label: It is used for labelling that is creating a label that will display any particular string.

15. List: It gives the user the choice to select any element from the list of elements.

16. Menu: This is helpful to create a pull down or a drop down kind of menu.

After having a brief discussion on the various classes of AWT lets move towards another important section which understands the structure of the window about which we will be talking in this complete section of the tutorial.

Component

This is the parent class of AWT and very much responsible for all the attributes of Visual component.

Container

It is a subclass of the parent class - Component. It acts as a parent class for many other classes like the panel.

Panel

As mentioned above, it is a sub class of Container .A panel can be defined as a window which is without any menu bar or title bar. We can have few methods related to this as add( ) method to add other components to the panel, setLocation() to set location of the panel, setSize ( ) to set the size of the panel.

Window

Window is always visible on the desktop and is created indirectly with the help of one of its subclass called Frame.

Frame

Frame is a sub class of Window as mentioned above and it consists of a title bar, menu bar, borders. We normally create a window with the help of frame and we have two Frame constructors through which we can do this. They are:

1. Frame ( ): This will create a standard window which is without any title.

2. Frame (String title): This will help to create a window which takes the name of the title as a parameter which is passed in the function.

Canvas

As the term illustrates, it is a blank window on which we can easily draw any object. We can understand it very similar to the canvas of an artist or a painter in our day to day life.

Color

The AWT color system facilitates us by giving the choice to select the colours of our choice when we are creating a window to make it more attractive. This is encapsulated by the Color class.

We can create our own colors, using one of the Color constructors. Few very common constructors are:

1. Color (int red, int green, int blue): This tales three colours as parameters and gives a new colour as a mix of the three.

2. Color (int rgbValue): This again takes three colours and gives a mix of the three, but the difference is that in this case it takes these three colours together in the form of an integer value.

3. Color (float red, float green, float blue): This takes three float values as parameters and gives again a mix of the three values given.

Font

We have multiple fonts available in AWT which helps us to make our window look more appealing and attractive. They are encapsulated by the Font class.

Few methods related to Font are:

1. getAvailableFontFamilyNames ( ) is used to find out what all fonts are available on our machine which we can use in our frame or window.

2.getAllFonts( ) method is used to get the array of all Font objects for all the fonts which are available. Font[ ] getAllFonts( )

Controls

The controls are basically the components which are required by any programmers at the time of interacting with that particular application. Now AWT controls are basically the components which a programmer will be using at the time of interacting with any GUI object like the panel, frame or a window.

These controls play an important role in AWT as they are used very effectively by the programmers, For example a very simple and frequently used control is the push button or the submit button. All these controls are subclasses of Component.

We also have the flexibility of adding and removing any particular control in AWT. This is done by: Add ( ) method is used to add a control in a window.

Remove ( ) is used to remove or erase any control from a window. void remove(Component obj)

We have few controls available in AWT which we will study in brief now one by one

1. Labels

2. Push buttons

3. Check boxes

4. Choice lists

5. Lists

6. Scroll bars

7. Text editing


1 .Labels: Labels have few constructors which are mentioned below:

Label ( ): This constructor will create a blank Label.

2. Label (String str) This constructor is used to create a label with the specified label which is passes as parameter.


Methods associated with the Labels are:

1. getText (): This method helps to obtain the current label.

           String getText( )	

2. setText ( ): This method is used to set or change the text in a label. void setText(String str)

3. setAlignment ( ): This method is used to set the alignment. void setAlignment(int how)


2. Buttons 2. Push Button: Push buttons are very commonly used with the programmers. It consists of a label which acts as a name of that button and has an event which gets activated once it is clicked .Push button are the objects of Buttons class.


Button defines these two constructors:

1. Button ( ): This constructor creates a default empty button.

2. Button (String str): This creates a button with name specified as parameter.


Few methods associated with buttons are:

1. getLabel ( ): This method is used to find out the name of a particular button. Its syntax is : String getLabel( )

2. setLabel ( ): This method will set or change the name of the button. Its syntax is:

void setLabel(String str)

3. Check Boxes A check box is a small box which enables the user to either select it clicking on it to check or uncheck it. It can be done by once clicking on that box and next deactivating it by again clicking on it. They can be implemented to work individually or in group too depending upon the user’s requirement. They are the objects of Check Box class.

Few constructors of Check Boxes are described below:

1. Checkbox ( ): It is used to create a check box whose label is initially blank and its state is unchecked.

2. Checkbox (String str): This creates a check box whose label is specified by str and the state of check box too is unchecked.

3. Checkbox (String str, boolean on): This is used to set the initial state of the check box and In this If on is true, the check box is checked else not.

Few methods associated with the Check Boxes are:

1. getState( ): This is used to get the current state of the check box. boolean getState( )

2. setState ( ): This method is used to set the state of the check box. void setState(boolean on) String getLabel( ) void setLabel(String str)


4. Choice Controls The Choice class is implemented when a user wants to select an element from a pop up list of items or elements. It works very similarly to the menu. The moment user clicks on it the entire list of elements gets displayed in front of it else only one item is displayed.

Few methods related to this are:

1. add( ): this is used to add a selection to the list. void add(String name)

2. getSelectedItem () : This will return the value as the item which is currently selected. String getSelectedItem( )

3. getItemCount( ) : This is used to obtain the number of items in the list. int getItemCount( )

5. Lists List class enables the user to create a list of elements from which he can select a single element or multiple elements with the help of control key .It consists of few constructors which are as under:

1. List ( ): This constructor creates a default list which will let the user select a single item or element at a time. 2. List(int numRows): This is used to create a list with the specified number of row in the list.

3. List(int numRows, boolean multipleSelect) : This is used to create a list with specified number of rows and allowing the option of multiple selection at a time if the Boolean value passed is true.

Few methods associated with List are :

1. add( ) is used to add selection to a particular list. It has the following two forms: void add(String name) void add(String name, int index)


2. getSelectedItem( ) : This is used to get a string which contains the name of the particular item. String getSelectedItem( )

3. getItemCount( ): This method is used to obtain the number of items in the list. Its syntax is: int getItemCount( )


6. Scroll Bars A scroll bar consists of various individual parts and is sued to select a continuous value between the minimum and maximum. It helps the user to move anywhere on the screen with the help of the arrows present on all four corners of the frame or screen or window. They are encapsulated by the Scrollbar class.

Scrollbar has few constructors which are as under:

1. Scrollbar ( ): This is used to create a vertical Scroll bar.

2. Scrollbar(int style): It creates a scroll bar with specified orientation.

Few methods associated with Scroll Bars are: 1. getValue( ) is used to obtain the current value of the scroll bar. Its syntax is int getValue( )

2. setValue( ) is used to set the current value void setValue(int newValue)

3. getMinimum( ) is sued to retrieve the minimum value in a scroll bar. Its syntax is int getMinimum( ) 4. getMaximum( ) is used to retrieve the maximum values . Iys Syntax is int getMaximum ( )

7. TextField: This is used to write a single line of text. It enables to Performa all the cut, copy and paste operation easily on the text. Since it is a subclass of TextComponent it lets to perform methods like getText () and setText ( ) on it.

Few Constructors of TextField are:

1. TextField( ): it creates a default Text filed.

2.TextField(int numChars): This is used to create a text field with specified length limit.

3. TextField(String str): It is used to create a text field with the specified string.

4. TextField(String str, int numChars): it is used to create a text field with the specified string and width mentioned as parameters.


Few methods related to TextField are: 1. getText ( ): This method helps to retrieve the string which is currently in the text. Its syntax is: string getText( )


2. setText( ): This method is used to set a new striung in the text. void setText(String str)

3. getselectedText ( ) is used to get the currently selected text in a text field. Its syntax is String getSelectedText( )


8. TextArea Textarea is used when we need to write multiple lines of text and thus it is called multiline editor. Since TextArea is a subclass of TextComponent. It very successfully performs the getText( ), setText( ), getSelectedText( ), select( ), isEditable( ), and setEditable( ) methods.

Few Constructors for TextArea are:

1.TextArea( )

2.TextArea(int numLines, int numChars)

3.TextArea(String str)

4. TextArea(String str, int numLines, int numChars)

5. TextArea(String str, int numLines, int numChars, int sBars)

Few methods of TextArea are: 1. void append(String str): This method adds the string to the end of the current text.

2. void insert(String str, int index): This method is used to insert the text at the specified position in between the text.

3. void replaceRange(String str, int startIndex, int endIndex): This method is used to replce a string with another specified string at the specified place.