hereral/Clara-Training-Data huggingface dataset:
Question: Can you provide the Java AWT CheckboxGroup Example?
Answer: import java.awt.*;    
public class CheckboxGroupExample    
{    
       CheckboxGroupExample(){    
       Frame f= new Frame("CheckboxGroup Example");    
        CheckboxGroup cbg = new CheckboxGroup();  
        Checkbox checkBox1 = new Checkbox("C++", cbg, false);    
        checkBox1.setBounds(100,100, 50,50);    
        Checkbox checkBox2 = new Checkbox("Java", cbg, true);    
        checkBox2.setBounds(100,150, 50,50);    
        f.add(checkBox1);    
        f.add(checkBox2);    
        f.setSize(400,400);    
        f.setLayout(null);    
        f.setVisible(true);    
     }    
public static void main(String args[])    
{    
    new CheckboxGroupExample();    
}    
}
Question: Can you give me the Java AWT CheckboxGroup Example with ItemListener?
Answer: import java.awt.*;    
import java.awt.event.*;  
public class CheckboxGroupExample    
{    
     CheckboxGroupExample(){    
       Frame f= new Frame("CheckboxGroup Example");    
       final Label label = new Label();          
       label.setAlignment(Label.CENTER);  
       label.setSize(400,100);  
        CheckboxGroup cbg = new CheckboxGroup();  
        Checkbox checkBox1 = new Checkbox("C++", cbg, false);    
        checkBox1.setBounds(100,100, 50,50);    
        Checkbox checkBox2 = new Checkbox("Java", cbg, false);    
        checkBox2.setBounds(100,150, 50,50);    
        f.add(checkBox1); f.add(checkBox2); f.add(label);    
        f.setSize(400,400);    
        f.setLayout(null);    
        f.setVisible(true);    
        checkBox1.addItemListener(new ItemListener() {  
            public void itemStateChanged(ItemEvent e) {               
               label.setText("C++ checkbox: Checked");  
            }  
         });  
        checkBox2.addItemListener(new ItemListener() {  
            public void itemStateChanged(ItemEvent e) {               
               label.setText("Java checkbox: Checked");  
            }  
         });  
     }    
public static void main(String args[])    
{    
    new CheckboxGroupExample();    
}    
}
Question: What is Java AWT Choice?

Answer: 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.
Question: What is AWT Choice Class Declaration?
Answer: public class Choice extends Component implements ItemSelectable, Accessible
Question: Can you give me the Choice Class constructor?
Answer: Constructor: Choice()
Description: It constructs a new choice menu.
Question: Can you give me the Choice Class Methods?
Answer: Sr. no:1
Method name: void add(String item)
Description: It adds an item to the choice menu.
.
Sr. no:2
Method name: void addItemListener(ItemListener l)
Description: It adds the item listener that receives item events from the choice menu.

Sr. no:3
Method name: void addNotify()
Description: It creates the peer of choice.

Sr. no:4
Method name: AccessibleContext getAccessibleContext()
Description: It gets the accessbile context related to the choice.

Sr. no:5
Method name: String getItem(int index)
Description: It gets the item (string) at the given index position in the choice menu.

Sr. no:6
Method name: int getItemCount()
Description: It returns the number of items of the choice menu.

Sr. no:7
Method name: ItemListener[] getItemListeners()
Description: It returns an array of all item listeners registered on choice.

Sr. no:8
Method name: T[] getListeners(Class listenerType)
Description: Returns an array of all the objects currently registered as FooListeners upon this Choice.

Sr. no:9
Method name: int getSelectedIndex()
Description: Returns the index of the currently selected item.
 
Sr. no:10
Method name: String getSelectedItem()
Description: Gets a representation of the current choice as a string.

Sr. no:11
Method name:Object[] getSelectedObjects()
Description:  Returns an array (length 1) containing the currently selected item.

Sr. no:12
Method name: void insert(String item, int index)
Description: Inserts the item into this choice at the specified position.

Sr. no:13
Method name: protected String paramString()
Description: Returns a string representing the state of this Choice menu.

Sr. no:14
Method name: protected void processEvent(AWTEvent e)
Description: It processes the event on the choice.

Sr. no:15
Method name: protected void processItemEvent (ItemEvent e)
Description: Processes item events occurring on this Choice menu by dispatching them to any registered ItemListener objects.

Sr. no:16
Method name: void remove(int position)
Description: It removes an item from the choice menu at the given index position.

Sr. no:17
Method name:  void remove(String item)
Description: It removes the first occurrence of the item from choice menu.

Sr. no:18
Method name: void removeAll()
Description: It removes all the items from the choice menu.

Sr. no:19
Method name: void removeItemListener (ItemListener l)
Description: It removes the mentioned item listener. Thus is doesn't receive item events from the choice menu anymore.

Sr. no:20
Method name:  void select(int pos)
Description: It changes / sets the selected item in the choice menu to the item at given index position.

Sr. no:21
Method name: void select(String str)
Description: It changes / sets the selected item in the choice menu to the item whose string value is equal to string specified in the argument.
Question: Can you provide the Java AWT Choice Example?
Answer: In the following example, we are creating a choice menu using Choice() constructor. Then we add 5 items to the menu using add() method and Then add the choice menu into the Frame.

ChoiceExample1.java

