<?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"

			>

	<channel>
		<title>Cycling 74  &#187;  Topic: Creating an Alert Box with Swing/mxj</title>
		<atom:link href="http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/feed" rel="self" type="application/rss+xml" />
		<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/feed</link>
		<description></description>
		<pubDate>Wed, 19 Jun 2013 00:34:23 +0000</pubDate>
		<generator>http://bbpress.org/?v=2.2.4</generator>
		<language></language>

		
														
					
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-24118</guid>
					<title><![CDATA[Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-24118</link>
					<pubDate>Mon, 23 Jan 2006 18:42:48 +0000</pubDate>
					<dc:creator>Ben Nevile</dc:creator>

					<description>
						<![CDATA[
						<p>Hi All,</p>
<p>I&#8217;ve put together a simple mxj class to create an alert box using Swing.  It compiles and runs fine on our Athlon 64 4000+, running XP with MaxMSP 4.5.6, with JVM Version 1.5.0_04, and displays the dialog upon receiving a bang with little or no noticeable lag.  </p>
<p>However, when I run the app on my OS X 10.4.4 machine, a G4 1 gHz, running 4.5.6 with JVM 1.5.0_05, I get the &#8220;spinning wheel of death&#8221; and Max hangs indefinitely and requires me to force quit.  I should clarify that the code compiles fine and gives me no errors on either machine.      </p>
<p>Both the patch and the java code are included in the text below.  I&#8217;ve also attached the .class file as well.  </p>
<p>Does anyone know why this might be?  </p>
<p>Thanks in Advance,<br />
-Henry</p>
<p>< ----save below as SaveChanges_test.pat-----></p>
<p>max v2;<br />
#N vpatcher 100 100 326 424;<br />
#P window setfont &#8220;Sans Serif&#8221; 9.;<br />
#P newex 75 140 59 9109513 fromsymbol;<br />
#P message 115 90 49 9109513 name bob;<br />
#P button 105 255 15 0;<br />
#P button 90 237 15 0;<br />
#P button 75 219 15 0;<br />
#P newex 75 169 57 9109513 select 0 1 2;<br />
#P button 75 69 33 0;<br />
#P newex 75 115 85 9109513 mxj SaveChanges;<br />
#P connect 6 0 0 0;<br />
#P connect 1 0 0 0;<br />
#P connect 0 0 7 0;<br />
#P connect 7 0 2 0;<br />
#P connect 2 0 3 0;<br />
#P connect 2 1 4 0;<br />
#P connect 2 2 5 0;<br />
#P pop;</p>
<p>< ----save below as SaveChanges.java and compile-----></p>
<p>//<br />
//	SaveChanges.java<br />
//<br />
//	01.21.06, Henry Till (henrytill@gmail.com)<br />
//</p>
<p>import com.cycling74.max.*;<br />
import javax.swing.JOptionPane;</p>
<p>public class SaveChanges extends MaxObject<br />
{<br />
	String name = null;</p>
<p>    public SaveChanges(Atom[] atoms) {<br />
		declareInlets(new int[] { 1 });<br />
		declareOutlets(new int[] { 1 });<br />
    }</p>
<p>	public void bang() {</p>
<p>		Object[] options = {&#8220;Don&#8217;t Save&#8221;,<br />
							&#8220;Cancel&#8221;,<br />
							&#8220;Save&#8221;};</p>
<p>		int n = JOptionPane.showOptionDialog(null,	// null to hatch from parent frame<br />
			&#8220;Do you want to save changes to &#8220;<br />
			+ name,<br />
			&#8220;Save Changes?&#8221;,<br />
			JOptionPane.YES_NO_CANCEL_OPTION,<br />
			JOptionPane.QUESTION_MESSAGE,<br />
			null,<br />
			options,<br />
			options[2]);</p>
<p>		if (n == JOptionPane.YES_OPTION) {<br />
			outlet(0, &#8220;2&#8243;);</p>
<p>		} else if (n == JOptionPane.NO_OPTION) {<br />
			outlet(0, &#8220;0&#8243;);<br />
		} else if (n == JOptionPane.CANCEL_OPTION) {<br />
			outlet(0, &#8220;1&#8243;);<br />
		} else {<br />
			post(&#8220;no answer given&#8221;);<br />
		}<br />
	}</p>
<p>	public void inlet(int i) { }</p>
<p>	public void inlet(float f) { }</p>
<p>	public void list(Atom[] list) { }</p>
<p>	public void anything(String s, Atom[] args) {		<br />
		if(s.equals(&#8220;name&#8221;)) <br />
			name = Atom.toOneString(args);<br />
	}</p>
<p>}</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69298</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69298</link>
					<pubDate>Mon, 23 Jan 2006 19:22:45 +0000</pubDate>
					<dc:creator>Ben Nevile</dc:creator>

					<description>
						<![CDATA[
						<p>
You have to call nextWindowIsModal() before displaying the Swing window, that should fix it.<br />
Look at the javadocs for MaxSystem, it&#8217;s somewhere in there.</p>
<p>Roby</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69299</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69299</link>
					<pubDate>Mon, 23 Jan 2006 20:26:57 +0000</pubDate>
					<dc:creator>Ben Nevile</dc:creator>

					<description>
						<![CDATA[
						<p>Hi Roby, Thanks for the reply.  The api doc makes it sound as though  <br />
this should do the trick.</p>
<p>Just to clarify, would it be correct to put this line:</p>
<p>		MaxSystem.nextWindowIsModal();			</p>
<p>right before the following block?</p>
<p>		int n = JOptionPane.showOptionDialog(null,	// null to hatch from  <br />
parent frame<br />
			&#8220;Do you want to save changes to &#8220;<br />
			+ name,<br />
			&#8220;Save Changes?&#8221;,<br />
			JOptionPane.YES_NO_CANCEL_OPTION,<br />
			JOptionPane.QUESTION_MESSAGE,<br />
			null,<br />
			options,<br />
			options[2]);</p>
<p>This compiled okay, but when I went to run it on OS X, it still hung,  <br />
which made me think maybe I am doing this wrong.</p>
<p>-H</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69300</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69300</link>
					<pubDate>Mon, 23 Jan 2006 21:40:14 +0000</pubDate>
					<dc:creator>Ben Nevile</dc:creator>

					<description>
						<![CDATA[
						<p>
> Just to clarify, would it be correct to put this line:<br />
> <br />
> 		MaxSystem.nextWindowIsModal();			<br />
> <br />
> right before the following block?<br />
> </p>
<p>> This compiled okay, but when I went to run it on OS X, it still hung,  <br />
> which made me think maybe I am doing this wrong.<br />
> </p>
<p>What I do is the following:<br />
I declare in my class:<br />
	MaxSystem maxsys = new MaxSystem();	</p>
<p>and later before displaying the window I put:</p>
<p>	maxsys.nextWindowIsModal();</p>
<p>Roby</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69301</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69301</link>
					<pubDate>Mon, 23 Jan 2006 22:56:30 +0000</pubDate>
					<dc:creator>Ben Nevile</dc:creator>

					<description>
						<![CDATA[
						<p>Roby et al,</p>
<p>That&#8217;s clearly the right way to do it &#8211; so I changed my code, and  <br />
it&#8217;s compiling fine, but still hanging when I run it.</p>
<p>Have you or anybody else successfully displayed an Alert Box using  <br />
JOptionPane in mxj on OS X before?</p>
<p>-Henry</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69302</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69302</link>
					<pubDate>Tue, 24 Jan 2006 11:08:22 +0000</pubDate>
					<dc:creator>Ben Nevile</dc:creator>

					<description>
						<![CDATA[
						<p>Henry Till wrote:<br />
> Have you or anybody else successfully displayed an Alert Box using <br />
> JOptionPane in mxj on OS X before?</p>
<p>Have you tried using SwingUtilities.invokeLater() to handle Swing <br />
display stuff asynchronously in Swing&#8217;s own event thread? Thus:</p>
<p>SwingUtilities.invokeLater(new Runnable(){<br />
	public void run(){<br />
		JOptionPane.showConfirmDialog(null,&#8221;Hello&#8221;); //eg<br />
	}<br />
});</p>
<p>
&#8211;<br />
Owen</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69303</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69303</link>
					<pubDate>Wed, 25 Jan 2006 18:24:40 +0000</pubDate>
					<dc:creator>Ben Nevile</dc:creator>

					<description>
						<![CDATA[
						<p>Owen,</p>
<p>Thanks a lot for the tip.  Using this technique did the trick.</p>
<p>-Henry</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69304</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69304</link>
					<pubDate>Wed, 25 Jan 2006 20:56:22 +0000</pubDate>
					<dc:creator>Ben Nevile</dc:creator>

					<description>
						<![CDATA[
						<p>Henry,<br />
Would you be so kind to share your code of the original example<br />
as enhanced with this trick? I am particularly interested in how<br />
you communicate the result of the asynchronously executed OptionPane  <br />
call<br />
back to the mxj thread.<br />
thanks<br />
-jennek</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69305</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69305</link>
					<pubDate>Thu, 26 Jan 2006 12:19:00 +0000</pubDate>
					<dc:creator>Ben Nevile</dc:creator>

					<description>
						<![CDATA[
						<p>Hi Jennek,</p>
<p>In general you don&#8217;t need to &#8211; the rest of the action can be done in the <br />
  Swing thread, or some other event handling thread. In this specific <br />
case there doesn&#8217;t seem to be much choice as it was the very condition <br />
of having the mxj thread wait on the swing thread that seemed to be <br />
causing deadlock.</p>
<p>&#8211; <br />
Owen</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69306</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69306</link>
					<pubDate>Thu, 26 Jan 2006 21:05:14 +0000</pubDate>
					<dc:creator>Jennek Geels</dc:creator>

					<description>
						<![CDATA[
						<p>OK, I was worried that calls to outlet would cause more threading  <br />
problems,<br />
as they run in in a Max thread. But from the &#8216;Writing Max externals  <br />
in Java&#8217;<br />
document from Topher I understand that this is safe to do (v0.3 page 42)<br />
&#8220;It is worth noting that when using Java threads inside of your Max  <br />
external, any outlet<br />
calls you make back into the Max application will automatically be  <br />
deferred for handling<br />
by the low-priority main thread for normal outlet calls and the high- <br />
priority scheduler<br />
thread for outletHigh calls.&#8221;</p>
<p>One thing puzzles me: why is there no hang up on WinXP?<br />
Anyway, I am glad to have learned about invokeLater().</p>
<p>For the sake of archiving, I&#8217;ll post my edition of your code below.<br />
-jennek</p>
<p>//<br />
//	SaveChanges.java<br />
//<br />
//	01.21.06, Henry Till (henrytill@gmail.com)<br />
//</p>
<p>import com.cycling74.max.*;<br />
import javax.swing.*;</p>
<p>public class SaveChanges extends MaxObject {<br />
	String name = null;</p>
<p>	public SaveChanges(Atom[] atoms) {<br />
		declareInlets(new int[] { 1 });<br />
		declareOutlets(new int[] { 1 });<br />
	}</p>
<p>	public void bang() {</p>
<p>		SwingUtilities.invokeLater(new Runnable() {<br />
			public void run() {</p>
<p>				Object[] options = { &#8220;Don&#8217;t Save&#8221;, &#8220;Cancel&#8221;, &#8220;Save&#8221; };</p>
<p>				int n = JOptionPane<br />
						.showOptionDialog(null, // null to hatch from parent<br />
												// frame<br />
								&#8220;Do you want to save changes to &#8221; + name,<br />
								&#8220;Save Changes?&#8221;,<br />
								JOptionPane.YES_NO_CANCEL_OPTION,<br />
								JOptionPane.QUESTION_MESSAGE, null, options,<br />
								options[2]);</p>
<p>				if (n == JOptionPane.YES_OPTION) {<br />
					outlet(0, &#8220;2&#8243;);</p>
<p>				} else if (n == JOptionPane.NO_OPTION) {<br />
					outlet(0, &#8220;0&#8243;);<br />
				} else if (n == JOptionPane.CANCEL_OPTION) {<br />
					outlet(0, &#8220;1&#8243;);<br />
				} else {<br />
					post(&#8220;no answer given&#8221;);<br />
				}</p>
<p>			}<br />
		});</p>
<p>	}</p>
<p>	public void inlet(int i) {<br />
	}</p>
<p>	public void inlet(float f) {<br />
	}</p>
<p>	public void list(Atom[] list) {<br />
	}</p>
<p>	public void anything(String s, Atom[] args) {<br />
		if (s.equals(&#8220;name&#8221;))<br />
			name = Atom.toOneString(args);<br />
	}</p>
<p>}</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69307</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69307</link>
					<pubDate>Thu, 26 Jan 2006 23:13:11 +0000</pubDate>
					<dc:creator>Owen Green</dc:creator>

					<description>
						<![CDATA[
						<p>jennek geels wrote:</p>
<p>> One thing puzzles me: why is there no hang up on WinXP?</p>
<p>It seems to have something to do with Carbon-Cocoa interaction. As far <br />
as I can tell, one has to make sure your main thread and the AWT/Swing <br />
thread are never in contention or a deadlock will occur.</p>
<p>&#8211;<br />
Owen</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69308</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69308</link>
					<pubDate>Sun, 29 Jan 2006 00:45:26 +0000</pubDate>
					<dc:creator>Léopold Frey</dc:creator>

					<description>
						<![CDATA[
						<p></p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69309</guid>
					<title><![CDATA[Re: Creating an Alert Box with Swing/mxj]]></title>
					<link>http://cycling74.com/forums/topic/creating-an-alert-box-with-swingmxj/#post-69309</link>
					<pubDate>Sun, 29 Jan 2006 01:16:43 +0000</pubDate>
					<dc:creator>topher lafata</dc:creator>

					<description>
						<![CDATA[
						<p>Yes. There is indeed much potential for deadlock when using swing on OS X due to the fact that swing does its event handling via cocoa and max/msp has its own event handlin loop which uses carbon. It definitely took a significant amount of vudu to get swing to work at all on os x.</p>
<p>Regardless. Anytime you display a frame or other UI element you should do it from the swing/awt thread. From suns own docs.</p>
<p>
&#8220;To avoid the possibility of thread problems, we recommend that you use invokeLater to create the GUI on the event-dispatching thread for all new applications. If you have old programs that are working fine they are probably OK; however you might want to convert them when it&#8217;s convenient to do so. &#8220;</p>
<p>This is probably particularly important on OS X given the fact that we are dealing with cocoa java embedded in a carbon application. I wish it wasn&#8217;t so but i think we need to live with it for the time being!</p>
						]]>
					</description>

					
					
				</item>

					
		
	</channel>
	</rss>

