<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: JTextArea with popup menu &#8211; JPopupTextArea</title>
	<atom:link href="http://www.objectdefinitions.com/odblog/2007/jtextarea-with-popup-menu/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.objectdefinitions.com/odblog/2007/jtextarea-with-popup-menu/</link>
	<description>Software Development - random theories, code snippits and opinions</description>
	<lastBuildDate>Thu, 19 Aug 2010 14:40:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: David Sowsy</title>
		<link>http://www.objectdefinitions.com/odblog/2007/jtextarea-with-popup-menu/comment-page-1/#comment-1388</link>
		<dc:creator>David Sowsy</dc:creator>
		<pubDate>Sat, 08 Nov 2008 21:04:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.objectdefinitions.com/odblog/2007/jtextarea-with-popup-menu/#comment-1388</guid>
		<description>Hi Nick,

 First off I want to thank you for providing this code. I have noticed a few problems and want to provide their solutions.  I was getting a NullPointerExceptions when using your code for a second instance of the JPopupTextArea in a JPanel when adding to the menu. 
 
I removed getActionByName, removed the actions HashMap, and  
 made the following changes: 

[cc lang=&quot;java&quot;]
   final static String COPY = &quot;Copy&quot;;
   final static String CUT = &quot;Cut&quot;;
   final static String PASTE = &quot;Paste&quot;;
   final static String SELECTALL = &quot;Select All&quot;;

   private void addPopupMenu() {
	final JPopupMenu menu = new JPopupMenu();
	final JMenuItem copyItem = new JMenuItem();
 copyItem.setAction(getActionMap().get(DefaultEditorKit.copyAction));
	copyItem.setText(COPY);

	final JMenuItem cutItem = new JMenuItem();
	cutItem.setAction(getActionMap().get(DefaultEditorKit.cutAction));
	cutItem.setText(CUT);

	final JMenuItem pasteItem = new JMenuItem(PASTE);
	pasteItem.setAction(getActionMap().get(DefaultEditorKit.pasteAction));
	pasteItem.setText(PASTE);

	final JMenuItem selectAllItem = new JMenuItem(SELECTALL);
	selectAllItem.setAction(getActionMap().get(DefaultEditorKit.selectAllAction));
	selectAllItem.setText(SELECTALL);

	menu.add(copyItem);
	menu.add(cutItem);
	menu.add(pasteItem);
	menu.add(new JSeparator());
	menu.add(selectAllItem);

	add(menu);
	addMouseListener(new PopupTriggerMouseListener(menu, this));
}
[/cc]

