<?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>Silveira Neto &#187; Videos</title>
	<atom:link href="http://silveiraneto.net/tag/videos/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description></description>
	<lastBuildDate>Fri, 09 Mar 2012 04:13:27 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>JavaFX, handling events with overlapping elements</title>
		<link>http://silveiraneto.net/2008/07/28/javafx-handling-events-with-overlapping-elements/</link>
		<comments>http://silveiraneto.net/2008/07/28/javafx-handling-events-with-overlapping-elements/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 03:21:46 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[blocksMouse]]></category>
		<category><![CDATA[JavaFX]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[RIA]]></category>
		<category><![CDATA[Videos]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=1000</guid>
		<description><![CDATA[<a href="http://silveiraneto.net/2008/07/28/javafx-handling-events-with-overlapping-elements/" title="JavaFX, handling events with overlapping elements"></a>Here is a problem I faced those days while programming with JavaFX. When you perform a click in a JavaFX area, mouse events are called to all nodes through that position. You can see this behavior in this video. Example &#8230;<p class="read-more"><a href="http://silveiraneto.net/2008/07/28/javafx-handling-events-with-overlapping-elements/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://silveiraneto.net/2008/07/28/javafx-handling-events-with-overlapping-elements/" title="JavaFX, handling events with overlapping elements"></a><p>Here is a problem I faced those days while programming with <a href="https://openjfx.dev.java.net/">JavaFX</a>.<br />
When you perform a click in a JavaFX area, mouse events are called to all nodes through that position. You can see this behavior in this video.</p>
<p><strong>Example 1.</strong></p>
<p><center><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/tsHMNrWzYrs&#038;hl=pt-br&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/tsHMNrWzYrs&#038;hl=pt-br&#038;fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object></center></p>
<p>Here is the code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.application.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.geometry.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.geometry.Rectangle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.paint.Color</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.input.MouseEvent</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">Frame</span> <span style="color: #009900;">&#123;</span>
    width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">200</span>
    height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">200</span>
    visible<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">true</span>
    stage<span style="color: #339933;">:</span> Stage <span style="color: #009900;">&#123;</span>
        content<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
            <span style="color: #003399;">Rectangle</span> <span style="color: #009900;">&#123;</span>
                var color1 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">BLUE</span><span style="color: #339933;">;</span>
                x<span style="color: #339933;">:</span> <span style="color: #cc66cc;">10</span>, y<span style="color: #339933;">:</span> <span style="color: #cc66cc;">10</span>, width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">140</span>, height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">90</span>, fill<span style="color: #339933;">:</span> bind color1
                onMouseClicked<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span> e<span style="color: #339933;">:</span> <span style="color: #003399;">MouseEvent</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>color1<span style="color: #339933;">==</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">BLUE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        color1 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">GREEN</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        color1 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">BLUE</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>,
            Circle <span style="color: #009900;">&#123;</span>
                var color2 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">RED</span>
                centerX<span style="color: #339933;">:</span> <span style="color: #cc66cc;">100</span>, centerY<span style="color: #339933;">:</span> <span style="color: #cc66cc;">100</span>, radius<span style="color: #339933;">:</span> <span style="color: #cc66cc;">40</span>, fill<span style="color: #339933;">:</span> bind color2
                onMouseClicked<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span> e<span style="color: #339933;">:</span> <span style="color: #003399;">MouseEvent</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>color2<span style="color: #339933;">==</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">YELLOW</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        color2 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">RED</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        color2 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">YELLOW</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the default behavior. All node are called with a mouse event. Is a expected and robust behavior but sometimes we just don&#8217;t want that. We want events called to just one node or a set of nodes.</p>
<p><strong>Example 2.</strong></p>
<p><center><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/85ogNUjXYGU&#038;hl=pt-br&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/85ogNUjXYGU&#038;hl=pt-br&#038;fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object></center></p>
<p>Is exactly the same code but with <em>blocksMouse: true</em> in the circle node. When blocksMouse is true the mouse event will not be called to others node behind it.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">overlapping</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.application.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.geometry.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.geometry.Rectangle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.paint.Color</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.input.MouseEvent</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">Frame</span> <span style="color: #009900;">&#123;</span>
    width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">200</span>
    height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">200</span>
    visible<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">true</span>
    stage<span style="color: #339933;">:</span> Stage <span style="color: #009900;">&#123;</span>
        content<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
            <span style="color: #003399;">Rectangle</span> <span style="color: #009900;">&#123;</span>
                var color1 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">BLUE</span><span style="color: #339933;">;</span>
                x<span style="color: #339933;">:</span> <span style="color: #cc66cc;">10</span>, y<span style="color: #339933;">:</span> <span style="color: #cc66cc;">10</span>, width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">140</span>, height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">90</span>, fill<span style="color: #339933;">:</span> bind color1
                onMouseClicked<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span> e<span style="color: #339933;">:</span> <span style="color: #003399;">MouseEvent</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>color1<span style="color: #339933;">==</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">BLUE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        color1 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">GREEN</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        color1 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">BLUE</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>,
            Circle <span style="color: #009900;">&#123;</span>
                var color2 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">RED</span>
                centerX<span style="color: #339933;">:</span> <span style="color: #cc66cc;">100</span>, centerY<span style="color: #339933;">:</span> <span style="color: #cc66cc;">100</span>, radius<span style="color: #339933;">:</span> <span style="color: #cc66cc;">40</span>, fill<span style="color: #339933;">:</span> bind color2
                blocksMouse<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">true</span>
                onMouseClicked<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span> e<span style="color: #339933;">:</span> <span style="color: #003399;">MouseEvent</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>color2<span style="color: #339933;">==</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">YELLOW</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        color2 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">RED</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        color2 <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">YELLOW</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Thanks guys on the <a href="https://openjfx.dev.java.net/">OpenJDK user mail list</a> and at <a href="http://forums.java.net/jive/category.jspa?categoryID=62">OpenJFX Forum</a>, specially <a href="http://forums.java.net/jive/thread.jspa?messageID=289116">this thread</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2008/07/28/javafx-handling-events-with-overlapping-elements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

