<?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; Click Event</title>
	<atom:link href="http://ankit.co/tag/click-event/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>ActionListener &#8211; Click event handling</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/actionlistener-click-event-handling?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=actionlistener-click-event-handling</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/actionlistener-click-event-handling#comments</comments>
		<pubDate>Wed, 09 Jan 2013 14:27:07 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Event Handling]]></category>
		<category><![CDATA[ActionListener]]></category>
		<category><![CDATA[Click Event]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=327</guid>
		<description><![CDATA[<p>Action(Click) Event Delegation Model Event Listeners: Listener Interface: java.awt.event.ActionListener Methods: public void actionPerformed(ActionEvent e) Events: java.awt.event.ActionEvent Events Source: Button, MenuItem E.g. Applicable on: ActionListener is used to handle click event on clickable entities. Steps to apply an event handling Import class/classes: like import java.awt.event.*; Implement Listener interface in a class. Define all the methods of [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/actionlistener-click-event-handling">ActionListener &#8211; Click event handling</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h2>Action(Click) Event Delegation Model</h2>
<h4>Event Listeners:</h4>
<p>Listener Interface: java.awt.event.ActionListener<br />
Methods:  public void actionPerformed(ActionEvent e)</p>
<h4>Events:</h4>
<p>java.awt.event.ActionEvent</p>
<h4>Events Source:</h4>
<p>Button, MenuItem E.g. </p>
<pre class="brush: java; title: ; notranslate">
Button b = new Button(&quot;Ok&quot;);
b.addActionListener(this);
</pre>
<p><br/></p>
<h4>Applicable on:</h4>
<p>ActionListener is used to handle click event on clickable entities.</p>
<h2>Steps to apply an event handling</h2>
<ol>
<li><strong>Import class/classes: </strong>like <strong>import java.awt.event.*;</strong></li>
<li>Implement Listener interface in a class.</li>
<li>Define all the methods of implemented interface and write the logic in appropriate method.</li>
<li>Apply Listener to a component.</li>
</ol>
<p><br/></p>
<h3>Program to Handle Click Event on a Button:</h3>
<p><strong>File Name: clickDemo.java</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class clickDemo extends Applet implements ActionListener
{
	Button btn;
	TextField tf;
	public void init()
	{
		btn = new Button(&quot;Increment&quot;);
		tf = new TextField(&quot;0&quot;, 20);
		
		add(tf);
		add(btn);
		
		btn.addActionListener(this);
	}	
	public void actionPerformed(ActionEvent e)
	{
		int val = Integer.parseInt(tf.getText());
		val++;
		tf.setText(val+&quot;&quot;);		
	}
}
/*
&lt;applet code=&quot;clickDemo&quot; width=&quot;500&quot; height=&quot;50&quot;&gt;&lt;/applet&gt;
*/
</pre>
<h2>Points to be noted</h2>
<p>1. actionPerformed() method will be automatically invoked whenever Button is clicked. </p>
<pre class="brush: java; title: ; notranslate">
    public void actionPerformed(ActionEvent e)
    {
    }
</pre>
<p>2. <b>tf.getText()</b> is used to retrieve current value of TextField named tf.</p>
<pre class="brush: java; title: ; notranslate">
        int val = Integer.parseInt(tf.getText());
        val++;
        tf.setText(val+&quot;&quot;);     
</pre>
<p>3. In the above code snippet we have used tf.setText() method which takes a String as argument. But our variable &#8220;val&#8221; is int that&#8217;s why we have type cast it to String by appending &#8220;&#8221;.</p>
<hr/>
<br/></p>
<h2>Applying single event listener to multiple components.</h2>
<p>Now we are planning 2 Buttons. 1) Increment 2) Decrement. Here the problem is we have only 1 method which will be automatically called on clicking any of the Button. But we would like to execute different logic in both the cases.</p>
<p><strong>File Name: twoClickDemo.java</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class twoClickDemo extends Applet implements ActionListener
{
	Button btn, btn2;
	TextField tf;
	public void init()
	{
		btn = new Button(&quot;Increment&quot;);
		btn2 = new Button(&quot;Decrement&quot;);
		
		tf = new TextField(&quot;0&quot;, 20);
		
		add(tf);
		add(btn);
		add(btn2);
		
		btn.addActionListener(this);
		btn2.addActionListener(this);
	}	
	public void actionPerformed(ActionEvent e)
	{
		int val = Integer.parseInt(tf.getText());
		if(e.getSource()==btn)
		{
			val++;
		}
		else
		{
			val--;	
		}
		tf.setText(val+&quot;&quot;);		
	}
}
/*
&lt;applet code=&quot;twoClickDemo&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/actionlistener-click-event-handling">ActionListener &#8211; Click 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/actionlistener-click-event-handling/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
