Java AWT
Java AWT
The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a Checkbox changes
its state from "on" to "off" or from "off" to "on".
Sr. Constructor
no.
3. Checkbox(String label, boolean state) It constructs a checkbox with the given label and sets the given
state.
4. Checkbox(String label, boolean state, It constructs a checkbox with the given label, set the given state
CheckboxGroup group) in the specified checkbox group.
5. Checkbox(String label, CheckboxGroup It constructs a checkbox with the given label, in the given
group, boolean state) checkbox group and set to the specified state.
o java.awt.Component
o java.lang.Object
1. void addItemListener(ItemListener IL) It adds the given item listener to get the item events from the
checkbox.
8. Object[] getSelectedObjects() It returns an array (size 1) containing checkbox label and returns
null if checkbox is not selected.
9. boolean getState() It returns true if the checkbox is on, else returns off.
10. protected String paramString() It returns a string representing the state of checkbox.
12. protected void It process the item events occurring in the checkbox by
processItemEvent(ItemEvent e) dispatching them to registered ItemListener object.
13. void removeItemListener(ItemListener l) It removes the specified item listener so that the item listener
doesn't receive item events from the checkbox anymore.
14. void setCheckboxGroup(CheckboxGroup It sets the checkbox's group to the given checkbox.
g)
15. void setLabel(String label) It sets the checkbox's label to the string argument.
16. void setState(boolean state) It sets the state of checkbox to the specified state.
Output:
Note: CheckboxGroup enables you to create radio buttons in AWT. There is no special control for creating radio buttons in AWT.
Output:
Java AWT CheckboxGroup Example with ItemListener
1. import java.awt.*;
2. import java.awt.event.*;
3. public class CheckboxGroupExample
4. {
5. CheckboxGroupExample(){
6. Frame f= new Frame("CheckboxGroup Example");
7. final Label label = new Label();
8. label.setAlignment(Label.CENTER);
9. label.setSize(400,100);
10. CheckboxGroup cbg = new CheckboxGroup();
11. Checkbox checkBox1 = new Checkbox("C++", cbg, false);
12. checkBox1.setBounds(100,100, 50,50);
13. Checkbox checkBox2 = new Checkbox("Java", cbg, false);
14. checkBox2.setBounds(100,150, 50,50);
15. f.add(checkBox1); f.add(checkBox2); f.add(label);
16. f.setSize(400,400);
17. f.setLayout(null);
18. f.setVisible(true);
19. checkBox1.addItemListener(new ItemListener() {
20. public void itemStateChanged(ItemEvent e) {
21. label.setText("C++ checkbox: Checked");
22. }
23. });
24. checkBox2.addItemListener(new ItemListener() {
25. public void itemStateChanged(ItemEvent e) {
26. label.setText("Java checkbox: Checked");
27. }
28. });
29. }
30. public static void main(String args[])
31. {
32. new CheckboxGroupExample();
33. }
34. }
Output:
Java AWT Choice
The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It
inherits Component class.
o java.awt.Component
o java.lang.Object
2. void addItemListener(ItemListener l) It adds the item listener that receives item events from the choice
menu.
5. String getItem(int index) It gets the item (string) at the given index position in the choice
menu.
8. T[] getListeners(Class listenerType) Returns an array of all the objects currently registered as
FooListeners upon this Choice.
11. Object[] getSelectedObjects() Returns an array (length 1) containing the currently selected item.
12. void insert(String item, int index) Inserts the item into this choice at the specified position.
13. protected String paramString() Returns a string representing the state of this Choice menu.
15. protected void processItemEvent Processes item events occurring on this Choice menu by dispatching
(ItemEvent e) them to any registered ItemListener objects.
16. void remove(int position) It removes an item from the choice menu at the given index
position.
17. void remove(String item) It removes the first occurrence of the item from choice menu.
18. void removeAll() It removes all the items from the choice menu.
19. void removeItemListener (ItemListener It removes the mentioned item listener. Thus is doesn't receive item
l) events from the choice menu anymore.
20. void select(int pos) It changes / sets the selected item in the choice menu to the item at
given index position.
21. void select(String str) It changes / sets the selected item in the choice menu to the item
whose string value is equal to string specified in the argument.
Output:
Java AWT Choice Example with ActionListener
In the following example, we are creating a choice menu with 5 items. Along with that we are creating a button and a label. Here,
we are adding an event to the button component using addActionListener(ActionListener a) method i.e. the selected item from
the choice menu is displayed on the label when the button is clicked.
ChoiceExample2.java
Output:
2. List(int row_num) It constructs a new scrolling list initialized with the given number of
rows visible.
3. List(int row_num, Boolean It constructs a new scrolling list initialized which displays the given
multipleMode) number of rows.
o java.awt.Component
o java.lang.Object
List Class Methods
1. void add(String item) It adds the specified item into the end of scrolling list.
2. void add(String item, int index) It adds the specified item into list at the given index position.
3. void addActionListener(ActionListener l) It adds the specified action listener to receive action events
from list.
4. void addItemListener(ItemListener l) It adds specified item listener to receive item events from list.
9. String getItem(int index) It fetches the item related to given index position.
11. ItemListener[] getItemListeners() It returns an array of item listeners registered on the list.
14. Dimension getMinimumSize(int rows) It gets the minimum size of a list with given number of rows.
16. Dimension getPreferredSize(int rows) It gets the preferred size of list with given number of rows.
17. int getRows() It fetches the count of visible rows in the list.
22. Object[] getSelectedObjects() It gets the selected items on scrolling list in array of objects.
23. int getVisibleIndex() It gets the index of an item which was made visible by method
makeVisible()
24. void makeVisible(int index) It makes the item at given index visible.
25. boolean isIndexSelected(int index) It returns true if given item in the list is selected.
26. boolean isMultipleMode() It returns the true if list allows multiple selections.
27. protected String paramString() It returns parameter string representing state of the scrolling
list.
28. protected void It process the action events occurring on list by dispatching
processActionEvent(ActionEvent e) them to a registered ActionListener object.
30. protected void processItemEvent(ItemEvent It process the item events occurring on list by dispatching
e) them to a registered ItemListener object.
31. void removeActionListener(ActionListener l) It removes specified action listener. Thus it doesn't receive
further action events from the list.
32. void removeItemListener(ItemListener l) It removes specified item listener. Thus it doesn't receive
further action events from the list.
33. void remove(int position) It removes the item at given index position from the list.
34. void remove(String item) It removes the first occurrence of an item from list.
35. void removeAll() It removes all the items from the list.
36. void replaceItem(String newVal, int index) It replaces the item at the given index in list with the new
string specified.
37. void select(int index) It selects the item at given index in the list.
38. void setMultipleMode(boolean b) It sets the flag which determines whether the list will allow
multiple selection or not.
ListExample1.java
ListExample2.java
Output:
72.
Class methods
2. void createBufferStrategy (int numBuffers) It creates a new multi buffering strategies on the particular
component.
3. void createBufferStrategy (int numBuffers, It creates a new multi buffering strategies on the particular
BufferCapabilities caps) component with the given buffer capabilities.
o lang.Component
o lang.Object
CanvasExample.java
Output:
Java AWT MenuItem and Menu
The object of MenuItem class adds a simple labeled menu item on menu. The items used in a menu must belong to the MenuItem
or any of its subclass.
The object of Menu class is a pull down menu component which is displayed on the menu bar. It inherits the MenuItem class.
1. class MenuExample
2. {
3. MenuExample(){
4. Frame f= new Frame("Menu and MenuItem Example");
5. MenuBar mb=new MenuBar();
6. Menu menu=new Menu("Menu");
7. Menu submenu=new Menu("Sub Menu");
8. MenuItem i1=new MenuItem("Item 1");
9. MenuItem i2=new MenuItem("Item 2");
10. MenuItem i3=new MenuItem("Item 3");
11. MenuItem i4=new MenuItem("Item 4");
12. MenuItem i5=new MenuItem("Item 5");
13. menu.add(i1);
14. menu.add(i2);
15. menu.add(i3);
16. submenu.add(i4);
17. submenu.add(i5);
18. menu.add(submenu);
19. mb.add(menu);
20. f.setMenuBar(mb);
21. f.setSize(400,400);
22. f.setLayout(null);
23. f.setVisible(true);
24. }
25. public static void main(String args[])
26. {
27. new MenuExample();
28. }
29. }
Output:
30.
Java AWT PopupMenu
PopupMenu can be dynamically popped up at specific position within a component. It inherits the Menu class.
Output:
JAVA AWT DIALOG
The Dialog control represents a top level window with a border and a title used to take some form of input from the user. It inherits
the Window class.
FRAME VS DIALOG
Frame and Dialog both inherits Window class. Frame has maximize and minimize buttons but Dialog doesn't have.
Output:
27.
JAVA AWT TOOLKIT
Toolkit class is the abstract superclass of every implementation in the Abstract Window Toolkit. Subclasses of Toolkit are used to
bind various components. It inherits Object class.
Output:
Screen resolution = 96
Screen width = 1366
Screen height = 768
JAVA AWT TOOLKIT EXAMPLE: BEEP()
1. import java.awt.event.*;
2. public class ToolkitExample {
3. public static void main(String[] args) {
4. Frame f=new Frame("ToolkitExample");
5. Button b=new Button("beep");
6. b.setBounds(50,100,60,30);
7. f.add(b);
8. f.setSize(300,300);
9. f.setLayout(null);
10. f.setVisible(true);
11. b.addActionListener(new ActionListener(){
12. public void actionPerformed(ActionEvent e){
13. Toolkit.getDefaultToolkit().beep();
14. }
15. });
16. }
17. }
Output:
JAVA AWT TOOLKIT EXAMPLE: CHANGE TITLEBAR ICON
1. import java.awt.*;
2. class ToolkitExample {
3. ToolkitExample(){
4. Frame f=new Frame();
5. Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");
6. f.setIconImage(icon);
7. f.setLayout(null);
8. f.setSize(400,400);
9. f.setVisible(true);
10. }
11. public static void main(String args[]){
12. new ToolkitExample();
13. }
14. }
Output:
Output: