8 Java AWT ButtonNew
8 Java AWT ButtonNew
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. 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.
Window
The window is the container that have no borders and menu bars. 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. It can have other components
like button, textfield etc.
Frame
The Frame is the container that contain title bar and can have menu bars. It can have other
components like button, textfield etc.
public void setSize(int width,int height) sets the size (width and height) of the
component.
public void setLayout(LayoutManager m) defines the layout manager for the component.
public void setVisible(boolean status) changes the visibility of the component, by default
false.
1. import java.awt.*;
2. class First extends Frame{
3. First(){
4. Button b=new Button("click me");
5. b.setBounds(30,100,80,30);// setting button position
6. add(b);//adding button into frame
7. setSize(300,300);//frame size 300 width and 300 height
8. setLayout(null);//no layout manager
9. setVisible(true);//now frame will be visible, by default not visible
10. }
11. public static void main(String args[]){
12. First f=new First();
13. }}
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.
1. import java.awt.*;
2. class First2{
3. First2(){
4. Frame f=new Frame();
5. Button b=new Button("click me");
6. b.setBounds(30,50,80,30);
7. f.add(b);
8. f.setSize(300,300);
9. f.setLayout(null);
10. f.setVisible(true);
11. }
12. public static void main(String args[]){
13. First2 f=new First2();
14. }}
Component Component is an object having a graphical representation that can be displayed on the
screen and that can interact with the user. For examples buttons, checkboxes, list and
scrollbars of a graphical user interface.
Container Container object is a component that can contain other components.Components added to
a container are tracked in a list. The order of the list will define the components' front-to-
back stacking order within the container. If no index is specified when adding a component
to a container, it will be added to the end of the list.
Panel Panel provides space in which an application can attach any other components, including
other panels.
Window Window is a rectangular area which is displayed on the screen. In different window we can
execute different program and display different data. Window provide us with multitasking
environment. A window must have either a frame, dialog, or another window defined as its
owner when it's constructed.
Frame A Frame is a top-level window with a title and a border. The size of the frame includes any
area designated for the border. Frame encapsulates window. It and has a title bar, menu
bar, borders, and resizing corners.
AWT Controls
Every user interface considers the following three main aspects:
UI elements : Thes are the core visual elements the user eventually sees and interacts with. GWT
provides a huge list of widely used and common elements varying from basic to complex which we will
cover in this tutorial.
Layouts: They define how UI elements should be organized on the screen and provide a final look and
feel to the GUI (Graphical User Interface). This part will be covered in Layout chapter.
Behavior: These are events which occur when the user interacts with UI elements. This part will be
covered in Event Handling chapter.
ActionEvent ActionListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
Registration Methods
For registering the component with the Listener, many classes provide the registration methods. For
example:
o Button
o public void addActionListener(ActionListener a){}
o MenuItem
o public void addActionListener(ActionListener a){}
o TextField
o public void addActionListener(ActionListener a){}
o public void addTextListener(TextListener a){}
o TextArea
1. Within class
2. Other class
3. Anonymous class
public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above
example that sets the position of the component it may be button, textfield etc.
1
Component
A Component is an abstract super class for GUI controls and it represents an object with
graphical representation.
AWT UI Elements:
Following is the list of commonly used controls while designed GUI using AWT.
1
Label
A Label object is a component for placing text in a container.
2
Button
This class creates a labeled button.
3
Check Box
A check box is a graphical component that can be in either an on (true) or off (false) state.
4
Check Box Group
The CheckboxGroup class is used to group the set of checkbox.
5
List
The List component presents the user with a scrolling list of text items.
6
Text Field
A TextField object is a text component that allows for the editing of a single line of text.
8
Choice
A Choice control is used to show pop up menu of choices. Selected choice is shown on the top
of the menu.
9
Canvas
A Canvas control represents a rectangular area where application can draw something or can
receive inputs created by user.
10
Image
An Image control is superclass for all image classes representing graphical images.
11
Scroll Bar
A Scrollbar control represents a scroll bar component in order to enable user to select from range
of values.
12
Dialog
A Dialog control represents a top-level window with a title and a border used to take some form
of input from the user.
13
File Dialog
A FileDialog control represents a dialog window from which the user can select a file.
Types of Event
The events can be broadly classified into two categories:
Callback Methods
EventObject class
It is the root class from which all event state objects shall be derived. All Events are constructed
with a reference to the object, the source, that is logically deemed to be the object upon which
the Event in question initially occurred upon.This class is defined in java.util package.
Class declaration
Following is the declaration for java.util.EventObject class:
public class EventObject
extends Object
implements Serializable
Field
Following are the fields for java.util.EventObject class:
protected Object source -- The object on which the Event initially occurred.
Class constructors
S.N. Constructor & Description
1
EventObject(Object source)
Constructs a prototypical Event.
Class methods
S.N. Method & Description
2
String toString()
Returns a String representation of this EventObject.
Methods inherited
This class inherits methods from the following classes:
java.lang.Object
1
AWTEvent
It is the root event class for all AWT events. This class and its subclasses supercede the original
java.awt.Event class.
2
ActionEvent
The ActionEvent is generated when button is clicked or the item of a list is double clicked.
3
InputEvent
The InputEvent class is root event class for all component-level input events.
4
KeyEvent
On entering the character the Key event is generated.
5
MouseEvent
This event indicates a mouse action occurred in a component.
6
TextEvent
The object of this class represents the text events.
8
AdjustmentEvent
The object of this class represents the adjustment event emitted by Adjustable objects.
9
ComponentEvent
The object of this class represents the change in state of a window.
10
ContainerEvent
The object of this class represents the change in state of a window.
11
MouseMotionEvent
The object of this class represents the change in state of a window.
12
PaintEvent
The object of this class represents the change in state of a window.
actionPerformed() method
The actionPerformed() method is invoked automatically whenever you click on the registered
component.
1. component.addActionListener(instanceOfListenerclass);
Output:
1. b.addActionListener(new ActionListener(){
2. public void actionPerformed(ActionEvent e){
3. tf.setText("Welcome to Javatpoint.");
4. }
5. });
1. import java.awt.*;
2. import java.awt.event.*;
3. public class ActionListenerExample {
4. public static void main(String[] args) {
5. Frame f=new Frame("ActionListener Example");
6. final TextField tf=new TextField();
7. tf.setBounds(50,50, 150,20);
8. Button b=new Button("Click Here");
9. b.setBounds(50,100,60,30);
10.
11. b.addActionListener(new ActionListener(){
12. public void actionPerformed(ActionEvent e){
13. tf.setText("Welcome to Javatpoint.");
14. }
15. });
16. f.add(b);f.add(tf);
17. f.setSize(400,400);
18. f.setLayout(null);
19. f.setVisible(true);
20. }
21. }
Reference:- www.javatpoint.com and www.tutorialspoint.com
Output:
Output:
Output:
Output:
Output:
Output:
Output:
Output:
itemStateChanged() method
The itemStateChanged() method is invoked automatically whenever you click or unclick on the
registered checkbox component.
Output:
Output:
Output:
Output:
The adapter classes are found in java.awt.event, java.awt.dnd and javax.swing.event packages.
The Adapter classes with their corresponding listener interfaces are given below.
WindowAdapter WindowListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
DragSourceAdapter DragSourceListener
DragTargetAdapter DragTargetListener
MouseInputAdapter MouseInputListener
InternalFrameAdapter InternalFrameListener
Output:
Output:
Output:
Output:
If you implement the WindowListener interface, you will be forced to override all the 7 methods of
WindowListener interface. So it is better to use WindowAdapter class.
o By anonymous class
o By inheriting WindowAdapter class
o By implementing WindowListener interface
Output: