<?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>Martin&#039;s Weekend Coding &#187; TextBox</title>
	<atom:link href="http://blog.alutam.com/tag/textbox/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.alutam.com</link>
	<description>Sharing useful tips from my &#34;weekend projects&#34;</description>
	<lastBuildDate>Sat, 31 Oct 2009 13:14:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
   <image>
    <title>Martin&#039;s Weekend Coding</title>
    <url>http://www.gravatar.com/avatar/fe13f36272d010f4b3c09e477a620991?s=</url>
    <link>http://blog.alutam.com</link>
   </image>
		<item>
		<title>JavaFX Password Field</title>
		<link>http://blog.alutam.com/2009/09/12/javafx-password-field/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://blog.alutam.com/2009/09/12/javafx-password-field/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 11:33:02 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[JavaFX]]></category>
		<category><![CDATA[Password]]></category>
		<category><![CDATA[TextBox]]></category>

		<guid isPermaLink="false">http://blog.alutam.com/?p=76</guid>
		<description><![CDATA[As I mentioned in My First Experience with JavaFX blog, there is no password field in JavaFX, so I had to google for some workarounds. Although I found a few, none of them worked flawlessly, so last night I decided to spend some time trying to come up with a password field that would really [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in <a href="http://blog.alutam.com/2009/08/21/my-first-experience-with-javafx/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed">My First Experience with JavaFX</a> blog, there is no password field in JavaFX, so I had to google for some workarounds. Although I found a few, none of them worked flawlessly, so last night I decided to spend some time trying to come up with a password field that would really work as expected. And, I think I managed to come up with an elegant and simple solution. No hacking in form of covering the text area with additional components or adding effects that blur the text box (including the caret and component borders). It looks and behaves exactly as you would expect of a password field. Click on the following picture to try it out:<br />
<script type="text/javascript">
function showApplet() { 
    document.getElementById("Applet").innerHTML = "<iframe alt=PasswordBoxDemo scrolling=no frameborder=0 src='/apps/javafx/PasswordBoxDemo/PasswordBoxDemo.html' height=100 width=300 ></iframe>"; 
}
</script></p>
<div id="Applet" style="border: 1px solid; text-align: left;"><a href="##utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img src="/img/pbdemo.png" onclick='showApplet();'/></a></div>
<p>Or click the following button for standalone mode:<br />
<a href="/apps/javafx/PasswordBoxDemo/PasswordBoxDemo.jnlp#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img src="/img/jws-launch-button.jpg"/></a><br />
And here is how it is implemented:</p>
<div class="codebox">
<pre><span class="Include"><span class="ReservedWord">import</span> javafx.scene.control.TextBox;</span>
<span class="Include"><span class="ReservedWord">import</span> javafx.util.Math;</span>

<span class="DocComment">/**
 * @author Martin Matula
 */</span>
<span class="ReservedWord">public</span> <span class="ReservedWord">class</span> <span class="Identifier">PasswordBox</span> <span class="ReservedWord">extends</span> <span class="Identifier">TextBox</span> <span class="Braces">{</span>
    <span class="ReservedWord">public</span><span class="Symbol">-</span><span class="Identifier">read</span> <span class="ReservedWord">var</span> <span class="Identifier">password</span> <span class="Symbol">=</span> <span class="String">&quot;&quot;</span><span class="Symbol">;</span>

    <span class="ReservedWord">override</span> <span class="ReservedWord">function</span> <span class="Identifier">replaceSelection</span><span class="Braces">(</span><span class="Identifier">arg</span><span class="Braces">)</span> <span class="Braces">{</span>
        <span class="ReservedWord">var</span> <span class="Identifier">pos1</span> <span class="Symbol">=</span> <span class="Identifier">Math</span>.<span class="Identifier">min</span><span class="Braces">(</span><span class="Identifier">dot</span>, <span class="Identifier">mark</span><span class="Braces">)</span><span class="Symbol">;</span>
        <span class="ReservedWord">var</span> <span class="Identifier">pos2</span> <span class="Symbol">=</span> <span class="Identifier">Math</span>.<span class="Identifier">max</span><span class="Braces">(</span><span class="Identifier">dot</span>, <span class="Identifier">mark</span><span class="Braces">)</span><span class="Symbol">;</span>
        <span class="Identifier">password</span> <span class="Symbol">=</span> <span class="String">&quot;{password.substring(0, pos1)}{arg}{password.substring(pos2)}&quot;</span><span class="Symbol">;</span>
        <span class="ReservedWord">super</span>.<span class="Identifier">replaceSelection</span><span class="Braces">(</span><span class="Identifier">getStars</span><span class="Braces">(</span><span class="Identifier">arg</span>.<span class="Identifier">length</span><span class="Braces">(</span><span class="Braces">)</span><span class="Braces">)</span><span class="Braces">)</span><span class="Symbol">;</span>
    <span class="Braces">}</span>

    <span class="ReservedWord">override</span> <span class="ReservedWord">function</span> <span class="Identifier">deleteNextChar</span><span class="Braces">(</span><span class="Braces">)</span> <span class="Braces">{</span>
        <span class="ReservedWord">if</span> <span class="Braces">(</span><span class="Braces">(</span><span class="Identifier">mark</span> <span class="Symbol">=</span><span class="Symbol">=</span> <span class="Identifier">dot</span><span class="Braces">)</span> <span class="ReservedWord">and</span> <span class="Braces">(</span><span class="Identifier">dot</span> <span class="Symbol">&lt;</span> <span class="Identifier">password</span>.<span class="Identifier">length</span><span class="Braces">(</span><span class="Braces">)</span><span class="Braces">)</span><span class="Braces">)</span> <span class="Braces">{</span>
            <span class="Identifier">password</span> <span class="Symbol">=</span> <span class="String">&quot;{password.substring(0, dot)}{password.substring(dot + 1)}&quot;</span><span class="Symbol">;</span>
        <span class="Braces">}</span>
        <span class="ReservedWord">super</span>.<span class="Identifier">deleteNextChar</span><span class="Braces">(</span><span class="Braces">)</span><span class="Symbol">;</span>
    <span class="Braces">}</span>

    <span class="ReservedWord">override</span> <span class="ReservedWord">function</span> <span class="Identifier">deletePreviousChar</span><span class="Braces">(</span><span class="Braces">)</span> <span class="Braces">{</span>
        <span class="ReservedWord">if</span> <span class="Braces">(</span><span class="Braces">(</span><span class="Identifier">mark</span> <span class="Symbol">=</span><span class="Symbol">=</span> <span class="Identifier">dot</span><span class="Braces">)</span> <span class="ReservedWord">and</span> <span class="Braces">(</span><span class="Identifier">dot</span> <span class="Symbol">&gt;</span> <span class="Numeric">0</span><span class="Braces">)</span><span class="Braces">)</span> <span class="Braces">{</span>
            <span class="Identifier">password</span> <span class="Symbol">=</span> <span class="String">&quot;{password.substring(0, dot - 1)}{password.substring(dot)}&quot;</span><span class="Symbol">;</span>
        <span class="Braces">}</span>
        <span class="ReservedWord">super</span>.<span class="Identifier">deletePreviousChar</span><span class="Braces">(</span><span class="Braces">)</span><span class="Symbol">;</span>
    <span class="Braces">}</span>

    <span class="ReservedWord">function</span> <span class="Identifier">getStars</span><span class="Braces">(</span><span class="Identifier">len</span><span class="Symbol">:</span> <span class="Identifier">Integer</span><span class="Braces">)</span><span class="Symbol">:</span> <span class="Identifier">String</span> <span class="Braces">{</span>
        <span class="ReservedWord">var</span> <span class="Identifier">result</span><span class="Symbol">:</span> <span class="Identifier">String</span> <span class="Symbol">=</span> <span class="String">&quot;&quot;</span><span class="Symbol">;</span>
        <span class="ReservedWord">for</span> <span class="Braces">(</span><span class="Identifier">i</span> <span class="ReservedWord">in</span> <span class="Braces">[</span><span class="Numeric">1</span>..<span class="Identifier">len</span><span class="Braces">]</span><span class="Braces">)</span> <span class="Braces">{</span>
            <span class="Identifier">result</span> <span class="Symbol">=</span> <span class="String">&quot;{result}*&quot;</span><span class="Symbol">;</span>
        <span class="Braces">}</span>
        <span class="Identifier">result</span><span class="Symbol">;</span>
    <span class="Braces">}</span>
<span class="Braces">}</span></pre>
</div>
<p>Quite simple, isn&#8217;t it? Here is how it is used in the Main class:</p>
<div class="codebox">
<pre><span class="Include"><span class="ReservedWord">import</span> javafx.stage.Stage;</span>
<span class="Include"><span class="ReservedWord">import</span> javafx.scene.Scene;</span>
<span class="Include"><span class="ReservedWord">import</span> javafx.scene.text.Text;</span>

<span class="ReservedWord">var</span> <span class="Identifier">password</span><span class="Symbol">:</span> <span class="Identifier">PasswordBox</span><span class="Symbol">;</span>

<span class="ReservedWord">var</span> <span class="Identifier">stage</span><span class="Symbol">:</span> <span class="Identifier">Stage</span> <span class="Symbol">=</span> <span class="Identifier">Stage</span> <span class="Braces">{</span>
    <span class="Identifier">title</span><span class="Symbol">:</span> <span class="String">&quot;PasswordBox Demo&quot;</span>
    <span class="Identifier">width</span><span class="Symbol">:</span> <span class="Numeric">200</span>
    <span class="Identifier">height</span><span class="Symbol">:</span> <span class="Numeric">100</span>
    <span class="Identifier">scene</span><span class="Symbol">:</span> <span class="Identifier">Scene</span> <span class="Braces">{</span>
        <span class="Identifier">content</span><span class="Symbol">:</span> <span class="Braces">[</span>
            <span class="Identifier">password</span> <span class="Symbol">=</span> <span class="Identifier">PasswordBox</span> <span class="Braces">{</span>
                <span class="Identifier">translateX</span><span class="Symbol">:</span> <span class="Numeric">10</span>
                <span class="Identifier">translateY</span><span class="Symbol">:</span> <span class="Numeric">10</span>
                <span class="Identifier">columns</span><span class="Symbol">:</span> <span class="Numeric">20</span>
            <span class="Braces">}</span>,
            <span class="Identifier">Text</span> <span class="Braces">{</span>
                <span class="Identifier">x</span><span class="Symbol">:</span> <span class="Numeric">10</span>
                <span class="Identifier">y</span><span class="Symbol">:</span> <span class="Numeric">50</span>
                <span class="Identifier">content</span><span class="Symbol">:</span> <span class="ReservedWord">bind</span> <span class="Identifier">password</span>.<span class="Identifier">password</span>
            <span class="Braces">}</span>
        <span class="Braces">]</span>
    <span class="Braces">}</span>
<span class="Braces">}</span></pre>
</div>
<p>I guess there is still a room for improving the API &#8211; the real password is stored in the <code>password</code> property, while the <code>text</code> property became useless and should never be set by a client. If you want to populate the field with a remembered password, you need to do it by calling <code>replaceSelection("password")</code> on the password field after it&#8217;s initialization (rather than setting the <code>text</code> or the <code>password</code> properties). Anyway, I wanted to keep the code simple so that you can easily see the basic idea behind it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alutam.com/2009/09/12/javafx-password-field/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