// importing awt class  
import java.awt.*;   
public class ChoiceExample1 {    
  
         // class constructor  
        ChoiceExample1() {    
  
        // creating a frame  
        Frame f = new Frame();    
  
        // creating a choice component  
        Choice c = new Choice();   
  
        // setting the bounds of choice menu   
        c.setBounds(100, 100, 75, 75);    
  
        // adding items to the choice menu  
        c.add("Item 1");    
        c.add("Item 2");    
        c.add("Item 3");    
        c.add("Item 4");    
        c.add("Item 5");    
  
        // adding choice menu to frame  
        f.add(c);    
  
        // setting size, layout and visibility of frame  
        f.setSize(400, 400);    
        f.setLayout(null);    
        f.setVisible(true);    
     }    
  
// main method  
public static void main(String args[])    
{    
   new ChoiceExample1();    
}    
}
Question: Can you give me the Java AWT Choice Example with ActionListener?
Answer: 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

// importing necessary packages  
import java.awt.*;    
import java.awt.event.*;   
  
public class ChoiceExample2 {  
  
      // class constructor    
        ChoiceExample2() {    
  
        // creating a frame  
        Frame f = new Frame();    
  
        // creating a final object of Label class  
        final Label label = new Label();   
  
        // setting alignment and size of label component           
        label.setAlignment(Label.CENTER);    
        label.setSize(400, 100);   
  
        // creating a button   
        Button b = new Button("Show");   
  
        // setting the bounds of button   
        b.setBounds(200, 100, 50, 20);    
  
        // creating final object of Choice class  
        final Choice c = new Choice();    
  
        // setting bounds of choice menu  
        c.setBounds(100, 100, 75, 75);   
  
        // adding 5 items to choice menu   
        c.add("C");    
        c.add("C++");    
        c.add("Java");    
        c.add("PHP");    
        c.add("Android");   
  
        // adding above components into the frame   
        f.add(c);  
        f.add(label);  
        f.add(b);    
  
        // setting size, layout and visibility of frame  
        f.setSize(400, 400);    
        f.setLayout(null);    
        f.setVisible(true);    
  
        // adding event to the button  
        // which displays the selected item from the list when button is clicked  
        b.addActionListener(new ActionListener() {    
            public void actionPerformed(ActionEvent e) {         
         String data = "Programming language Selected: "+ c.getItem(c.getSelectedIndex());    
         label.setText(data);    
        }    
        });             
        }    
  
// main method  
public static void main(String args[])    
{    
   new ChoiceExample2();    
}    
}
Question: What is Java AWT List?
Answer: The object of List class represents a list of text items. With the help of the List class, user can choose either one item or multiple items. It inherits the Component class.
Question: What is AWT List class Declaration?
Answer: public class List extends Component implements ItemSelectable, Accessible
Question: Can you give me the AWT List Class Constructors?
Answer: Sr. no:1
Constructor:  List()
Description: It constructs a new scrolling list.

Sr. no:2
Constructor:List(int row_num)
Description:It constructs a new scrolling list initialized with the given number of rows visible.

Sr. no:3
Constructor: List(int row_num, Boolean multipleMode)
Description: It constructs a new scrolling list initialized which displays the given number of rows.
Question: Can you provide the Java AWT List Example?
Answer: In the following example, we are creating a List component with 5 rows and adding it into the Frame.

ListExample1.java

// importing awt class  
import java.awt.*;    
  
public class ListExample1  
{    
    // class constructor   
     ListExample1() {    
     // creating the frame  
        Frame f = new Frame();   
       // creating the list of 5 rows   
        List l1 = new List(5);   
  
        // setting the position of list component   
        l1.setBounds(100, 100, 75, 75);    
  
        // adding list items into the list  
        l1.add("Item 1");    
        l1.add("Item 2");    
        l1.add("Item 3");    
        l1.add("Item 4");    
        l1.add("Item 5");    
  
        // adding the list to frame  
        f.add(l1);   
  
        // setting size, layout and visibility of frame  
        f.setSize(400, 400);    
        f.setLayout(null);    
        f.setVisible(true);    
     }    
  
// main method  
public static void main(String args[])    
{    
   new ListExample1();    
}    
}
Question: Can you give me the Java AWT List Example with ActionListene?
Answer: In the following example, we are creating two List components, a Button and a Label and adding them into the frame. Here, we are generating an event on the button using the addActionListener(ActionListener l) method. On clicking the button, it displays the selected programming language and the framework.

ListExample2.java

// importing awt and event class  
import java.awt.*;    
import java.awt.event.*;    
  
public class ListExample2    
{    
    // class constructor  
     ListExample2() {   
     // creating the frame   
        Frame f = new Frame();   
       // creating the label which is final   
        final Label label = new Label();   
  
        // setting alignment and size of label           
        label.setAlignment(Label.CENTER);    
        label.setSize(500, 100);    
  
        // creating a button   
        Button b = new Button("Show");  
  
        // setting location of button    
        b.setBounds(200, 150, 80, 30);   
  
        // creating the 2 list objects of 4 rows  
        // adding items to the list using add()   
        // setting location of list components  
        final List l1 = new List(4, false);    
        l1.setBounds(100, 100, 70, 70);    
        l1.add("C");    
        l1.add("C++");    
        l1.add("Java");    
        l1.add("PHP");    
  
  
        final List l2=new List(4, true);    
        l2.setBounds(100, 200, 70, 70);    
        l2.add("Turbo C++");    
        l2.add("Spring");    
        l2.add("Hibernate");    
        l2.add("CodeIgniter");   
  
        // adding List, Label and Button to the frame   
        f.add(l1);  
        f.add(l2);  
        f.add(label);  
        f.add(b);    
  
        // setting size, layout and visibility of frame  
        f.setSize(450,450);    
        f.setLayout(null);    
        f.setVisible(true);    
  
        // generating event on the button  
        b.addActionListener(new ActionListener() {    
         public void actionPerformed(ActionEvent e) {         
          String data = "Programming language Selected: "+l1.getItem(l1.getSelectedIndex());    
          data += ", Framework Selected:";    
          for(String frame:l2.getSelectedItems()) {    
                   data += frame + " ";    
          }    
          label.setText(data);    
          }    
         });            
}    
  
// main method  
public static void main(String args[])    
{    
   new ListExample2();    
}    
}
Question: What is Java AWT Canvas?
Answer: The Canvas class controls and represents a blank rectangular area where the application can draw or trap input events from the user. It inherits the Component class.
Question: What is AWT Canvas class Declaration?
Answer: public class Canvas extends Component implements Accessible
Question: What are the Constructors of the Canvas Class?
Answer: Constructors of Canvas Class 
Constructor: Canvas()	
Description: It constructs a new Canvas.

Constructor: Canvas(GraphicConfiguration config)	
Description: It constructs a new Canvas with the given Graphic Configuration object.
Question: What are the Method Inherited by Canvas Class?
Answer: The Canvas has inherited above methods from the following classes:

lang.Component
lang.Object
Question: Can you provide the Java AWT Canvas Example?
Answer: In the following example, we are creating a Canvas in the Frame and painting a red colored oval inside it.

CanvasExample.java


// importing awt class  
import java.awt.*;  
  
// class to construct a frame and containing main method    
public class CanvasExample    
{    
  // class constructor   
  public CanvasExample()    
  {    
  
    // creating a frame  
    Frame f = new Frame("Canvas Example");   
    // adding canvas to frame   
    f.add(new MyCanvas());    
  
    // setting layout, size and visibility of frame  
    f.setLayout(null);    
    f.setSize(400, 400);    
    f.setVisible(true);    
  }    
  
  // main method  
  public static void main(String args[])    
  {    
    new CanvasExample();    
  }    
}    
  
// class which inherits the Canvas class  
// to create Canvas  
class MyCanvas extends Canvas    
{    
        // class constructor  
        public MyCanvas() {    
        setBackground (Color.GRAY);    
        setSize(300, 200);    
     }    
  
     // paint() method to draw inside the canvas  
  public void paint(Graphics g)    
  {    
  
    // adding specifications  
    g.setColor(Color.red);    
    g.fillOval(75, 75, 150, 75);    
  }    
}
Question: What is Java AWT Scrollbar?
Answer: The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a GUI component allows us to see invisible number of rows and columns.

It can be added to top-level container like Frame or a component like Panel. The Scrollbar class extends the Component class.
Question: What is AWT Scrollbar Class Declaration?
Answer: public class Scrollbar extends Component implements Adjustable, Accessible
Question: What are the Scrollbar Class Fields?
Answer: static int HORIZONTAL - It is a constant to indicate a horizontal scroll bar.
static int VERTICAL - It is a constant to indicate a vertical scroll bar.
Question: What are the Constructors of the Scrollbar Class?
Answer: Sr. no.1 
Constructor: Scrollbar()
Description: Constructs a new vertical scroll bar.
Sr. no.2 
Constructor: Scrollbar(int orientation)
Description: Constructs a new scroll bar with the specified orientation.
Sr. no.3
Constructor: Scrollbar(int orientation, int value, int visible, int minimum, int maximum)
Description: Constructs a new scroll bar with the specified orientation, initial value, visible amount, and minimum and maximum values.
Question: What are the Method Inherited by Scrollbar?
Answer: The methods of Scrollbar class are inherited from the following classes:
java.awt.Component
java.lang.Object
Question: Can you provide a Java AWT Scrollbar Example?
Answer: The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a GUI component allows us to see invisible number of rows and columns.

It can be added to top-level container like Frame or a component like Panel. The Scrollbar class extends the Component class.
Question: Can you provide a Java AWT Scrollbar Example with AdjustmentListener?
Answer: In the following example, we are creating a Scrollbar and adding it into the Frame. Here, we are using addAdjustmentListener() method of Scrollbar class which receives the instances of AdjustmentEvent and eventually we display it in the form of Label.

ScrollbarExample2.java

// importing necessary packages  
import java.awt.*;    
import java.awt.event.*;    
  
  
public class ScrollbarExample2 {   
  
       // class constructor   
     ScrollbarExample2() {    
       // creating a Frame with a title   
            Frame f = new Frame("Scrollbar Example");    
  
            // creating a final object of Label  
            final Label label = new Label();   
  
            // setting alignment and size of label object           
            label.setAlignment(Label.CENTER);    
            label.setSize(400, 100);    
  
            // creating a final object of Scrollbar class  
            final Scrollbar s = new Scrollbar();    
  
            // setting the position of scroll bar  
            s.setBounds(100, 100, 50, 100);    
  
            // adding Scrollbar and Label to the Frame  
            f.add(s);  
       f.add(label);    
  
       // setting the size, layout, and visibility of frame   
            f.setSize(400, 400);    
            f.setLayout(null);    
            f.setVisible(true);  
  
       // adding AdjustmentListener to the scrollbar object  
            s.addAdjustmentListener(new AdjustmentListener() {    
                public void adjustmentValueChanged(AdjustmentEvent e) {    
                   label.setText("Vertical Scrollbar value is:"+ s.getValue());    
                }    
            });    
         }  
  
// main method    
public static void main(String args[]){    
new ScrollbarExample2();    
}    
}
Question: What is Java AWT MenuItem and Menu?
Answer: 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.
Question: What is AWT MenuItem class declaration?
Answer: public class MenuItem extends MenuComponent implements Accessible
Question: What is AWT Menu class declaration?
Answer: public class Menu extends MenuItem implements MenuContainer, Accessible
Question: Can you provide a Java AWT MenuItem and Menu Example?
Answer: import java.awt.*;  
class MenuExample  
{  
     MenuExample(){  
         Frame f= new Frame("Menu and MenuItem Example");  
         MenuBar mb=new MenuBar();  
         Menu menu=new Menu("Menu");  
         Menu submenu=new Menu("Sub Menu");  
         MenuItem i1=new MenuItem("Item 1");  
         MenuItem i2=new MenuItem("Item 2");  
         MenuItem i3=new MenuItem("Item 3");  
         MenuItem i4=new MenuItem("Item 4");  
         MenuItem i5=new MenuItem("Item 5");  
         menu.add(i1);  
         menu.add(i2);  
         menu.add(i3);  
         submenu.add(i4);  
         submenu.add(i5);  
         menu.add(submenu);  
         mb.add(menu);  
         f.setMenuBar(mb);  
         f.setSize(400,400);  
         f.setLayout(null);  
         f.setVisible(true);  
}  
public static void main(String args[])  
{  
new MenuExample();  
}  
}
Question: What is Java AWT PopupMenu?
Answer: PopupMenu can be dynamically popped up at specific position within a component. It inherits the Menu class.
Question: What is AWT PopupMenu class declaration?
Answer: public class PopupMenu extends Menu implements MenuContainer, Accessible
Question: Can you provide a Java AWT PopupMenu Example?
Answer: import java.awt.*;  
import java.awt.event.*;  
class PopupMenuExample  
{  
     PopupMenuExample(){  
         final Frame f= new Frame("PopupMenu Example");  
         final PopupMenu popupmenu = new PopupMenu("Edit");   
         MenuItem cut = new MenuItem("Cut");  
         cut.setActionCommand("Cut");  
         MenuItem copy = new MenuItem("Copy");  
         copy.setActionCommand("Copy");  
         MenuItem paste = new MenuItem("Paste");  
         paste.setActionCommand("Paste");      
         popupmenu.add(cut);  
         popupmenu.add(copy);  
         popupmenu.add(paste);        
         f.addMouseListener(new MouseAdapter() {  
            public void mouseClicked(MouseEvent e) {              
                popupmenu.show(f , e.getX(), e.getY());  
            }                 
         });  
         f.add(popupmenu);   
         f.setSize(400,400);  
         f.setLayout(null);  
         f.setVisible(true);  
     }  
public static void main(String args[])  
{  
        new PopupMenuExample();  
}  
}
Question: What is Java AWT Panel?

Answer: The Panel is a simplest container class. It provides space in which an application can attach any other component. It inherits the Container class.
Question: What is AWT Panel class declaration?
Answer: public class Panel extends Container implements Accessible
Question: Can you provide a Java AWT Panel Example?
Answer: import java.awt.*;  
public class PanelExample {  
     PanelExample()  
        {  
        Frame f= new Frame("Panel Example");    
        Panel panel=new Panel();  
        panel.setBounds(40,80,200,200);    
        panel.setBackground(Color.gray);  
        Button b1=new Button("Button 1");     
        b1.setBounds(50,100,80,30);    
        b1.setBackground(Color.yellow);   
        Button b2=new Button("Button 2");   
        b2.setBounds(100,100,80,30);    
        b2.setBackground(Color.green);   
        panel.add(b1); panel.add(b2);  
        f.add(panel);  
        f.setSize(400,400);    
        f.setLayout(null);    
        f.setVisible(true);    
        }  
        public static void main(String args[])  
        {  
        new PanelExample();  
        }  
}
Question: What is Java AWT Dialog?
Answer: 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.

Unlike Frame, it doesn't have maximize and minimize buttons.
Question: Frame vs Dialog
Answer: Frame and Dialog both inherits Window class. Frame has maximize and minimize buttons but Dialog doesn't have.
Question: What is AWT Dialog class declaration?
Answer: public class Dialog extends Window
Question: Can you provide a Java AWT Dialog Example?
Answer: import java.awt.*;  
import java.awt.event.*;  
public class DialogExample {  
    private static Dialog d;  
    DialogExample() {  
        Frame f= new Frame();  
        d = new Dialog(f , "Dialog Example", true);  
        d.setLayout( new FlowLayout() );  
        Button b = new Button ("OK");  
        b.addActionListener ( new ActionListener()  
        {  
            public void actionPerformed( ActionEvent e )  
            {  
                DialogExample.d.setVisible(false);  
            }  
        });  
        d.add( new Label ("Click button to continue."));  
        d.add(b);   
        d.setSize(300,300);    
        d.setVisible(true);  
    }  
    public static void main(String args[])  
    {  
        new DialogExample();  
    }  
}
Question: What is Java AWT Toolkit?
Answer: 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.
Question: What is AWT Toolkit class declaration?
Answer: public abstract class Toolkit extends Object
Question: Can you provide a Java AWT Toolkit Example?
Answer: import java.awt.*;  
public class ToolkitExample {  
  public static void main(String[] args) {  
      Toolkit t = Toolkit.getDefaultToolkit();  
      System.out.println("Screen resolution = " + t.getScreenResolution());  
      Dimension d = t.getScreenSize();  
      System.out.println("Screen width = " + d.width);  
      System.out.println("Screen height = " + d.height);  
  }  
}  
Output:

Screen resolution = 96
Screen width = 1366
Screen height = 768
Question: Can you provide a Java AWT Toolkit Example: Change TitleBar Icon?
Answer: import java.awt.*;   
class ToolkitExample {   
ToolkitExample(){   
Frame f=new Frame();   
Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");  
f.setIconImage(icon);  
f.setLayout(null);   
f.setSize(400,400);   
f.setVisible(true);   
}   
public static void main(String args[]){   
new ToolkitExample();   
}   
}
Question: Can you provide a Java ActionListener Example: On Button click?
Answer: import java.awt.*;  
import java.awt.event.*;  
//1st step  
public class ActionListenerExample implements ActionListener{  
public static void main(String[] args) {  
    Frame f=new Frame("ActionListener Example");  
    final TextField tf=new TextField();  
    tf.setBounds(50,50, 150,20);  
    Button b=new Button("Click Here");  
    b.setBounds(50,100,60,30);  
    //2nd step  
    b.addActionListener(this);  
    f.add(b);f.add(tf);  
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);   
}  
//3rd step  
public void actionPerformed(ActionEvent e){  
            tf.setText("Welcome to Javatpoint.");  
}  
}
Question: Can you provide a Java ActionListener Example: Using Anonymous class?
Answer: We can also use the anonymous class to implement the ActionListener. It is the shorthand way, so you do not need to follow the 3 steps:

b.addActionListener(new ActionListener(){  
public void actionPerformed(ActionEvent e){  
           tf.setText("Welcome to Javatpoint.");  
}  
});  
Let us see the full code of ActionListener using anonymous class.

import java.awt.*;  
import java.awt.event.*;  
public class ActionListenerExample {  
public static void main(String[] args) {  
    Frame f=new Frame("ActionListener Example");  
    final TextField tf=new TextField();  
    tf.setBounds(50,50, 150,20);  
    Button b=new Button("Click Here");  
    b.setBounds(50,100,60,30);  
  
    b.addActionListener(new ActionListener(){  
    public void actionPerformed(ActionEvent e){  
            tf.setText("Welcome to Javatpoint.");  
    }  
    });  
    f.add(b);f.add(tf);  
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);   
}  
}
Question: What is Java MouseListener Interface?
Answer: The Java MouseListener is notified whenever you change the state of mouse. It is notified against MouseEvent. The MouseListener interface is found in java.awt.event package. It has five methods.
Question: What are the Methods of MouseListener interface?
Answer: The signature of 5 methods found in MouseListener interface are given below:

public abstract void mouseClicked(MouseEvent e);  
public abstract void mouseEntered(MouseEvent e);  
public abstract void mouseExited(MouseEvent e);  
public abstract void mousePressed(MouseEvent e);  
public abstract void mouseReleased(MouseEvent e);
Question: Can you provide a Java MouseListener Example?
Answer: import java.awt.*;  
import java.awt.event.*;  
public class MouseListenerExample extends Frame implements MouseListener{  
    Label l;  
    MouseListenerExample(){  
        addMouseListener(this);  
          
        l=new Label();  
        l.setBounds(20,50,100,20);  
        add(l);  
        setSize(300,300);  
        setLayout(null);  
        setVisible(true);  
    }  
    public void mouseClicked(MouseEvent e) {  
        l.setText("Mouse Clicked");  
    }  
    public void mouseEntered(MouseEvent e) {  
        l.setText("Mouse Entered");  
    }  
    public void mouseExited(MouseEvent e) {  
        l.setText("Mouse Exited");  
    }  
    public void mousePressed(MouseEvent e) {  
        l.setText("Mouse Pressed");  
    }  
    public void mouseReleased(MouseEvent e) {  
        l.setText("Mouse Released");  
    }  
public static void main(String[] args) {  
    new MouseListenerExample();  
}  
}
Question: What is Java MouseMotionListener Interface?
Answer: The Java MouseMotionListener is notified whenever you move or drag mouse. It is notified against MouseEvent. The MouseMotionListener interface is found in java.awt.event package. It has two methods.
Question: What are the Methods of MouseMotionListener interface?
Answer: The signature of 2 methods found in MouseMotionListener interface are given below:

public abstract void mouseDragged(MouseEvent e);  
public abstract void mouseMoved(MouseEvent e);
Question: Can you provide a Java MouseMotionListener Example?
Answer: import java.awt.*;  
import java.awt.event.*;  
public class MouseMotionListenerExample extends Frame implements MouseMotionListener{  
    MouseMotionListenerExample(){  
        addMouseMotionListener(this);  
          
        setSize(300,300);  
        setLayout(null);  
        setVisible(true);  
    }  
public void mouseDragged(MouseEvent e) {  
    Graphics g=getGraphics();  
    g.setColor(Color.BLUE);  
    g.fillOval(e.getX(),e.getY(),20,20);  
}  
public void mouseMoved(MouseEvent e) {}  
  
public static void main(String[] args) {  
    new MouseMotionListenerExample();  
}  
}
Question: What is Java ItemListener Interface?
Answer: The Java ItemListener is notified whenever you click on the checkbox. It is notified against ItemEvent. The ItemListener interface is found in java.awt.event package. It has only one method: itemStateChanged().
Question: What is itemStateChanged() method?
Answer: The itemStateChanged() method is invoked automatically whenever you click or unclick on the registered checkbox component.

public abstract void itemStateChanged(ItemEvent e);
Question: Can you provide a Java ItemListener Example?
Answer: import java.awt.*;    
import java.awt.event.*;    
public class ItemListenerExample implements ItemListener{    
    Checkbox checkBox1,checkBox2;  
    Label label;  
    ItemListenerExample(){    
        Frame f= new Frame("CheckBox Example");    
        label = new Label();            
        label.setAlignment(Label.CENTER);    
        label.setSize(400,100);    
        checkBox1 = new Checkbox("C++");    
        checkBox1.setBounds(100,100, 50,50);    
        checkBox2 = new Checkbox("Java");    
        checkBox2.setBounds(100,150, 50,50);    
        f.add(checkBox1); f.add(checkBox2); f.add(label);    
        checkBox1.addItemListener(this);    
        checkBox2.addItemListener(this);    
        f.setSize(400,400);    
        f.setLayout(null);    
        f.setVisible(true);    
     }    
    public void itemStateChanged(ItemEvent e) {      
        if(e.getSource()==checkBox1)  
            label.setText("C++ Checkbox: "     
            + (e.getStateChange()==1?"checked":"unchecked"));   
        if(e.getSource()==checkBox2)  
        label.setText("Java Checkbox: "     
        + (e.getStateChange()==1?"checked":"unchecked"));    
     }  
public static void main(String args[])    
{    
    new ItemListenerExample();    
}    
}
Question: What is Java KeyListener Interface?
Answer: The Java KeyListener is notified whenever you change the state of key. It is notified against KeyEvent. The KeyListener interface is found in java.awt.event package, and it has three methods.
Question: What are the Methods of KeyListener interface?
Answer: Methods of KeyListener interface
The signature of 3 methods found in KeyListener interface are given below:

Method name: public abstract void keyPressed (KeyEvent e);
Description: It is invoked when a key has been pressed.

Method name: public abstract void keyReleased (KeyEvent e);
Description: It is invoked when a key has been released.

Method name: public abstract void keyTyped (KeyEvent e);
Description: It is invoked when a key has been typed.
Question: Can you provide a Java KeyListener Example?
Answer: In the following example, we are implementing the methods of the KeyListener interface.

KeyListenerExample.java

// importing awt libraries  
import java.awt.*;    
import java.awt.event.*;    
// class which inherits Frame class and implements KeyListener interface  
public class KeyListenerExample extends Frame implements KeyListener {    
// creating object of Label class   and TextArea class  
 Label l;    
    TextArea area;    
// class constructor  
    KeyListenerExample() {    
          // creating the label  
        l = new Label();    
// setting the location of the label in frame  
        l.setBounds (20, 50, 100, 20);    
// creating the text area  
        area = new TextArea();    
// setting the location of text area   
        area.setBounds (20, 80, 300, 300);    
// adding the KeyListener to the text area  
        area.addKeyListener(this);  
// adding the label and text area to the frame  
        add(l);  
add(area);    
// setting the size, layout and visibility of frame  
        setSize (400, 400);    
        setLayout (null);    
        setVisible (true);    
    }    
// overriding the keyPressed() method of KeyListener interface where we set the text of the label when key is pressed  
    public void keyPressed (KeyEvent e) {    
        l.setText ("Key Pressed");    
    }    
// overriding the keyReleased() method of KeyListener interface where we set the text of the label when key is released  
    public void keyReleased (KeyEvent e) {    
        l.setText ("Key Released");    
    }    
// overriding the keyTyped() method of KeyListener interface where we set the text of the label when a key is typed  
    public void keyTyped (KeyEvent e) {    
        l.setText ("Key Typed");    
    }    
  // main method  
    public static void main(String[] args) {    
        new KeyListenerExample();    
    }    
}
Question: What is Java WindowListener Interface?
Answer: The Java WindowListener is notified whenever you change the state of window. It is notified against WindowEvent. The WindowListener interface is found in java.awt.event package. It has three methods.
Question: What is WindowListener interface declaration?
Answer: The declaration for java.awt.event.WindowListener interface is shown below:

public interface WindowListener extends EventListener
Question: What are the Methods of WindowListener interface?
Answer: Methods of WindowListener interface
The signature of 7 methods found in WindowListener interface with their usage are given below:

Method signature: public abstract void windowActivated (WindowEvent e);
Description: It is called when the Window is set to be an active Window.

Method signature: public abstract void windowClosed (WindowEvent e);
Description: It is called when a window has been closed as the result of calling dispose on the window.

Method signature: public abstract void windowClosing (WindowEvent e);
Description: It is called when the user attempts to close the window from the system menu of the window.

Method signature: public abstract void windowDeactivated (WindowEvent e);
Description: It is called when a Window is not an active Window anymore.

Method signature: public abstract void windowDeiconified (WindowEvent e);
Description: It is called when a window is changed from a minimized to a normal state.

Method signature: public abstract void windowIconified (WindowEvent e);
Description: It is called when a window is changed from a normal to a minimized state.

Method signature: public abstract void windowOpened (WindowEvent e);
Description: It is called when window is made visible for the first time.
Question: What is Working of WindowListener interface?
Answer: -If a class needs to process some Window events, an object should exist which can implement the interface.
-As the object is already registered with Listener, an event will be generated on all the states of window.
-This helps in generation of invocation of relevant method in listener's object. And then WindowEvent is passed after invocation.
Question: Can you provide a Java WindowListener Example?
Answer: In the following example, we are going to implement all the method of WindowListener interface one by one.

WindowExample.java

// importing necessary libraries of awt  
import java.awt.*;    
import java.awt.event.WindowEvent;    
import java.awt.event.WindowListener;   
  
// class which inherits Frame class and implements WindowListener interface   
public class WindowExample extends Frame implements WindowListener {    
  
// class constructor  
    WindowExample() {   
  
    // adding WindowListener to the frame   
        addWindowListener(this);   
        // setting the size, layout and visibility of frame        
        setSize (400, 400);    
        setLayout (null);    
        setVisible (true);    
    }   
// main method  
public static void main(String[] args) {    
    new WindowExample();    
}    
  
// overriding windowActivated() method of WindowListener interface which prints the given string when window is set to be active  
public void windowActivated (WindowEvent arg0) {    
    System.out.println("activated");    
}    
  
// overriding windowClosed() method of WindowListener interface which prints the given string when window is closed  
public void windowClosed (WindowEvent arg0) {    
    System.out.println("closed");    
}    
  
// overriding windowClosing() method of WindowListener interface which prints the given string when we attempt to close window from system menu  
public void windowClosing (WindowEvent arg0) {    
    System.out.println("closing");    
    dispose();    
}    
  
// overriding windowDeactivated() method of WindowListener interface which prints the given string when window is not active  
public void windowDeactivated (WindowEvent arg0) {    
    System.out.println("deactivated");    
}    
  
// overriding windowDeiconified() method of WindowListener interface which prints the given string when window is modified from minimized to normal state  
public void windowDeiconified (WindowEvent arg0) {    
    System.out.println("deiconified");    
}    
  
// overriding windowIconified() method of WindowListener interface which prints the given string when window is modified from normal to minimized state  
public void windowIconified(WindowEvent arg0) {    
    System.out.println("iconified");    
}    
  
// overriding windowOpened() method of WindowListener interface which prints the given string when window is first opened  
public void windowOpened(WindowEvent arg0) {    
    System.out.println("opened");    
}    
}
Question: What is Java Adapter Classes?
Answer: Java adapter classes provide the default implementation of listener interfaces. If you inherit the adapter class, you will not be forced to provide the implementation of all the methods of listener interfaces. So it saves code.
Question: What are the Pros of using Adapter classes?
Answer: It assists the unrelated classes to work combinedly.
It provides ways to use classes in different ways.
It increases the transparency of classes.
It provides a way to include related patterns in the class.
It provides a pluggable kit for developing an application.
It increases the reusability of the class.
Question: What is the list of java.awt.event Adapter classes?
Answer: Adapter class : WindowAdapter
Listener interface : WindowListener
Adapter class : KeyAdapter
Listener interface : KeyListener
Adapter class : MouseAdapter
Listener interface : MouseListener
Adapter class : MouseMotionAdapter
Listener interface : MouseMotionListener
Adapter class : FocusAdapter
Listener interface : FocusListener
Adapter class : ComponentAdapter
Listener interface : ComponentListener
Adapter class : ContainerAdapter
Listener interface : ContainerListener
Adapter class : HierarchyBoundsAdapter
Listener interface : HierarchyBoundsListener
Question: What is the list java.awt.dnd Adapter classes?
Answer: Adapter class : DragSourceAdapter
Listener interface : DragSourceListener
Adapter class : DragTargetAdapter
Listener interface : DragTargetListener
Question: What is the list of javax.swing.event Adapter classes?
Answer: Adapter class : MouseInputAdapter
Listener interface : MouseInputListener
Adapter class : InternalFrameAdapter
Listener interface : InternalFrameListener
Question: Can you provide a Java WindowAdapter Example?
Answer: // importing the necessary libraries  
import java.awt.*;    
import java.awt.event.*;    
  
public class AdapterExample {  
// object of Frame    
    Frame f;    
// class constructor  
    AdapterExample() {    
// creating a frame with the title  
        f = new Frame ("Window Adapter");    
// adding the WindowListener to the frame  
// overriding the windowClosing() method   
        f.addWindowListener (new WindowAdapter() {    
            public void windowClosing (WindowEvent e) {    
                f.dispose();    
            }    
        });    
         // setting the size, layout and   
        f.setSize (400, 400);    
        f.setLayout (null);    
        f.setVisible (true);    
    }    
  
// main method  
public static void main(String[] args) {    
    new AdapterExample();    
}    
}
Question: Can you provide a Java MouseAdapter Example?
Answer: // importing the necessary libraries  
import java.awt.*;    
import java.awt.event.*;    
  
// class which inherits the MouseAdapter class  
public class MouseAdapterExample extends MouseAdapter {    
// object of Frame class  
    Frame f;    
// class constructor  
    MouseAdapterExample() {    
// creating the frame with the title  
        f = new Frame ("Mouse Adapter");    
// adding MouseListener to the Frame  
        f.addMouseListener(this);    
   // setting the size, layout and visibility of the frame  
        f.setSize (300, 300);    
        f.setLayout (null);    
        f.setVisible (true);    
    }    
// overriding the mouseClicked() method of the MouseAdapter class  
    public void mouseClicked (MouseEvent e) {    
// creating the Graphics object and fetching them from the Frame object using getGraphics() method  
        Graphics g = f.getGraphics();    
// setting the color of graphics object  
        g.setColor (Color.BLUE);    
// setting the shape of graphics object  
        g.fillOval (e.getX(), e.getY(), 30, 30);    
    }    
  // main method  
public static void main(String[] args) {    
    new MouseAdapterExample();    
}    
}
Question: Can you provide a Java MouseMotionAdapter Example?
Answer: // importing the necessary libraries  
import java.awt.*;    
import java.awt.event.*;    
// class which inherits the MouseMotionAdapter class  
public class MouseMotionAdapterExample extends MouseMotionAdapter {     
// object of Frame class  
    Frame f;    
// class constructor  
    MouseMotionAdapterExample() {    
// creating the frame with the title  
        f = new Frame ("Mouse Motion Adapter");    
// adding MouseMotionListener to the Frame  
        f.addMouseMotionListener (this);    
 // setting the size, layout and visibility of the frame  
        f.setSize (300, 300);    
        f.setLayout (null);    
        f.setVisible (true);    
    }    
// overriding the mouseDragged() method   
public void mouseDragged (MouseEvent e) {    
// creating the Graphics object and fetching them from the Frame object using getGraphics() method  
    Graphics g = f.getGraphics();    
// setting the color of graphics object  
    g.setColor (Color.ORANGE);    
// setting the shape of graphics object  
    g.fillOval (e.getX(), e.getY(), 20, 20);    
}    
public static void main(String[] args) {    
    new MouseMotionAdapterExample();    
}    
}
Question: Can you provide a Java KeyAdapter Example?
Answer: // importing the necessary libraries  
import java.awt.*;    
import java.awt.event.*;    
// class which inherits the KeyAdapter class  
public class KeyAdapterExample extends KeyAdapter {    
// creating objects of Label, TextArea and Frame  
    Label l;    
    TextArea area;    
    Frame f;    
// class constructor  
    KeyAdapterExample() {    
// creating the Frame with title  
        f = new Frame ("Key Adapter");    
// creating the Label  
        l = new Label();    
// setting the location of label   
        l.setBounds (20, 50, 200, 20);    
// creating the text area  
        area = new TextArea();  
// setting the location of text area    
        area.setBounds (20, 80, 300, 300);    
// adding KeyListener to text area  
        area.addKeyListener(this);    
 // adding the label and text area to frame  
        f.add(l);  
f.add(area);    
// setting the size, layout and visibility of frame  
        f.setSize (400, 400);    
        f.setLayout (null);    
        f.setVisible (true);    
    }    
// overriding the keyReleased() method   
    public void keyReleased (KeyEvent e) {    
// creating the String object to get the text fromTextArea   
        String text = area.getText();    
// splitting the String into words  
        String words[] = text.split ("\\s");    
// setting the label text to print the number of words and characters of given string  
        l.setText ("Words: " + words.length + " Characters:" + text.length());    
    }    
  // main method  
    public static void main(String[] args) {    
        new KeyAdapterExample();    
    }    
}
Question: How to close AWT Window in Java?
Answer: We can close the AWT Window or Frame by calling dispose() or System.exit() inside windowClosing() method. The windowClosing() method is found in WindowListener interface and WindowAdapter class.
Question: What are the Different ways to override windowClosing() method?
Answer: By anonymous class
By inheriting WindowAdapter class
By implementing WindowListener interface
Question: Can you provide a Close AWT Window Example 1: Anonymous class?
Answer: // importing necessary awt libraries   
import java.awt.*;    
import java.awt.event.WindowEvent;    
import java.awt.event.WindowListener;    
  
// class which inherits the Frame class  
public class WindowExample extends Frame {    
// class constructor  
    WindowExample() {    
// adding WindowListener to the Frame  
// and using the windowClosing() method of WindowAdapter class  
        addWindowListener (new WindowAdapter() {    
            public void windowClosing (WindowEvent e) {    
                dispose();    
            }    
        });    
// setting the size, layout and visibility of frame  
        setSize (400, 400);    
        setLayout (null);    
        setVisible (true);    
    }    
// main method  
public static void main (String[] args) {    
    new WindowExample();    
}