Best Regards,
  David</description>
		<content:encoded><![CDATA[<p>Hi Nick,</p>
<p> First off I want to thank you for providing this code. I have noticed a few problems and want to provide their solutions.  I was getting a NullPointerExceptions when using your code for a second instance of the JPopupTextArea in a JPanel when adding to the menu. </p>
<p>I removed getActionByName, removed the actions HashMap, and<br />
 made the following changes:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:735px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> COPY <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Copy&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> CUT <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Cut&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> PASTE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Paste&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> SELECTALL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Select All&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> addPopupMenu<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajpopupmenu+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JPopupMenu</span></a> menu <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajpopupmenu+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JPopupMenu</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajmenuitem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JMenuItem</span></a> copyItem <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajmenuitem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JMenuItem</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;copyItem.<span style="color: #006633;">setAction</span><span style="color: #009900;">&#40;</span>getActionMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adefaulteditorkit+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">DefaultEditorKit</span></a>.<span style="color: #006633;">copyAction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; copyItem.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>COPY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajmenuitem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JMenuItem</span></a> cutItem <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajmenuitem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JMenuItem</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; cutItem.<span style="color: #006633;">setAction</span><span style="color: #009900;">&#40;</span>getActionMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adefaulteditorkit+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">DefaultEditorKit</span></a>.<span style="color: #006633;">cutAction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; cutItem.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>CUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajmenuitem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JMenuItem</span></a> pasteItem <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajmenuitem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JMenuItem</span></a><span style="color: #009900;">&#40;</span>PASTE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; pasteItem.<span style="color: #006633;">setAction</span><span style="color: #009900;">&#40;</span>getActionMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adefaulteditorkit+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">DefaultEditorKit</span></a>.<span style="color: #006633;">pasteAction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; pasteItem.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>PASTE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajmenuitem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JMenuItem</span></a> selectAllItem <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajmenuitem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JMenuItem</span></a><span style="color: #009900;">&#40;</span>SELECTALL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; selectAllItem.<span style="color: #006633;">setAction</span><span style="color: #009900;">&#40;</span>getActionMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adefaulteditorkit+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">DefaultEditorKit</span></a>.<span style="color: #006633;">selectAllAction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; selectAllItem.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>SELECTALL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>copyItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>cutItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>pasteItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajseparator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JSeparator</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>selectAllItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; add<span style="color: #009900;">&#40;</span>menu<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; addMouseListener<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PopupTriggerMouseListener<span style="color: #009900;">&#40;</span>menu, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Best Regards,<br />
  David</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://www.objectdefinitions.com/odblog/2007/jtextarea-with-popup-menu/comment-page-1/#comment-547</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Fri, 25 Jul 2008 08:47:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.objectdefinitions.com/odblog/2007/jtextarea-with-popup-menu/#comment-547</guid>
		<description>Hi Nelson. Thanks for the feedback! Yes using an anonymous inner class mouse adapter to trigger the popup works just as well. At the time I wrote this blog article I think I must have wanted a drop in replacement for the old AWT TextArea class which provided a default popup menu without having to code up extra mouse listeners etc - which is what the code above attempts to provide.

If you check the mouse event with e.isPopupTrigger() that is a platform independent way to detect whether the correct mouse button has been pressed, since different OS may have different conventions relating to how a user triggers the popup menu. If you read the javadocs on MouseEvent.isPopupTrigger it mentions that you need to check this on both mousePressed() and mouseReleased(). I&#039;ll update my code to do that exactly as it suggests!</description>
		<content:encoded><![CDATA[<p>Hi Nelson. Thanks for the feedback! Yes using an anonymous inner class mouse adapter to trigger the popup works just as well. At the time I wrote this blog article I think I must have wanted a drop in replacement for the old AWT TextArea class which provided a default popup menu without having to code up extra mouse listeners etc &#8211; which is what the code above attempts to provide.</p>
<p>If you check the mouse event with e.isPopupTrigger() that is a platform independent way to detect whether the correct mouse button has been pressed, since different OS may have different conventions relating to how a user triggers the popup menu. If you read the javadocs on MouseEvent.isPopupTrigger it mentions that you need to check this on both mousePressed() and mouseReleased(). I&#8217;ll update my code to do that exactly as it suggests!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nelson</title>
		<link>http://www.objectdefinitions.com/odblog/2007/jtextarea-with-popup-menu/comment-page-1/#comment-546</link>
		<dc:creator>Nelson</dc:creator>
		<pubDate>Fri, 25 Jul 2008 08:11:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.objectdefinitions.com/odblog/2007/jtextarea-with-popup-menu/#comment-546</guid>
		<description>HI, my name is Nelson. Honestly, I don&#039;t know much about Java, but I found out that is not that complicate to create a TextArea&#039;s PopupMenu after all. Check this code and let me know if it helps you.

private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.add(getJPopupMenuEdicion());
			jTextArea.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if (e.getButton()==MouseEvent.BUTTON3){
						//getJPopupMenuEdicion();
						jPopupMenuEdicion.show(jTextArea, e.getX(), e.getY());
						jPopupMenuEdicion.setVisible(true);
					}
				}
			});
   		}
		return jTextArea;
	}

	private JPopupMenu getJPopupMenuEdicion() {
		if (jPopupMenuEdicion == null) {
			jPopupMenuEdicion = new JPopupMenu();
			jPopupMenuEdicion.add(getJPopupMenuItemCopiar());
			jPopupMenuEdicion.add(getJPopupMenuItemCortar());
			jPopupMenuEdicion.add(getJPopupMenuItemPegar());
			
		}
		return jPopupMenuEdicion;
	}

Sorry if it is a little messed up. By the way, I&#039;m using Eclipse, but on this code I had to figure it out by myself.

Let me know your opinion, thanks.
P.D.: I&#039;m from Argentina, so all the unknown words are in Spanish:
Copiar = Copy
Cortar = Cut
Pegar = Paste
Edicion = Edition

NAS.QWERTY@HOTMAIL.COM</description>
		<content:encoded><![CDATA[<p>HI, my name is Nelson. Honestly, I don&#8217;t know much about Java, but I found out that is not that complicate to create a TextArea&#8217;s PopupMenu after all. Check this code and let me know if it helps you.</p>
<p>private JTextArea getJTextArea() {<br />
		if (jTextArea == null) {<br />
			jTextArea = new JTextArea();<br />
			jTextArea.add(getJPopupMenuEdicion());<br />
			jTextArea.addMouseListener(new java.awt.event.MouseAdapter() {<br />
				public void mouseClicked(java.awt.event.MouseEvent e) {<br />
					if (e.getButton()==MouseEvent.BUTTON3){<br />
						//getJPopupMenuEdicion();<br />
						jPopupMenuEdicion.show(jTextArea, e.getX(), e.getY());<br />
						jPopupMenuEdicion.setVisible(true);<br />
					}<br />
				}<br />
			});<br />
   		}<br />
		return jTextArea;<br />
	}</p>
<p>	private JPopupMenu getJPopupMenuEdicion() {<br />
		if (jPopupMenuEdicion == null) {<br />
			jPopupMenuEdicion = new JPopupMenu();<br />
			jPopupMenuEdicion.add(getJPopupMenuItemCopiar());<br />
			jPopupMenuEdicion.add(getJPopupMenuItemCortar());<br />
			jPopupMenuEdicion.add(getJPopupMenuItemPegar());</p>
<p>		}<br />
		return jPopupMenuEdicion;<br />
	}</p>
<p>Sorry if it is a little messed up. By the way, I&#8217;m using Eclipse, but on this code I had to figure it out by myself.</p>
<p>Let me know your opinion, thanks.<br />
P.D.: I&#8217;m from Argentina, so all the unknown words are in Spanish:<br />
Copiar = Copy<br />
Cortar = Cut<br />
Pegar = Paste<br />
Edicion = Edition</p>
<p><a href="mailto:NAS.QWERTY@HOTMAIL.COM">NAS.QWERTY@HOTMAIL.COM</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
