<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ankit Virparia &#187; Event Handling</title>
	<atom:link href="http://ankit.co/category/tutorials/java-tutorials/event-handling/feed" rel="self" type="application/rss+xml" />
	<link>http://ankit.co</link>
	<description>A Programmer, Designer and Trainer</description>
	<lastBuildDate>Sun, 11 May 2014 04:15:47 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>ComponentListener Applet</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/componentlistener-applet?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=componentlistener-applet</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/componentlistener-applet#comments</comments>
		<pubDate>Fri, 11 Jan 2013 02:20:27 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Event Handling]]></category>
		<category><![CDATA[Listener]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=421</guid>
		<description><![CDATA[<p>ComponentListener is used to detect and handle changes/updates done on any components like Button, MenuItem, JToggleButton, JTextField, any&#8230; ComponentListener Interface As per the event delegation model we are going to implement this interface in our class. ComponentListener consists of 4 methods which are as below. public void componentHidden(ComponentEvent e) public void componentMoved(ComponentEvent e) public void [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/componentlistener-applet">ComponentListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>ComponentListener is used to detect and handle changes/updates done on any components like Button, MenuItem, JToggleButton, JTextField, any&#8230;</p>
<h2>ComponentListener Interface</h2>
<p>As per the event delegation model we are going to implement this interface in our class. ComponentListener consists of 4 methods which are as below.</p>
<ol>
<li>public void componentHidden(ComponentEvent e)</li>
<li>public void componentMoved(ComponentEvent e)</li>
<li>public void componentResized(ComponentEvent e)</li>
<li>public void componentShown(ComponentEvent e)</li>
</ol>
<p><strong>To add ComponentListener on any component the syntax is:</strong>  btn.addComponentListener(this);<br />
For further reference you can refer my article on <a href="tutorials/java-tutorials/event-handling/event-handling-methodologies" title="Event Handling Methodologies"><strong>Event Handling Methodologies</strong></a>.</p>
<p><strong>Program:</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AppletComponentListener extends Applet implements ComponentListener
{	
	TextArea ta = new TextArea(7,50);
	public void init() 
	{
		Button left = new Button(&quot;Left&quot;);
		left.setName(&quot;Left&quot;);
		left.addComponentListener(this);
	
		final Button right = new Button(&quot;Right&quot;);
		right.setName(&quot;Right&quot;);
		right.addComponentListener(this);
	
		ActionListener action = new ActionListener() {
		  public void actionPerformed(ActionEvent e) {
			right.setVisible(!right.isVisible());
		  }
		};
		left.addActionListener(action);
	
		add(left);
		add(right);	
		add(ta);
	}
	public void componentHidden(ComponentEvent e) 
	{
		dump(&quot;Hidden&quot;, e);
	}
	
	public void componentMoved(ComponentEvent e) 
	{
		dump(&quot;Moved&quot;, e);
	}
	
	public void componentResized(ComponentEvent e) 
	{
		dump(&quot;Resized&quot;, e);
	}
	
	public void componentShown(ComponentEvent e) 
	{
		dump(&quot;Shown&quot;, e);
	}
	
	private void dump(String type, ComponentEvent e) 
	{
		ta.append(e.getComponent().getName() + &quot; : &quot; + type+&quot;\n&quot;);
	}
}
/*
&lt;applet code=&quot;AppletComponentListener&quot; width=&quot;400&quot; height=&quot;250&quot;&gt;&lt;/applet&gt;
*/

</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/componentlistener-applet">ComponentListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/event-handling/componentlistener-applet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Color Chooser &#8211; multiple event handling</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/color-chooser-multiple-event-handling?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=color-chooser-multiple-event-handling</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/color-chooser-multiple-event-handling#comments</comments>
		<pubDate>Fri, 11 Jan 2013 01:21:28 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Event Handling]]></category>
		<category><![CDATA[ActionListener]]></category>
		<category><![CDATA[Applet]]></category>
		<category><![CDATA[Listener]]></category>
		<category><![CDATA[NullLayout]]></category>
		<category><![CDATA[Thread]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=411</guid>
		<description><![CDATA[<p>This tutorial is on applying/handling multiple events in Applet based Application. What we are going to develop is a utility which selects the value of Red, Green and Blue in the range of 0 &#8211; 255 by using Scrollbar components. After setting up those three values, user can set the generated Color as Background. And [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/color-chooser-multiple-event-handling">Color Chooser &#8211; multiple event handling</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>This tutorial is on applying/handling multiple events in Applet based Application. What we are going to develop is a utility which selects the value of Red, Green and Blue in the range of 0 &#8211; 255 by using Scrollbar components. After setting up those three values, user can set the generated Color as Background. And once the color is set, we gonna make it black in 10 seconds by a smooth change in Color intensity.</p>
<p><strong>If you are not clear with the program aim then scroll down and see the output and read again! </strong></p>
<p><strong>Declaring and Defining Array of components:</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.applet.*;

public class ColorChooserApplet extends Applet 
{
	Button btn = new Button(&quot;Run&quot;);
	Label lbl[] = new Label[3];
	Scrollbar sc[] = new Scrollbar[3];
	TextField tf[] = new TextField[3];
	String[] slbl = {&quot;Red&quot;,&quot;Green&quot;,&quot;Blue&quot;};
	public void init()
	{
		for(int i=0;i&lt;3;i++)
		{
			lbl[i] = new Label(slbl[i]);	
			sc[i] = new Scrollbar(Scrollbar.HORIZONTAL, 0, 20, 0, 275);
			tf[i] = new  TextField(&quot;0&quot;);
			add(lbl[i]);
			add(sc[i]);
			add(tf[i]);
		}
		add(btn);
	}
}

</pre>
<hr/>
<strong>Applying null layout:</strong><br />
Java based desktop applications support various Layouts. Default Layout of Applet application is FlowLayout in which components are attached one after another as per the direction set by use(default:center). Now in case we need exact positioning of components, We can remove the layout by calling a method setLayout(null) and add the components. After setting null layout if we try to add components, they will not be visible in output. To make it visible we have to call a method named setBounds(int x_cord, int y_cord, int component_width, int component_height) for each component object.</p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class ColorChooserApplet extends Applet 
{
	Button btn = new Button(&quot;Run&quot;);
	Label lbl[] = new Label[3];
	Scrollbar sc[] = new Scrollbar[3];
	TextField tf[] = new TextField[3];
	String[] slbl = {&quot;Red&quot;,&quot;Green&quot;,&quot;Blue&quot;};
	public void init()
	{
		setLayout(null);

		for(int i=0;i&lt;3;i++)
		{
			lbl[i] = new Label(slbl[i]);	
			sc[i] = new Scrollbar(Scrollbar.HORIZONTAL, 0, 20, 0, 275);
			tf[i] = new  TextField(&quot;0&quot;);
			add(lbl[i]);
			add(sc[i]);
			add(tf[i]);
			lbl[i].setBounds(20,(75*i)+50, 50,30);
			sc[i].setBounds(70,(75*i)+50, 350,30);
			tf[i].setBounds(450,(75*i)+50, 50, 30);

		}
		add(btn);
		btn.setBounds(200,400,100,50);
	}
	
}

</pre>
<hr/>
<p><strong>Code to sync Scrollbars and TextFields:</strong><br />
In our application, When user moves Scrollbar the current value of scroll bar is to be displayed/updated in text field and vice versa.</p>
<pre class="brush: java; title: ; notranslate">
        public void adjustmentValueChanged(AdjustmentEvent e) 
        {
		for(int i=0;i&lt;3;i++)
		{
			tf[i].setText(sc[i].getValue()+&quot;&quot;);	
		}
			
        }
	 
        public void keyReleased(KeyEvent e) 
        {
		System.out.println(&quot;Release:&quot;+tf[0].getText());

		for(int i=0;i&lt;3;i++)
		{
			sc[i].setValue(Integer.parseInt(tf[i].getText()));	
		}
        }
</pre>
<hr/>
<p><strong>Full Program:</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class ColorChooserApplet extends Applet implements AdjustmentListener, KeyListener, ActionListener, Runnable
{
	int r=0,g=0,b=0;
	Thread t;
	Button btn = new Button(&quot;Run&quot;);
	Label lbl[] = new Label[3];
	Scrollbar sc[] = new Scrollbar[3];
	TextField tf[] = new TextField[3];
	String[] slbl = {&quot;Red&quot;,&quot;Green&quot;,&quot;Blue&quot;};
	public void init()
	{
		setLayout(null);
		
		for(int i=0;i&lt;3;i++)
		{
			lbl[i] = new Label(slbl[i]);	
			sc[i] = new Scrollbar(Scrollbar.HORIZONTAL, 0, 20, 0, 275);
			tf[i] = new  TextField(&quot;0&quot;);
			add(lbl[i]);
			add(sc[i]);
			add(tf[i]);
			lbl[i].setBounds(20,(75*i)+50, 50,30);
			sc[i].setBounds(70,(75*i)+50, 350,30);
			tf[i].setBounds(450,(75*i)+50, 50, 30);
			
			sc[i].addAdjustmentListener(this);
			tf[i].addKeyListener(this);
		}
		add(btn);
		btn.setBounds(200,400,100,50);
		btn.addActionListener(this);
	}
	public void adjustmentValueChanged(AdjustmentEvent e) 
	{
		for(int i=0;i&lt;3;i++)
		{
			tf[i].setText(sc[i].getValue()+&quot;&quot;);	
		}
			
	}
	public void	keyPressed(KeyEvent e)
	{
		System.out.println(&quot;Pressed:&quot;+tf[0].getText());
		
	} 
    public void	keyReleased(KeyEvent e) 
	{
		System.out.println(&quot;Release:&quot;+tf[0].getText());

		for(int i=0;i&lt;3;i++)
		{
			sc[i].setValue(Integer.parseInt(tf[i].getText()));	
		}
	}
    public void	keyTyped(KeyEvent e)
	{
		System.out.println(&quot;Typed:&quot;+tf[0].getText());

	} 
	public void actionPerformed(ActionEvent e)
	{
		r = sc[0].getValue();
		g = sc[1].getValue();
		b = sc[2].getValue();
		Color clr = new Color(r,g,b);	
		setBackground(clr);
		t = new Thread(this);
		t.start();
	}
	
	public void run()
	{
		try
		{
			for(int i=0;i&lt;10;i++)
			{
				t.sleep(1000);
				r = r - sc[0].getValue()/10;
				g = g - sc[1].getValue()/10;
				b = b - sc[2].getValue()/10;
				Color clr = new Color(r,g,b);	
				setBackground(clr);			
			}
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		
	}
}

/*
&lt;applet code=&quot;ColorChooserApplet&quot; width=&quot;550&quot; height=&quot;550&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/color-chooser-multiple-event-handling">Color Chooser &#8211; multiple event handling</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/event-handling/color-chooser-multiple-event-handling/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionListener Applet based Calculator</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/actionlistener-applet-based-calculator?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=actionlistener-applet-based-calculator</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/actionlistener-applet-based-calculator#comments</comments>
		<pubDate>Fri, 11 Jan 2013 01:11:13 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Event Handling]]></category>
		<category><![CDATA[Listener]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=409</guid>
		<description><![CDATA[<p>ActionListener is used to handle click event on clickable components like Button, MenuItem, JToggleButton, etc. ActionListener Interface As per the event delegation model we are going to implement this interface in our class. ActionListener consists of 1 method which is as below. public void actionPerformed(ActionEvent e) To add ActionListener on any component the syntax is: [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/actionlistener-applet-based-calculator">ActionListener Applet based Calculator</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>ActionListener is used to handle click event on clickable components like Button, MenuItem, JToggleButton, etc.</p>
<h2>ActionListener Interface</h2>
<p>As per the event delegation model we are going to implement this interface in our class. ActionListener consists of 1 method which is as below.</p>
<ol>
<li>public void actionPerformed(ActionEvent e)</li>
</ol>
<p><strong>To add ActionListener on any component the syntax is:</strong>  btn.addActionListener(this);<br />
For further reference you can refer my article on <a href="tutorials/java-tutorials/event-handling/event-handling-methodologies" title="Event Handling Methodologies"><strong>Event Handling Methodologies</strong></a>.</p>
<p><strong>Program:</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class AppletActionListener extends Applet implements ActionListener 
{
	Label l1,l2,l3;
	TextField t1,t2,ans;
	Button btn,btn2,btn3,btn4;
	public void init()
	{
		l1 = new Label(&quot;OP1: &quot;);	
		l2 = new Label(&quot;OP2: &quot;);	
		l3 = new Label(&quot;Ans: &quot;);	
		t1 = new TextField(&quot;0&quot;,20);
		t2 = new TextField(&quot;0&quot;,20);
		ans = new TextField(20);
		btn = new Button(&quot;+&quot;);
		btn2 = new Button(&quot;-&quot;);
		btn3 = new Button(&quot;*&quot;);
		btn4 = new Button(&quot;/&quot;);
		btn.addActionListener(this);
		btn2.addActionListener(this);
		btn3.addActionListener(this);
		btn4.addActionListener(this);
		add(l1);add(t1);
		add(l2);add(t2);
		add(btn);add(btn2);add(btn3);add(btn4);
		add(l3);add(ans);


	}	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource()==btn)
		{
		ans.setText(Integer.parseInt(t1.getText()) + Integer.parseInt(t2.getText())+&quot;&quot;);
		}
		else if(e.getSource()==btn2)
		{
		ans.setText(Integer.parseInt(t1.getText()) - Integer.parseInt(t2.getText())+&quot;&quot;);
		}
		else if(e.getSource()==btn3)
		{
		ans.setText(Integer.parseInt(t1.getText()) * Integer.parseInt(t2.getText())+&quot;&quot;);
		}
		else if(e.getSource()==btn4)
		{
		ans.setText(Integer.parseInt(t1.getText()) / Integer.parseInt(t2.getText())+&quot;&quot;);
		}
		
	}
}
/*
&lt;applet code=&quot;AppletActionListener&quot; width=&quot;190&quot; height=&quot;200&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/actionlistener-applet-based-calculator">ActionListener Applet based Calculator</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/event-handling/actionlistener-applet-based-calculator/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>KeyListener Applet</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/keylistener-applet?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=keylistener-applet</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/keylistener-applet#comments</comments>
		<pubDate>Fri, 11 Jan 2013 00:48:19 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Event Handling]]></category>
		<category><![CDATA[Listener]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=403</guid>
		<description><![CDATA[<p>KeyListener is generally used with Text related components like TextField, TextArea, etc. KeyListener Interface As per the event delegation model we are going to implement this interface in our class. KeyListener consists of 3 methods which are as below. public void keyPressed(KeyEvent e) public void keyReleased(KeyEvent e) public void keyTyped(KeyEvent e) To add KeyListener on [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/keylistener-applet">KeyListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>KeyListener is generally used with Text related components like TextField, TextArea, etc.</p>
<h2>KeyListener Interface</h2>
<p>As per the event delegation model we are going to implement this interface in our class. KeyListener consists of 3 methods which are as below.</p>
<ol>
<li>public void keyPressed(KeyEvent e)</li>
<li>public void keyReleased(KeyEvent e)</li>
<li>public void keyTyped(KeyEvent e)</li>
</ol>
<p><strong>To add KeyListener on any component/applet the syntax is:</strong>  textObj.addKeyListener(this);<br />
For further reference you can refer my article on <a href="tutorials/java-tutorials/event-handling/event-handling-methodologies" title="Event Handling Methodologies"><strong>Event Handling Methodologies</strong></a>.</p>
<p><strong>Program:</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AppletKeyEvents extends Applet implements KeyListener
{
	Label l1, l2;
	TextField tf;
	TextArea ta;
	public void init() 
	{
		l1 = new Label(&quot;Enter Text:&quot;);
		l2 = new Label(&quot;Output:&quot;);
		
		ta = new TextArea(7,50);
		tf = new TextField(20);
		
		tf.addKeyListener(this);
		
		add(l1);
		add(tf);		
		add(l2);
		add(ta);
		
	}
	public void keyTyped ( KeyEvent e )
	{  
  		ta.append(&quot;Key Typed: &quot; + tf.getText()+&quot;\n&quot;);
 	}  
    public void keyPressed ( KeyEvent e)
	{  
  		ta.append ( &quot;Key Pressed: &quot;+ tf.getText() +&quot;\n&quot;) ; 
  	}  
 	public void keyReleased ( KeyEvent e )
  	{  
  		ta.append( &quot;Key Released: &quot;+ tf.getText()+&quot;\n&quot; ) ; 
  	}  

}
/*
&lt;applet code=&quot;AppletKeyEvents&quot; width=&quot;500&quot; height=&quot;300&quot;&gt;&lt;/applet&gt;
*/

</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/keylistener-applet">KeyListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/event-handling/keylistener-applet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ItemListener Applet</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/itemlistener-applet?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=itemlistener-applet</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/itemlistener-applet#comments</comments>
		<pubDate>Thu, 10 Jan 2013 17:34:05 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Event Handling]]></category>
		<category><![CDATA[Listener]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=401</guid>
		<description><![CDATA[<p>ItemListener is applicable to selectable components which can have only 2 states: selected/deselected. Like Checkbox, Radio Button, etc. ItemListener Interface As per the event delegation model we are going to implement this interface in our class. ItemListener consists of 1 method which is as below. public void itemStateChanged(ItemEvent e) To add ItemListener on any component, [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/itemlistener-applet">ItemListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>ItemListener is applicable to selectable components which can have only 2 states: selected/deselected. Like Checkbox, Radio Button, etc.</p>
<h2>ItemListener Interface</h2>
<p>As per the event delegation model we are going to implement this interface in our class. ItemListener consists of 1 method which is as below.</p>
<ol>
<li>public void itemStateChanged(ItemEvent e)</li>
</ol>
<p><strong>To add ItemListener on any component, syntax is:</strong>  checkboxObject.addItemListener(this);<br />
For further reference you can refer my article on <a href="tutorials/java-tutorials/event-handling/event-handling-methodologies" title="Event Handling Methodologies"><strong>Event Handling Methodologies</strong></a>.</p>
<p><strong>To manually invoke paint(Graphics) method:</strong> we can call repaint(). This will internally create object of java.awt.Graphics and call paint(Graphics) internally.</p>
<p><strong>Program:</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AppletItemEvents extends Applet implements ItemListener
{
	Checkbox c1,c2,r1,r2;
	CheckboxGroup cbg;
	Label l1,l2;
	String msg1 = &quot;&quot;, msg2 = &quot;&quot;;
	public void init() 
	{
		l1 = new Label(&quot;Languages you know:&quot;);
		l2 = new Label(&quot;Gender:&quot;);
		c1 = new Checkbox(&quot;English&quot;);
		c2 = new Checkbox(&quot;French&quot;);
		cbg = new CheckboxGroup();
		r1 = new Checkbox(&quot;Male&quot;,cbg,true);
		r2 = new Checkbox(&quot;Female&quot;,cbg,false);
		
		c1.addItemListener(this);
		c2.addItemListener(this);
		r1.addItemListener(this);
		r2.addItemListener(this);
		
		add(l1);
		add(c1);
		add(c2);
		add(l2);
		add(r1);
		add(r2);		
		
	}
	public void itemStateChanged(ItemEvent ie) 
	{
		String t1 = c1.getState()?&quot;English &quot;:&quot;&quot;;
		String t2 = c2.getState()?&quot;French&quot;:&quot;&quot;;
		msg1 = &quot;Languages:&quot; + t1 + t2 ;
		String t3 = r1.getState()?&quot;Male&quot;:&quot;Female&quot;;
		msg2 = &quot;gender:&quot; + t3;
		repaint();
	}
	public void paint(Graphics g)
	{
		g.drawString(msg1, 50,200);	
		g.drawString(msg2, 50,240);	
	}
}
/*
&lt;applet code=&quot;AppletItemEvents&quot; width=&quot;300&quot; height=&quot;300&quot;&gt;&lt;/applet&gt;
*/


</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/itemlistener-applet">ItemListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/event-handling/itemlistener-applet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MouseWheelListener Applet</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/mousewheellistener-applet?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mousewheellistener-applet</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/mousewheellistener-applet#comments</comments>
		<pubDate>Thu, 10 Jan 2013 16:11:53 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Event Handling]]></category>
		<category><![CDATA[Listener]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=393</guid>
		<description><![CDATA[<p>Mouse as an input device can generate major 8 events. Among those 8 events below 1 of them can be handled by MouseWheelListener, 2 are handled by MouseMotionListener and rest can be handled using MouseListener. MouseWheelListener Interface As per the event delegation model we are going to implement this interface in our class. MouseWheelListener consists [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/mousewheellistener-applet">MouseWheelListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Mouse as an input device can generate major 8 events. Among those 8 events below 1 of them can be handled by MouseWheelListener, 2 are handled by <a href="tutorials/java-tutorials/event-handling/mousemotionlistener-applet" title="MouseMotionListener Applet">MouseMotionListener</a>  and rest can be handled using <a href="tutorials/java-tutorials/event-handling/mouselistener-applet" title="MouseListener Applet">MouseListener</a>.</p>
<h2>MouseWheelListener Interface</h2>
<p>As per the event delegation model we are going to implement this interface in our class. MouseWheelListener consists of 1 method which is as below.</p>
<ol>
<li>public void mouseWheelMoved(MouseWheelEvent e)</li>
</ol>
<p><strong>To add MouseWheelListener on whole applet area the syntax will be:</strong>  this.addMouseMotionListener(this);<br />
In above line 1st &#8220;this&#8221; stands for current class on which we would like to handle the Mouse related events. And 2nd &#8220;this&#8221; is the object of current class which has implemented MouseMotionListener and defined all the methods. For further reference you can refer my article on <a href="tutorials/java-tutorials/event-handling/event-handling-methodologies" title="Event Handling Methodologies"><strong>Event Handling Methodologies</strong></a>.</p>
<p><strong>To manually invoke paint(Graphics) method:</strong> we can call repaint(). This will internally create object of java.awt.Graphics and call paint(Graphics) internally.</p>
<p><strong>Program:</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AppletMouseWheelEvents extends Applet implements MouseWheelListener
{
	int mouseX = 0, mouseY = 0;
	int scrollAmount = 0;
	int scrollType = 0;
	int scrollUnits = 0;
	int scrollRotation = 0;
	
	public void init() 
	{
		this.addMouseWheelListener(this);
	}
	public void mouseWheelMoved(MouseWheelEvent me) 
	{
		scrollAmount = me.getScrollAmount();
		scrollType = me.getScrollType();
		scrollUnits = me.getUnitsToScroll();
		scrollRotation = me.getWheelRotation();
		repaint();
	}
	
	public void paint(Graphics g) 
	{
		g.drawString(&quot;Scroll Amount:&quot; + scrollAmount, 50, 50);
		g.drawString(&quot;Scroll Type:&quot; + scrollType, 50, 70);
		g.drawString(&quot;Scroll Units:&quot; + scrollUnits, 50, 90);
		g.drawString(&quot;Scroll Rotations:&quot; + scrollRotation, 50, 110);
	}
}
/*
&lt;applet code=&quot;AppletMouseWheelEvents&quot; width=&quot;300&quot; height=&quot;300&quot;&gt;&lt;/applet&gt;
*/

</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/mousewheellistener-applet">MouseWheelListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/event-handling/mousewheellistener-applet/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MouseMotionListener Applet</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/mousemotionlistener-applet?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mousemotionlistener-applet</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/mousemotionlistener-applet#comments</comments>
		<pubDate>Thu, 10 Jan 2013 14:58:53 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Event Handling]]></category>
		<category><![CDATA[Listener]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=381</guid>
		<description><![CDATA[<p>Mouse as an input device can generate major 8 events. Among those 8 events below 2 of them can be handled by MouseMotionListener, 1 is handled by MouseWheelListener and rest can be handled using MouseListener. MouseMotionListener Interface As per the event delegation model we are going to implement this interface in our class. MouseMotionListener consists [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/mousemotionlistener-applet">MouseMotionListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Mouse as an input device can generate major 8 events. Among those 8 events below 2 of them can be handled by MouseMotionListener, 1 is handled by <a href="tutorials/java-tutorials/event-handling/mousewheellistener-applet" title="MouseWheelListener Applet">MouseWheelListener</a>  and rest can be handled using <a href="tutorials/java-tutorials/event-handling/mouselistener-applet" title="MouseListener Applet">MouseListener</a>.</p>
<h2>MouseMotionListener Interface</h2>
<p>As per the event delegation model we are going to implement this interface in our class. MouseMotionListener consists of 2 methods which are as below.</p>
<ol>
<li>public void mouseMoved(MouseEvent e)</li>
<li>public void mouseDragged(MouseEvent e)</li>
</ol>
<p><strong>To add MouseMotionListener on whole applet area the syntax will be:</strong>  this.addMouseMotionListener(this);<br />
In above line 1st &#8220;this&#8221; stands for current class on which we would like to handle the Mouse related events. And 2nd &#8220;this&#8221; is the object of current class which has implemented MouseMotionListener and defined all the methods. For further reference you can refer my article on <a href="tutorials/java-tutorials/event-handling/event-handling-methodologies" title="Event Handling Methodologies"><strong>Event Handling Methodologies</strong></a>.</p>
<p><strong>To manually invoke paint(Graphics) method:</strong> we can call repaint(). This will internally create object of java.awt.Graphics and call paint(Graphics) internally.</p>
<p><strong>Program:</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AppletMouseMotionEvents extends Applet implements MouseMotionListener
{
	int mouseX = 0, mouseY = 0; 
	String msg = &quot;&quot;;
	public void init() 
	{
		this.addMouseMotionListener(this);
	}
	public void mouseMoved(MouseEvent me) 
	{
		mouseX = me.getX();
		mouseY = me.getY();
		msg = &quot;Moved&quot;;
		repaint();
	}
	public void mouseDragged(MouseEvent me) 
	{
		mouseX = me.getX();
		mouseY = me.getY();
		msg = &quot;Dragged&quot;;
		repaint();
	}
	
	public void paint(Graphics g) 
	{
		g.drawString(msg+&quot;(&quot;+mouseX+&quot;,&quot;+mouseY+&quot;)&quot;, mouseX, mouseY);
	}
}
/*
&lt;applet code=&quot;AppletMouseMotionEvents&quot; width=&quot;300&quot; height=&quot;300&quot;&gt;&lt;/applet&gt;
*/


</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/mousemotionlistener-applet">MouseMotionListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/event-handling/mousemotionlistener-applet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MouseListener Applet</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/mouselistener-applet?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mouselistener-applet</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/mouselistener-applet#comments</comments>
		<pubDate>Thu, 10 Jan 2013 14:34:05 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Event Handling]]></category>
		<category><![CDATA[Listener]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=376</guid>
		<description><![CDATA[<p>Mouse as an input device can generate major 8 events. Among those 8 events below 5 of them can be handled by MouseListener, 1 is handled by MouseWheelListener and rest can be handled using MouseMotionListener. MouseListener Interface As per the event delegation model we are going to implement this interface in our class. MouseListener consists [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/mouselistener-applet">MouseListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Mouse as an input device can generate major 8 events. Among those 8 events below 5 of them can be handled by MouseListener, 1 is handled by <a href="tutorials/java-tutorials/event-handling/mousewheellistener-applet" title="MouseWheelListener Applet">MouseWheelListener</a>  and rest can be handled using <a href="tutorials/java-tutorials/event-handling/mousemotionlistener-applet" title="MouseMotionListener Applet">MouseMotionListener</a>.</p>
<h2>MouseListener Interface</h2>
<p>As per the event delegation model we are going to implement this interface in our class. MouseListener consists of 5 methods which are as below.</p>
<ol>
<li>public void mouseClicked(MouseEvent e)</li>
<li>public void mouseEntered(MouseEvent e)</li>
<li>public void mouseExited(MouseEvent e)</li>
<li>public void mousePressed(MouseEvent e) </li>
<li>public void mouseReleased(MouseEvent e) </li>
</ol>
<p><strong>To add MouseListener on whole applet area the syntax will be:</strong>  this.addMouseListener(this);<br />
In above line 1st &#8220;this&#8221; stands for current class on which we would like to handle the Mouse related events. And 2nd &#8220;this&#8221; is the object of current class which has implemented MouseListener and defined all the methods. For further reference you can refer my article on <a href="tutorials/java-tutorials/event-handling/event-handling-methodologies" title="Event Handling Methodologies"><strong>Event Handling Methodologies</strong></a>.</p>
<p><strong>To manually invoke paint(Graphics) method:</strong> we can call repaint(). This will internally create object of java.awt.Graphics and call paint(Graphics) internally.</p>
<p><strong>Program:</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AppletMouseEvents extends Applet implements MouseListener
{
	String msg = &quot;&quot;;
	int mouseX = 0, mouseY = 0; 
	public void init() 
	{
		this.addMouseListener(this);
	}
	public void mouseClicked(MouseEvent me) 
	{
		mouseX = me.getX();
		mouseY = me.getY();
		msg = &quot;Mouse clicked.&quot;;
		repaint();
	}
	public void mouseEntered(MouseEvent me) 
	{
		mouseX = me.getX();
		mouseY = me.getY();
		msg = &quot;Mouse entered.&quot;;
		repaint();
	}
	public void mouseExited(MouseEvent me) 
	{
		mouseX = me.getX();
		mouseY = me.getY();
		msg = &quot;Mouse exited.&quot;;
		repaint();
	}
	public void mousePressed(MouseEvent me) 
	{
		mouseX = me.getX();
		mouseY = me.getY();
		msg = &quot;Down&quot;;
		repaint();
	}
	public void mouseReleased(MouseEvent me) 
	{
		mouseX = me.getX();
		mouseY = me.getY();
		msg = &quot;Up&quot;;
		repaint();
	}
	
	public void paint(Graphics g) 
	{
		g.drawString(msg, mouseX, mouseY);
	}
}
/*
&lt;applet code=&quot;AppletMouseEvents&quot; width=&quot;300&quot; height=&quot;300&quot;&gt;&lt;/applet&gt;
*/

</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/mouselistener-applet">MouseListener Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/event-handling/mouselistener-applet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending Adapter Class</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/event-handling-methodologies/extending-adapter-class?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=extending-adapter-class</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/event-handling-methodologies/extending-adapter-class#comments</comments>
		<pubDate>Thu, 10 Jan 2013 11:12:35 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Methodologies]]></category>
		<category><![CDATA[Event Delegation Model]]></category>
		<category><![CDATA[Event Handling]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=369</guid>
		<description><![CDATA[<p>As we all know that: Java do not support multiple inheritance we cannot derive our Applet/Frame/JFrame based classes by any other class at the same time. In that case what can be done is: we can create a new class and that class will be derived from Adapter class. And our newly derived Adapter class [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/event-handling-methodologies/extending-adapter-class">Extending Adapter Class</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>As we all know that: Java do not support multiple inheritance we cannot derive our Applet/Frame/JFrame based classes by any other class at the same time. In that case what can be done is: we can create a new class and that class will be derived from Adapter class. And our newly derived Adapter class can be used as an argument of addXYZListener() method.</p>
<h4>Characteristics:</h4>
<ol>
<li>Components/Event Sources cannot be easily accessed from Listener methods. We must have object of base class(where we have components) in the Listener class(where we have Listener methods)</li>
<li>Event can be applied to a component by passing object of newly created adapter class which extends built in adapter class.</li>
<li>View and Controller part can be separated which is a good practice.</li>
<li>Not compulsory to define all the methods. No need to write Blank definition if not required.</li>
</ol>
<p>Code:</p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class EventDemo5 extends Applet
{
	Button btn;
	static Label lbl;
	public void init()
	{
		btn = new Button(&quot;Click Me!&quot;);
		lbl = new Label(&quot;          &quot;);
		add(btn);
		add(lbl);

		myAdapterClass myObj = new myAdapterClass();
		btn.addMouseListener(myObj);		
	}		 
 }
 
 class myAdapterClass extends MouseAdapter
 {
	public void mouseClicked(MouseEvent e)
	{
	 EventDemo5.lbl.setText(&quot;Clicked!&quot;);
	} 
  }
  
/*
&lt;applet code=&quot;EventDemo5&quot; width=&quot;500&quot; height=&quot;50&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/event-handling-methodologies/extending-adapter-class">Extending Adapter Class</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/event-handling/event-handling-methodologies/extending-adapter-class/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Anonymous Adapter Class</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/event-handling-methodologies/anonymous-adapter-class?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=anonymous-adapter-class</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/event-handling-methodologies/anonymous-adapter-class#comments</comments>
		<pubDate>Thu, 10 Jan 2013 11:10:46 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Methodologies]]></category>
		<category><![CDATA[Event Delegation Model]]></category>
		<category><![CDATA[Event Handling]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=366</guid>
		<description><![CDATA[<p>An Adapter class is provided in java.awt.event package for those Listener interfaces which are having more than one method. Such as MouseAdapter, WindowAdapter, etc. Adapter class is derived from Listener interface and it provides blank definition of all the methods of derived listener. So that when we use Adapter class instead of Listener interface we [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/event-handling-methodologies/anonymous-adapter-class">Anonymous Adapter Class</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>An Adapter class is provided in java.awt.event package for those Listener interfaces which are having more than one method. Such as MouseAdapter, WindowAdapter, etc. Adapter class is derived from Listener interface and it provides blank definition of all the methods of derived listener. So that when we use Adapter class instead of Listener interface we can skip writing any of the method which are unused. </p>
<h4>Characteristics:</h4>
<ol>
<li>Components/Event Sources can be easily accessed from Listener methods</li>
<li>To apply an event we have to pass anonymous object of Adapter class</li>
<li>Logic is to be written in complex inner methods</li>
<li>Not compulsory to define all the methods. No need to write Blank definition if not required.</li>
</ol>
<p>Code:</p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class EventDemo4 extends Applet
{
	Button btn;
	Label lbl;
	public void init()
	{
		btn = new Button(&quot;Click Me!&quot;);
		lbl = new Label(&quot;          &quot;);
		add(btn);
		add(lbl);

		btn.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				lbl.setText(&quot;Clicked&quot;);
			}
		});		
	}	
	 
 }
/*
&lt;applet code=&quot;EventDemo4&quot; width=&quot;500&quot; height=&quot;50&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/event-handling-methodologies/anonymous-adapter-class">Anonymous Adapter Class</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/event-handling/event-handling-methodologies/anonymous-adapter-class/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
