<?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>Suraj&#039;s blog &#187; ActionListener</title>
	<atom:link href="http://www.surajshrestha.com.np/tag/actionlistener/feed" rel="self" type="application/rss+xml" />
	<link>http://www.surajshrestha.com.np</link>
	<description>imagination more important than knowledge</description>
	<lastBuildDate>Sun, 08 Aug 2010 23:20:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A simple JMenu and JMenuItem Demonstration</title>
		<link>http://www.surajshrestha.com.np/java/a-simple-jmenu-and-jmenuitem-demonstration.html</link>
		<comments>http://www.surajshrestha.com.np/java/a-simple-jmenu-and-jmenuitem-demonstration.html#comments</comments>
		<pubDate>Sun, 05 Apr 2009 18:49:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[ActionListener]]></category>
		<category><![CDATA[JFrame]]></category>
		<category><![CDATA[JMenu]]></category>
		<category><![CDATA[JMenuBar]]></category>
		<category><![CDATA[JMenuItem]]></category>

		<guid isPermaLink="false">http://www.surajshrestha.com.np/?p=97</guid>
		<description><![CDATA[Menu and Menu Items are tools to facilitate users to command the software they are using. JMenu and JMenuItem are Swing components of Java which work exactly as Menu and MenuItems of AWT (Abstract Window Toolkit) of Java. In this post, I have shown how to use JMenu and JMenuItems in Java.
First of all, we [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 10pt;">Menu and Menu Items are tools to facilitate users to command the software they are using. JMenu and JMenuItem are Swing components of Java which work exactly as Menu and MenuItems of AWT (Abstract Window Toolkit) of Java. In this post, I have shown how to use JMenu and JMenuItems in Java.</p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri; font-size: small;">First of all, we have to create a JFrame window which acts as container for all components. Then we have to add JMenuBar in it. The JMenuBar will contain JMenus and each JMenu will contain JMenuItems. During actual coding we go in reverse order. First we create JMenuItems. We give a label to it and add ActionListener to it. ActionListener is the way of saying JVM (Java Virtual Machine) which method to execute when the user clicks the particular JMenuItem. After that, we create JMenus and add those created JMenuItems in required JMenus. Then after, we create JMenuBar and add those JMenus in it in required order. Finally, we attach the JMenuBar to the JFrame. To demonstrate all these, I have created two java files -<span style="mso-spacerun: yes;">  </span>AppMenuBar.java and JMenuTest.java. The Full source code with descriptive comments can be downloaded here. – <a title="Sample Code for JMenubar, JMenu and JMenuItems" href="http://www.surajshrestha.com.np/wp-content/uploads/2009/04/JMenuTest.zip" target="_blank">Source Code</a>.</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt;"> </span></p>
<p class="MsoNoSpacing" style="margin: 0in 0in 0pt;"><span style="font-family: Calibri; font-size: small;">Sample code</span></p>
<p class="MsoNoSpacing" style="margin: 0in 0in 0pt;"><span style="font-family: Calibri; font-size: small;"> </span></p>
<p class="MsoNoSpacing" style="margin: 0in 0in 0pt;"><span style="font-family: Calibri; font-size: small;">To create JMenuItem.</span></p>
<p class="MsoNoSpacing" style="margin: 0in 0in 0pt;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">JMenuItem item = </span><strong><span style="font-family: &quot;Courier New&quot;; color: #7f0055; font-size: 10pt;">new</span></strong><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;"> JMenuItem(</span><span style="font-family: &quot;Courier New&quot;; color: #2a00ff; font-size: 10pt;">&#8220;Show All Empty Seats&#8221;</span><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">);</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">item.addActionListener(</span><strong><span style="font-family: &quot;Courier New&quot;; color: #7f0055; font-size: 10pt;">this</span></strong><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">);</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;">This creates a menu item with label ‘Show All Empty Seats’ and adds itself (the object which is calling the statement—the object of class AppMenuBar). </span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="mso-spacerun: yes;"><span style="font-family: Calibri; font-size: small;"> </span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">JMenu </span><span style="font-family: &quot;Courier New&quot;; color: #0000c0; font-size: 10pt;">showMenu </span><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">=<span style="mso-spacerun: yes;">  </span></span><strong><span style="font-family: &quot;Courier New&quot;; color: #7f0055; font-size: 10pt;">new</span></strong><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;"> JMenu(</span><span style="font-family: &quot;Courier New&quot;; color: #2a00ff; font-size: 10pt;">&#8220;Show&#8221;</span><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">);</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; color: #0000c0; font-size: 10pt;">showMenu</span><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">.add(item);</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;">These above statements create a JMenu with label ‘Show’ and add a JMenuItem created above.</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;">The signature of actionPerformed() method in class which implements the ActionListener Interface should be like this.</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><strong><span style="font-family: &quot;Courier New&quot;; color: #7f0055; font-size: 10pt;">public</span></strong><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;"> </span><strong><span style="font-family: &quot;Courier New&quot;; color: #7f0055; font-size: 10pt;">void</span></strong><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;"> <strong style="mso-bidi-font-weight: normal;">actionPerformed</strong>(ActionEvent e) {</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;"><span style="mso-tab-count: 2;">            </span></span><strong><span style="font-family: &quot;Courier New&quot;; color: #7f0055; font-size: 10pt;">if</span></strong><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">(e.getActionCommand()==</span><span style="font-family: &quot;Courier New&quot;; color: #2a00ff; font-size: 10pt;">&#8220;Reserve a Seat&#8221;</span><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">){</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;"><span style="mso-tab-count: 3;">                  </span></span><span style="font-family: &quot;Courier New&quot;; color: #0000c0; font-size: 10pt;">jMenuTest</span><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">.showMessage(</span><span style="font-family: &quot;Courier New&quot;; color: #2a00ff; font-size: 10pt;">&#8220;A Seat Reserved&#8221;</span><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">);<span style="mso-tab-count: 3;">             </span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;"><span style="mso-tab-count: 2;">            </span>}</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">}</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;">Finally, the JMenus created above can be added to JMenuBar like below.</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">JMenuBar menubar = </span><strong><span style="font-family: &quot;Courier New&quot;; color: #7f0055; font-size: 10pt;">new</span></strong><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;"><span style="mso-spacerun: yes;">  </span>JMenuBar();</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">menubar.add(</span><span style="font-family: &quot;Courier New&quot;; color: #0000c0; font-size: 10pt;">showMenu</span><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">);</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;">The menubar created above can be set as JMenuBar of the JFrame as below.</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; color: black; font-size: 10pt;">setJMenuBar(menubar);</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: Calibri; font-size: small;"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri; font-size: small;">In the source code attached, AppMenuBar.java creates a JMenuBar with a JMenu named ‘Show’ with 2 JMenuItems and another JMenu named ‘Transaction’ with 4 JMenuItems. AppMenuBar class inherits built-In class JMenuBar. It also implements ActionListener Interface class. That’s why, it has got actionPerformed() method. Any class which implemensts ActionListener Interface can be action listener of the JMenuItem and those classes will have actionPerformed() method. I have used this actionPerformed() method to change the Message label text of the calling JFrame.</span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri; font-size: small;">JMenuTest class inherits the built-in classs JFrame. In constructor method, it creates an AppMenuBar and sets that as its JMenuBar. Also, it creates a JLabel and adds that to it. I have not use any layout format so far, so the JLabel is added to middle covering the entire JFrame container. I will discuss more on the Layouts in forth coming posts. The final output picture is shown below.</span></p>
<div id="attachment_101" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-101 " title="JMenu Test Sample Picture" src="http://www.surajshrestha.com.np/wp-content/uploads/2009/04/jmenutest1-300x232.jpg" alt="JMenu Test Sample Picture" width="300" height="232" /><p class="wp-caption-text">JMenu Test Sample Picture</p></div>
<p class="MsoNormal" style="TEXT-ALIGN: center; MARGIN: 0in 0in 10pt"> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.surajshrestha.com.np/java/a-simple-jmenu-and-jmenuitem-demonstration.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

<!-- www.000webhost.com Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>
<!-- End Of Analytics Code -->
