Events and Listeners in Java

The event classes represents the events that are generated when an end user interact with the AWT components. An event is an object which specifies the change of state in the source.

 

It is generated whenever an action takes place like a mouse button is clicked or text is modified. Events generated by the events source are sent to event listeners which handle them in an appropriate manner.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class EventDemo2 extends Applet implements ActionListener, ItemListener, AdjustmentListener

{

   ColorCanvas displaay;

   Choice shapeeChoice;  

   Checkbox brighttColors;

   TextField text;      

   Scrollbar horiScroll;  

   Scrollbar vertScroll;  

    

   public void init()

{

    setBackground(Color.gray);

      Panel dispPanel = new Panel(); // to hold display and scroll bars

       dispPanel.setLayout(new BorderLayout());

       displaay = new ColorCanvas("XCnotes.com",ColorCanvas.RECT);

       dispPanel.add(displaay, BorderLayout.CENTER); //set border at center

       horiScroll = new Scrollbar(Scrollbar.HORIZONTAL,51,2,0,100);

       dispPanel.add(horiScroll, BorderLayout.SOUTH);

       vertScroll = new Scrollbar(Scrollbar.VERTICAL,1,2,0,100);

       dispPanel.add(vertScroll, BorderLayout.EAST);

            

       Panel upPanel = new Panel(); // for controls

       upPanel.setBackground(Color.white);

       upPanel.setLayout(new GridLayout(1,3,5,5));

       shapeeChoice = new Choice();

       shapeeChoice.add("Rectangle");

       shapeeChoice.add("Oval");

       shapeeChoice.add("RoundRect");

       upPanel.add(shapeeChoice);

       brighttColors = new Checkbox("More Bright Colors");

       upPanel.add(brighttColors);

       text = new TextField("Different Shapes With Different Colors");

       upPanel.add(text);

            

       setLayout(new BorderLayout(1,1));

       add("Center", dispPanel);

       add("North", upPanel);

            

       setDisplayColors(); //calling function

      

       text.addActionListener(this); // set this applet to listen for events

       horiScroll.addAdjustmentListener(this);

       vertScroll.addAdjustmentListener(this);

       shapeeChoice.addItemListener(this);

       brighttColors.addItemListener(this);

            

   } // end of init()

        

public Insets getInsets()

{

     return new Insets(1,1,1,1);

   }

void setDisplayColors()

{

   float backgroundBrightness = horiScroll.getValue() / 100.0F;

     float foregroundHue = vertScroll.getValue() / 100.0F;

     float saturation = 1.0F;

     float brightnesss;

     if (brighttColors.getState())

         brightnesss = 1.0F;

     else

         brightnesss = 0.6F;

     Color backColor = Color.getHSBColor(0.0F,0.0F,backgroundBrightness);

     Color foregroundColor = Color.getHSBColor(foregroundHue,1.0F,brightnesss);

     displaay.setBackground(backColor);

     displaay.setForeground(foregroundColor);

   }

        

   public void itemStateChanged(ItemEvent itemevtt)

   {

     Object source = itemevtt.getSource(); // object that generated the event

     if (source == shapeeChoice)

     {

        switch (shapeeChoice.getSelectedIndex())

         {

           case 0:

               displaay.setShape(ColorCanvas.RECT);

               break;

           case 1:

               displaay.setShape(ColorCanvas.OVAL);

               break;

           case 2:

               displaay.setShape(ColorCanvas.ROUNDED);

               break;

         }

     }

     else if (source == brighttColors)

         {

             setDisplayColors();

              displaay.repaint();

        }

  

   }

   public void actionPerformed(ActionEvent aevt)

     {  

      displaay.setText(text.getText());

    }

        

   public void adjustmentValueChanged(AdjustmentEvent advalevt)

   {

    setDisplayColors();

       displaay.repaint();

   }

}

class ColorCanvas extends Canvas

   {

       private String teext; // text to be displayed

        private int shapee;   // code for shape to be displayed;

        private Font fontt;   // used for displaying the text

       public final static int RECT = 0; // shape code for a rectangle

      public final static int OVAL = 1; // shape code for an oval

      public final static int ROUNDED = 2; // shape code for an round rect

   public ColorCanvas(String teext, int shapee)

   {

       this.teext = teext;  

       this.shapee = shapee;

       fontt = new Font("Times New Roman",Font.BOLD,22);

   }

public void setText(String teext)

   {

     this.teext = teext;

     repaint();

   }

public void setShape(int shapee)

{

     this.shapee = shapee;

     repaint();

   }

   public void paint(Graphics g)

   {

       int widtth = getSize().width;   // get size of canvas

       int heigh = getSize().height;

       int shape_leftt = widtth / 9; // compute position and size of shape

       int shape_up = heigh / 3;

       int shape_widtth = (7*widtth / 9);

       int shape_heightt = (5*heigh / 9);

       switch (shapee)

       {

         case RECT:

             g.fillRect(shape_leftt,shape_up,shape_widtth,shape_heightt);

             break;

         case OVAL:

             g.fillOval(shape_leftt,shape_up,shape_widtth,shape_heightt);

             break;

         case ROUNDED:

             g.fillRoundRect(shape_leftt,shape_up,shape_widtth,shape_heightt,24,24);

             break;

       }

       g.setFont(fontt);

       g.drawString(teext,widtth/9,2*heigh/9); // draw the text

   }

      

}

//<applet code="EventDemo2.class" height=500 width=500></applet>

Event and Listener in Java

 

Explanation…

In above example by using Choice we can select the shape(Rectangle,Oval,RoundRectangle) to display.

CheckBox is used for select the bright or dim colors              

By using ScrollBar(Horizontal or Vertical) we can change the color bright or dim

TextField is used for entering the text to be displayed

public Insets getInsets() is used to leave border around edge of applet

     return new Insets(1,1,1,1);

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @