<?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>Java Swing</title>
	<atom:link href="http://www.javaswing.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javaswing.net</link>
	<description>Swing, SWT and Java Desktop related Technologies</description>
	<pubDate>Wed, 30 Jul 2008 14:59:32 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>Never thought Even constants(public static final) could be Bad</title>
		<link>http://www.javaswing.net/never-thought-even-constantspublic-static-final-could-be-bad.html</link>
		<comments>http://www.javaswing.net/never-thought-even-constantspublic-static-final-could-be-bad.html#comments</comments>
		<pubDate>Wed, 30 Jul 2008 14:59:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[articles]]></category>

		<category><![CDATA[other than swing]]></category>

		<category><![CDATA[tipsAndTricks]]></category>

		<category><![CDATA[compiler]]></category>

		<category><![CDATA[constants]]></category>

		<category><![CDATA[final]]></category>

		<category><![CDATA[static]]></category>

		<guid isPermaLink="false">http://www.javaswing.net/?p=46</guid>
		<description><![CDATA[ Don&#8217;t get me wrong. Using Constants are good. But I am just facing a situation where I feel terribly bad on why I did.
 
  Let me explain the background. We make software. As in, Big software. At least So big that we cannot re-deliver the whole code to the client every time [...]]]></description>
			<content:encoded><![CDATA[<p> Don&#8217;t get me wrong. Using Constants are good. But I am just facing a situation where I feel terribly bad on why I did.<br />
 <span id="more-46"></span><br />
  Let me explain the background. We make software. As in, Big software. At least So big that we cannot re-deliver the whole code to the client every time we make a change. So, obviously, we deliver patches that contains only the files that got modified as a part of a bug fix. We also take pride of having a beautiful patching system that when given the names of the class files that changed, will automatically make a patch(zip) out of the class files, download it at the client end, unzip it, replace it at correct locations and restart the customer&#8217;s server.<br />
  This time, the bug I fixed was simple. All I had to do was modify the value of a constant , like for eg., from<br />
	public static final int A_CONST = 10;<br />
    to<br />
	public static final int A_CONST = 20;</p>
<p>  that&#8217;s it. So as usual, I made this change, gave it to the patching system ,which took the new class file made a zip downloaded it at the customer&#8217;s end and replaced the new class file. What do you know, it din&#8217;t work. I should have guessed. But I din&#8217;t.<br />
  The problem was this. In Java when constants are being referred from other files, it doesn&#8217;t read the value from the other variable dynamically, but copies the value within itself during compile time. For eg., when class &#8220;AnotherClass&#8221; access A_CONST, using code, say,<br />
      int b = ConstantClass.A_CONST;<br />
  it&#8217;s replaced to<br />
      int b = 10; within the class file. </p>
<p>  So Iam off, making a patch by recompiling all the other (nearly 100) java files, and making a patch out of all those :(.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javaswing.net/never-thought-even-constantspublic-static-final-could-be-bad.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Demystifying JFormattedTextField: A Step by Step tutorial</title>
		<link>http://www.javaswing.net/demystifying-jformattedtextfield-a-step-by-step-tutorial.html</link>
		<comments>http://www.javaswing.net/demystifying-jformattedtextfield-a-step-by-step-tutorial.html#comments</comments>
		<pubDate>Wed, 30 Jul 2008 09:59:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[components]]></category>

		<category><![CDATA[ready made components]]></category>

		<category><![CDATA[textfield]]></category>

		<category><![CDATA[tipsAndTricks]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[formatted textfield]]></category>

		<category><![CDATA[formatter]]></category>

		<category><![CDATA[PercentageTextField]]></category>

		<category><![CDATA[special textfield]]></category>

		<guid isPermaLink="false">http://www.javaswing.net/?p=45</guid>
		<description><![CDATA[ &#160;&#160;&#160;JFormattedTextField is a very useful Swing component, that let&#8217;s your users see data in a format suitable for them, while letting you read or write from it in a way that that suitable for your code. Let&#8217;s make sure that you clearly understand everything related to it.

 &#160;&#160;&#160;Ok, let&#8217;s start. here&#8217;s the first thing [...]]]></description>
			<content:encoded><![CDATA[<p> &nbsp;&nbsp;&nbsp;JFormattedTextField is a very useful Swing component, that let&#8217;s your users see data in a format suitable for them, while letting you read or write from it in a way that that suitable for your code. Let&#8217;s make sure that you clearly understand everything related to it.<br />
<span id="more-45"></span><br />
 &nbsp;&nbsp;&nbsp;Ok, let&#8217;s start. here&#8217;s the first thing you need to know about the JFormattedTextField:</p>
<h2>What you see is not what&#8217;s inside:</h2>
<p>&nbsp;&nbsp;&nbsp;Consider the TextField component. It has only one value associated with its model. The &#8220;text&#8221;. This is the same value that the user sees, and the value that you set or get using your code. This is OK for simple text fields, but what about cases where it&#8217;s convenient for you to manipulate the data in different way than what you need to show to the user. Not sure when such a case would arise? Consider this. Say you have a percentageTextField, in which the user can type his value in percentage like &#8220;30%&#8221; , &#8220;40%&#8221; etc. It&#8217;s very intuitive and readable for the user to see the string with &#8220;%&#8221; appeneded in the end (like &#8220;30%&#8221; instead of &#8220;30&#8243;), but wouldn&#8217;t you find it more easier to manipulate this value as integers 30 and 40, instead of Strings &#8220;30%&#8221; and &#8220;40%&#8221;?<br />
&nbsp;&nbsp;&nbsp;To solve this problem, JFormattedTextField introduces the notion of &#8220;value&#8221; in addition to the concept of &#8220;text&#8221;. The &#8220;text&#8221; part is what is showed to the user and the &#8220;value&#8221; part is what is easy for the code to manipulate. Got it? If you did, it should be obvious that the &#8220;text&#8221; part can only be a string(as it&#8217;s the one that displayed to the user) and the &#8220;value&#8221; part can be any object<br />
&nbsp;&nbsp;&nbsp;Easy So far? The rest should be even easier. read on.</p>
<h2>Formatter: The one who converts between the &#8220;text&#8221; and &#8220;value&#8221;</h2>
<p>&nbsp;&nbsp;&nbsp;So, as far, you have understood that the JFormattedTextField has both &#8220;text&#8221; and &#8220;value&#8221; associated with it. But there&#8217;s one small catch left yet. When the &#8220;text&#8221; is updated so should the &#8220;value&#8221; and vice verca. For eg., when the user types &#8220;30%&#8221; the value should have an integer value of 30. and then when the user changes the text to &#8220;40%&#8221; the value should get automatically updated to integer 40. Right? And viceverca is true too, when through code you set the value to &#8220;50&#8243; the user should see &#8220;50%&#8221; in the UI.  When does this conversion happen? Before that let&#8217;s see who converts these.<br />
&nbsp;&nbsp;&nbsp;The &#8220;JFormattedTextField&#8221; has an inner class &#8220;AbstractFormatter&#8221;, instances of which are responsible for this conversion. So what methods should this class have for the subclasses to override? Obviously, one method that converts and returns the &#8220;value&#8221; object from the &#8220;text&#8221; string, and another method that converts and returns the &#8220;text&#8221; string from the &#8220;value&#8221; object. So here are the two methods that you need to override when you are making your own Formatter. Ofcourse, I need not tell now, which method should do what.<br />
<br/></p>
<ul>
<li>public String valueToString(Object value) ;</li>
<li>public Object stringToValue(String text) ;</li>
</ul>
<h2>When does Synchronization between text and value happen?</h2>
<p>&nbsp;&nbsp;&nbsp;When the user types some text it should get converted to the appropriate value object and when you modify the value object through code, it should somehow get converted back to the &#8220;text&#8221; string that the user sees. When does this conversion happen? When modifying the value object the conversion to text happens immediately, but when ther user types text, it usually happens in one of the following ways</p>
<ul>
<li>When the user presses enter after typing in some value </li>
<li>When the user goes out of the text field (during focus lost)</li>
</ul>
<p>Note, however this is not always true. The Formatter may decide to do it in other times too, like for example , for each character press by the user. You can also programatically call the commitEdit() method in the JFormattedTextField to convert the text to value at once.</p>
<h2>An Example:</h2>
<p>&nbsp;&nbsp;&nbsp;Let&#8217;s write the percentage text field that we were discussing above. As discussed above it displays values like &#8220;30%&#8221;, &#8220;40%&#8221; etc., while the in the code we use integers like 30, 40 etc. First Let&#8217;s write an AbstractFormatter subclass<br />
&nbsp;&nsbp;&nsbp;Here&#8217;s the class. It implements the methods &#8220;stringToValue&#8221; and &#8220;valueToString&#8221;. The implementation is pretty simple, follow the code comments, if you have any doubts</p>
<h2>Step 1: Create the PertentageFormatter class</h2>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">class</span> PercentageFormatter <span style="color: #000000; font-weight: bold;">extends</span> AbstractFormatter<span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">//Convert the string to Integer	</span>
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">Integer</span> stringToValue<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> text<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #aaaadd; font-weight: bold;">ParseException</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>text == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #aaaadd; font-weight: bold;">Integer</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #808080; font-style: italic;">//If the text ends with &quot;%&quot; remove it</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>text.<span style="color: #006600;">endsWith</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
      text = text.<span style="color: #006600;">substring</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, text.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//If the text is empty, assume it's 0</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #aaaadd; font-weight: bold;">Integer</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">//Try to parse the remaining string, if there's an exception use 0</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #aaaadd; font-weight: bold;">Integer</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
      <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">NumberFormatException</span> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">//Leave it. If there's any exception return 0 below</span>
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #aaaadd; font-weight: bold;">Integer</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">String</span> valueToString<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Object</span> value<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #aaaadd; font-weight: bold;">ParseException</span> <span style="color: #66cc66;">&#123;</span>
     <span style="color: #808080; font-style: italic;">//If the value is an integer, append &quot;%&quot; to it</span>
     <span style="color: #808080; font-style: italic;">// and return the value to display.</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>value == <span style="color: #000000; font-weight: bold;">null</span> || <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> <span style="color: #66cc66;">&#40;</span>value <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #aaaadd; font-weight: bold;">Integer</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #ff0000;">&quot;0%&quot;</span><span style="color: #66cc66;">;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #aaaadd; font-weight: bold;">Integer</span> val = <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Integer</span><span style="color: #66cc66;">&#41;</span>value<span style="color: #66cc66;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> val.<span style="color: #006600;">intValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;%&quot;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Step2: Create the JFormattedTextField using the &#8220;PercentageFormatter&#8221; formatter</h2>
<p>This is really simple. You pass an instance of the &#8220;PercentageFormatter&#8221; sub class to the JFormattedTextField.</p>

<div class="wp_syntax"><div class="code"><pre class="java">JFormattedTextField field = 
  <span style="color: #000000; font-weight: bold;">new</span> JFormattedTextField<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PercentageFormatter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>tha&#8217;s it. Now add this field to your form, type some value like &#8220;40%&#8221; and use the getValue() to get the value as an Integer Object 40.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javaswing.net/demystifying-jformattedtextfield-a-step-by-step-tutorial.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Ready made Simple Custom TextField components</title>
		<link>http://www.javaswing.net/ready-made-simple-custom-textfield-components.html</link>
		<comments>http://www.javaswing.net/ready-made-simple-custom-textfield-components.html#comments</comments>
		<pubDate>Sun, 27 Jul 2008 08:01:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[basic]]></category>

		<category><![CDATA[components]]></category>

		<category><![CDATA[ready made components]]></category>

		<category><![CDATA[textfield]]></category>

		<category><![CDATA[custom components]]></category>

		<category><![CDATA[email]]></category>

		<category><![CDATA[numeric]]></category>

		<category><![CDATA[swing textfields]]></category>

		<category><![CDATA[upper case]]></category>

		<guid isPermaLink="false">http://www.javaswing.net/?p=44</guid>
		<description><![CDATA[&#160;&#160;&#160;Here&#8217;s 3 kinds of text fields that you may want to use in your java swing projects.

An &#8220;Email&#8221; text-field: That doesn&#8217;t let the focus out without the user entering a valid email address
An &#8220;Numeric&#8221; text-field: That accepts only numeric input and ignores all others
An &#8220;upper case&#8221; text-field: That automatically converts the characters to upper case [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;&nbsp;&nbsp;Here&#8217;s 3 kinds of text fields that you may want to use in your java swing projects.</p>
<ul>
<li><a href="http://www.javaswing.net/email-jtextfield-in-swing-ready-made-code.html">An &#8220;Email&#8221; text-field</a>: That doesn&#8217;t let the focus out without the user entering a valid email address</li>
<li><a href="http://www.javaswing.net/numeric-number-only-jtextfield-in-swing-ready-made-code.html">An &#8220;Numeric&#8221; text-field</a>: That accepts only numeric input and ignores all others</li>
<li><a href="http://www.javaswing.net/upper-case-jtextfield-in-swing-ready-made-code.html">An &#8220;upper case&#8221; text-field</a>: That automatically converts the characters to upper case as the user types in</li>
</ul>
<p>&nbsp;&nbsp;&nbsp;Besides using this components in whatever way you like, looking at the code can give you an excellent insight on using <a href="http://www.javaswing.net/swings-inputverifier-tutorial-a-super-elegant-way-for-validation.html">InputVerifier </a>and <a href="http://www.javaswing.net/max-length-on-jtextfield-using-documentfilter-a-step-by-step-tutorial.html">DocumentFilter</a> if you have already not used them</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javaswing.net/ready-made-simple-custom-textfield-components.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Email JTextField in Swing: Ready made code</title>
		<link>http://www.javaswing.net/email-jtextfield-in-swing-ready-made-code.html</link>
		<comments>http://www.javaswing.net/email-jtextfield-in-swing-ready-made-code.html#comments</comments>
		<pubDate>Sun, 27 Jul 2008 02:42:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[ready made components]]></category>

		<category><![CDATA[textfield]]></category>

		<category><![CDATA[email textfield]]></category>

		<category><![CDATA[input verifier]]></category>

		<category><![CDATA[regular expressions]]></category>

		<category><![CDATA[swing components]]></category>

		<guid isPermaLink="false">http://www.javaswing.net/?p=43</guid>
		<description><![CDATA[&#160;&#160;&#160;Here’s a ready made Email text field written as a swing component. This text field allows the user to type any thing in it but does not allow the focus to go out of it unless whatever the user has typed is a valid email address. Note that extensive validation is not done. You can [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;&nbsp;&nbsp;Here’s a ready made Email text field written as a swing component. This text field allows the user to type any thing in it but does not allow the focus to go out of it unless whatever the user has typed is a valid email address. Note that extensive validation is not done. You can modify the code as you like. There are no restrictions. The code uses an input verifier to achieve this.<br />
<span id="more-43"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">package</span> kaushik.<span style="color: #006600;">test</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #a1a100;">import javax.swing.InputVerifier;</span>
<span style="color: #a1a100;">import javax.swing.JComponent;</span>
<span style="color: #a1a100;">import javax.swing.JOptionPane;</span>
<span style="color: #a1a100;">import javax.swing.JTextField;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EmailTextField <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #aaaadd; font-weight: bold;">JTextField</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> EmailTextField<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> columns<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span>columns<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    addVerifier<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> addVerifier<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">setInputVerifier</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> EmailVerifier<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">class</span> EmailVerifier <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #aaaadd; font-weight: bold;">InputVerifier</span><span style="color: #66cc66;">&#123;</span>    
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> verify<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">JComponent</span> input<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">String</span> regx = <span style="color: #ff0000;">&quot;[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+<span style="color: #000099; font-weight: bold;">\\</span>.[A-Za-z]{2,4}&quot;</span><span style="color: #66cc66;">;</span>
      <span style="color: #aaaadd; font-weight: bold;">String</span> text = EmailTextField.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">getText</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>text.<span style="color: #006600;">matches</span><span style="color: #66cc66;">&#40;</span>regx<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>       
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">;</span>
      <span style="color: #66cc66;">&#125;</span>
      <span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #aaaadd; font-weight: bold;">JOptionPane</span>.<span style="color: #006600;">showMessageDialog</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span>, 
            <span style="color: #ff0000;">&quot;Invalid email format&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.javaswing.net/email-jtextfield-in-swing-ready-made-code.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Numeric (Number only) JTextField in Swing: Ready made code</title>
		<link>http://www.javaswing.net/numeric-number-only-jtextfield-in-swing-ready-made-code.html</link>
		<comments>http://www.javaswing.net/numeric-number-only-jtextfield-in-swing-ready-made-code.html#comments</comments>
		<pubDate>Sun, 27 Jul 2008 02:32:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[ready made components]]></category>

		<category><![CDATA[textfield]]></category>

		<category><![CDATA[documentFilter]]></category>

		<category><![CDATA[numeric text field]]></category>

		<category><![CDATA[ready made component]]></category>

		<category><![CDATA[text field]]></category>

		<guid isPermaLink="false">http://www.javaswing.net/?p=41</guid>
		<description><![CDATA[  Here’s a ready made Numeric(Numbers only) text field written as a swing component. This text field allows only numeric data to be entered in it. When the user enters anything that non numeric in the text field, the system will beep. It&#8217;s implemented using a DocumentFilter that filters out all non-numeric characters from [...]]]></description>
			<content:encoded><![CDATA[<p>  Here’s a ready made Numeric(Numbers only) text field written as a swing component. This text field allows only numeric data to be entered in it. When the user enters anything that non numeric in the text field, the system will beep. It&#8217;s implemented using a DocumentFilter that filters out all non-numeric characters from being entered in the Swing textfield&#8217;s document<br />
<span id="more-41"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">package</span> kaushik.<span style="color: #006600;">test</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #a1a100;">import java.awt.Toolkit;</span>
&nbsp;
<span style="color: #a1a100;">import javax.swing.JTextField;</span>
<span style="color: #a1a100;">import javax.swing.text.AbstractDocument;</span>
<span style="color: #a1a100;">import javax.swing.text.AttributeSet;</span>
<span style="color: #a1a100;">import javax.swing.text.BadLocationException;</span>
<span style="color: #a1a100;">import javax.swing.text.DocumentFilter;</span>
<span style="color: #a1a100;">import javax.swing.text.DocumentFilter.FilterBypass;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NumericTextField <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #aaaadd; font-weight: bold;">JTextField</span> <span style="color: #66cc66;">&#123;</span>	 
  <span style="color: #808080; font-style: italic;">//Add other constructors as required. If you do,</span>
  <span style="color: #808080; font-style: italic;">//be sure to call the &quot;addFilter&quot; method</span>
  <span style="color: #000000; font-weight: bold;">public</span> NumericTextField<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> text, <span style="color: #993333;">int</span> columns<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span>text, columns<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    addFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">//Add an instance of NumericDocumentFilter as a </span>
  <span style="color: #808080; font-style: italic;">//document filter to the current text field</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> addFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">AbstractDocument</span><span style="color: #66cc66;">&#41;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">getDocument</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
       <span style="color: #006600;">setDocumentFilter</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> NumericDocumentFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">class</span> NumericDocumentFilter <span style="color: #000000; font-weight: bold;">extends</span> DocumentFilter<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> insertString<span style="color: #66cc66;">&#40;</span>FilterBypass fb, 
      <span style="color: #993333;">int</span> offset, <span style="color: #aaaadd; font-weight: bold;">String</span> string, <span style="color: #aaaadd; font-weight: bold;">AttributeSet</span> attr<span style="color: #66cc66;">&#41;</span> 
        <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #aaaadd; font-weight: bold;">BadLocationException</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
      <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>string == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #66cc66;">;</span>
      <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>isStringNumeric<span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">insertString</span><span style="color: #66cc66;">&#40;</span>fb, offset, string, attr<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
      <span style="color: #66cc66;">&#125;</span>
      <span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #aaaadd; font-weight: bold;">Toolkit</span>.<span style="color: #006600;">getDefaultToolkit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">beep</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> replace<span style="color: #66cc66;">&#40;</span>FilterBypass fb, <span style="color: #993333;">int</span> offset, 
      <span style="color: #993333;">int</span> length, <span style="color: #aaaadd; font-weight: bold;">String</span> text, <span style="color: #aaaadd; font-weight: bold;">AttributeSet</span> attrs<span style="color: #66cc66;">&#41;</span> 
        <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #aaaadd; font-weight: bold;">BadLocationException</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>text == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #66cc66;">;</span>
      <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>isStringNumeric<span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span>fb, offset, length, text, attrs<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
      <span style="color: #66cc66;">&#125;</span>
      <span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #aaaadd; font-weight: bold;">Toolkit</span>.<span style="color: #006600;">getDefaultToolkit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">beep</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">boolean</span> isStringNumeric<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> string<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
     <span style="color: #993333;">char</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> characters = string.<span style="color: #006600;">toCharArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
     <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">char</span> c: characters<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
       <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> <span style="color: #aaaadd; font-weight: bold;">Character</span>.<span style="color: #006600;">isDigit</span><span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>					 
         <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">;</span>
       <span style="color: #66cc66;">&#125;</span>
     <span style="color: #66cc66;">&#125;</span>
     <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.javaswing.net/numeric-number-only-jtextfield-in-swing-ready-made-code.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Upper Case JTextField in Swing: Ready made code</title>
		<link>http://www.javaswing.net/upper-case-jtextfield-in-swing-ready-made-code.html</link>
		<comments>http://www.javaswing.net/upper-case-jtextfield-in-swing-ready-made-code.html#comments</comments>
		<pubDate>Sun, 27 Jul 2008 02:23:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[components]]></category>

		<category><![CDATA[ready made components]]></category>

		<category><![CDATA[textfield]]></category>

		<category><![CDATA[document]]></category>

		<category><![CDATA[document filter]]></category>

		<category><![CDATA[swing component]]></category>

		<category><![CDATA[text field]]></category>

		<category><![CDATA[upper case]]></category>

		<guid isPermaLink="false">http://www.javaswing.net/?p=40</guid>
		<description><![CDATA[&#160;&#160;&#160; Here&#8217;s a ready made Upper case text field written as a swing component. It converts all characters entered in it automatically to upper case before inserting into it&#8217;s own document. So the when the user types in this text field he will see only characters in upper case irrespective of the case with which [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;&nbsp;&nbsp; Here&#8217;s a ready made Upper case text field written as a swing component. It converts all characters entered in it automatically to upper case before inserting into it&#8217;s own document. So the when the user types in this text field he will see only characters in upper case irrespective of the case with which he types. There is no limitation to use this code. Use it in whatever way you like.<br />
<span id="more-40"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">package</span> kaushik.<span style="color: #006600;">test</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #a1a100;">import java.awt.Toolkit;</span>
&nbsp;
<span style="color: #a1a100;">import javax.swing.JTextField;</span>
<span style="color: #a1a100;">import javax.swing.text.AbstractDocument;</span>
<span style="color: #a1a100;">import javax.swing.text.AttributeSet;</span>
<span style="color: #a1a100;">import javax.swing.text.BadLocationException;</span>
<span style="color: #a1a100;">import javax.swing.text.DocumentFilter;</span>
<span style="color: #a1a100;">import javax.swing.text.DocumentFilter.FilterBypass;</span>
&nbsp;
<span style="color: #a1a100;">import kaushik.test.NumericTextField.NumericDocumentFilter;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UpperCaseTextField <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #aaaadd; font-weight: bold;">JTextField</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Add other constructors as required. If you do,</span>
	<span style="color: #808080; font-style: italic;">//be sure to call the &quot;addFilter&quot; method</span>
	<span style="color: #000000; font-weight: bold;">public</span> UpperCaseTextField<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> text, <span style="color: #993333;">int</span> columns<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span>text, columns<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		addFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Add an instance of NumericDocumentFilter as a document filter</span>
	<span style="color: #808080; font-style: italic;">//to the current text field</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> addFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">AbstractDocument</span><span style="color: #66cc66;">&#41;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">getDocument</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
	  	  <span style="color: #006600;">setDocumentFilter</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UppercaseDocumentFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #808080; font-style: italic;">//This DocumentFilter will modify all characters entered</span>
	<span style="color: #808080; font-style: italic;">//in the text field to upper case and then submit it to the</span>
	<span style="color: #808080; font-style: italic;">//document</span>
	<span style="color: #000000; font-weight: bold;">class</span> UppercaseDocumentFilter <span style="color: #000000; font-weight: bold;">extends</span> DocumentFilter<span style="color: #66cc66;">&#123;</span>		 
	 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> insertString<span style="color: #66cc66;">&#40;</span>FilterBypass fb, <span style="color: #993333;">int</span> offset, 
			<span style="color: #aaaadd; font-weight: bold;">String</span> string, <span style="color: #aaaadd; font-weight: bold;">AttributeSet</span> attr<span style="color: #66cc66;">&#41;</span> 
			<span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #aaaadd; font-weight: bold;">BadLocationException</span> <span style="color: #66cc66;">&#123;</span>
		 <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>string == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #66cc66;">;</span>
		 string = string.<span style="color: #006600;">toUpperCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		 <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">insertString</span><span style="color: #66cc66;">&#40;</span>fb, offset, string, attr<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
	 <span style="color: #66cc66;">&#125;</span>
&nbsp;
	 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> replace<span style="color: #66cc66;">&#40;</span>FilterBypass fb, <span style="color: #993333;">int</span> offset, 
			<span style="color: #993333;">int</span> length, <span style="color: #aaaadd; font-weight: bold;">String</span> text, <span style="color: #aaaadd; font-weight: bold;">AttributeSet</span> attrs<span style="color: #66cc66;">&#41;</span> 
				<span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #aaaadd; font-weight: bold;">BadLocationException</span> <span style="color: #66cc66;">&#123;</span>
		 <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>text == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #66cc66;">;</span>
		 text = text.<span style="color: #006600;">toUpperCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		 <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span>fb, offset, length, text, attrs<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
	 <span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.javaswing.net/upper-case-jtextfield-in-swing-ready-made-code.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Max Length on JTextField Using DocumentFilter: A Step by Step tutorial</title>
		<link>http://www.javaswing.net/max-length-on-jtextfield-using-documentfilter-a-step-by-step-tutorial.html</link>
		<comments>http://www.javaswing.net/max-length-on-jtextfield-using-documentfilter-a-step-by-step-tutorial.html#comments</comments>
		<pubDate>Wed, 23 Jul 2008 18:30:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[documentFilter]]></category>

		<category><![CDATA[utility classes]]></category>

		<category><![CDATA[document filter]]></category>

		<category><![CDATA[swing]]></category>

		<category><![CDATA[textfield]]></category>

		<guid isPermaLink="false">http://www.javaswing.net/?p=38</guid>
		<description><![CDATA[&#160;&#160;Have you ever tried to make a textfield that accepts only 20 characters? Or a text area that automatically converts the user typed letters to upper case? Swing&#8217;s Document Listener is the most accurate and hassle-free way to achieve this in Swing. This articles explains you what you need to get started with Document Filter. [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;&nbsp;Have you ever tried to make a textfield that accepts only 20 characters? Or a text area that automatically converts the user typed letters to upper case? Swing&#8217;s Document Listener is the most accurate and hassle-free way to achieve this in Swing. This articles explains you what you need to get started with Document Filter. Step by Step.<br />
<span id="more-38"></span></p>
<h2>Let&#8217;s Make a MaxTextField</h2>
<p>   Let&#8217;s start by defining the problem. We are trying to add a TextField that will accept only 20 characters. After typing 20 characters the user cannot enter any more characters either by keypress or copy-paste. The user can however, delete existing characters and type new ones as long as the total number of characters is less than or equal to 20.</p>
<h2>The Dirty way: Using KeyEvent.consume()</h2>
<p>One&#8217;s first reaction would be to add a key listener to the textfield and calling the consume() method in a listener method like keyTyped() when the no. of characters exceeds 20.<br />
<br />
Here&#8217;s the sample code that one would be templted to right<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #808080; font-style: italic;">//Assume that textField is an instance of JTextField</span>
textField.<span style="color: #006600;">addKeyListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">KeyAdapter</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> keyTyped<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">KeyEvent</span> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>textField.<span style="color: #006600;">getText</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">//Consume if the user has typed more than 20 charactes</span>
  e.<span style="color: #006600;">consume</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>   This would seem to work fine. But there&#8217;s a catch. after typing 20 characters, the user can still insert more characters in the textfield by copy-pasting the text!!! why does this not work? Because, the paste happens at keyPress not keyTyped.<br />
   So, what&#8217;s the solution? We could add code in the keyPressed() that could handle ctrl+v. But what about shift+ins? What about right click and paste?<br />
Started Sounding messy right? But the most itchy part of using this technique is this. When multiple keylisteners are attached to a textfield, there is no guarantee that which one will be called first. so, calling consume() on one listener does not prevent other listener from seeing this event! So when one listener is preventing the data from being entered, the other listener might happily be checking the spelling and underline the word in red(for a wrong spelling) even thought the user sees no character typed!!!</p>
<h2>Enter DocumentFilter</h2>
<p>   What follows from the above discussion is this<br />
<b><u><br />
&#8220;We need a technique that prevents more than 20 characters from being entered in the textfield before it reaches the textfield, not a technique that removes the character after it&#8217;s already there&#8221;<br />
</b></u></p>
<p>   The solution is to use a document filter. What&#8217;s a document filter? Before that, What&#8217;s a document in the first place? A document is something like a container that sort-of stores the text entered in any text component in Swing. Each text component in swing has it&#8217;s own Document. It&#8217;s kind of a model for the swing-text-based-ui components. Got it?<br />
Ok, so now what&#8217;s a DocumentFilter? Simple. A document filter is a special class that can be attached to documents to filter out the data before it goes into the document.</p>
<h2>How to implement a DocumentFilter:</h2>
<p>You simply follow the following steps</p>
<ul>
<li>Write a class extending the DocumentFilter class</li>
<li> override one or many of insertString(), remove() or replace() methods, depending on whether you are filtering string insertion, string removal or string-replacement operations.</li>
<li>you grab the document of the textfield and attach an instance of this document filter to that document</li>
</ul>
<p>Let&#8217;s start. Here&#8217;s the DocumentFilter class for our problem. Clearly we need to override the insertString() and replace() methods so that user may not enter more than 20 characters why inserting or replacing strings. Here&#8217;s the code. See comments for more details</p>
<p>
Step 1) Writing the class:</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">class</span> MyDocumentFilter <span style="color: #000000; font-weight: bold;">extends</span> DocumentFilter<span style="color: #66cc66;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> replace<span style="color: #66cc66;">&#40;</span>FilterBypass fb, <span style="color: #993333;">int</span> offset, <span style="color: #993333;">int</span> length,
      <span style="color: #aaaadd; font-weight: bold;">String</span> text, <span style="color: #aaaadd; font-weight: bold;">AttributeSet</span> attrs<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #aaaadd; font-weight: bold;">BadLocationException</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">//The current length minus the no.of characters deleted plus the</span>
 <span style="color: #808080; font-style: italic;">// number of characters inserted is the final length in the textfield</span>
 <span style="color: #808080; font-style: italic;">//and should not be more than 20</span>
 <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>fb.<span style="color: #006600;">getDocument</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getLength</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + text.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - length<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span>fb, offset,length, text, attrs<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
 <span style="color: #66cc66;">&#125;</span>
 <span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
 	<span style="color: #808080; font-style: italic;">//Let's beep if the user tries to enter more characters</span>
 	<span style="color: #aaaadd; font-weight: bold;">Toolkit</span>.<span style="color: #006600;">getDefaultToolkit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">beep</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> insertString<span style="color: #66cc66;">&#40;</span>FilterBypass fb, <span style="color: #993333;">int</span> offset,
       <span style="color: #aaaadd; font-weight: bold;">String</span> string, <span style="color: #aaaadd; font-weight: bold;">AttributeSet</span> attr<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #aaaadd; font-weight: bold;">BadLocationException</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">//While inserting the current length plus the number of characters inserted</span>
  <span style="color: #808080; font-style: italic;">//is the final length in the textfield and should not be more than 20</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>fb.<span style="color: #006600;">getDocument</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getLength</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + string.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">insertString</span><span style="color: #66cc66;">&#40;</span>fb, offset,string, attr<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">//Let's beep if the user tries to enter more characters</span>
   <span style="color: #aaaadd; font-weight: bold;">Toolkit</span>.<span style="color: #006600;">getDefaultToolkit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">beep</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Step 2) Attach an instance of this class to the textfields document.</p>
<p>This is a simple and one line code as below</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">AbstractDocument</span><span style="color: #66cc66;">&#41;</span>textField.<span style="color: #006600;">getDocument</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
.<span style="color: #006600;">setDocumentFilter</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MyDocumentFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>A small point to note is that we need to cast the return type of getDocument() to AbstractDocument, only which has the setDocumentFilter method.</p>
<p>That&#8217;s it! Hope you found this article interesting.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javaswing.net/max-length-on-jtextfield-using-documentfilter-a-step-by-step-tutorial.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Swing&#8217;s InputVerifier tutorial: A super elegant way for validation</title>
		<link>http://www.javaswing.net/swings-inputverifier-tutorial-a-super-elegant-way-for-validation.html</link>
		<comments>http://www.javaswing.net/swings-inputverifier-tutorial-a-super-elegant-way-for-validation.html#comments</comments>
		<pubDate>Fri, 04 Jul 2008 05:07:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[articles]]></category>

		<category><![CDATA[input verifier]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[utility classes]]></category>

		<category><![CDATA[input]]></category>

		<category><![CDATA[inputverifier]]></category>

		<category><![CDATA[papaya]]></category>

		<category><![CDATA[textfield focus]]></category>

		<category><![CDATA[validation]]></category>

		<category><![CDATA[verify]]></category>

		<guid isPermaLink="false">http://www.javaswing.net/?p=35</guid>
		<description><![CDATA[    Let&#8217;s consider a common coding situation. Suppose you have a text field in your form that accepts an email address. Suppose you need to code such that if the email address typed by the user is not valid, then you shouldn&#8217;t let the focus out of this textfield. Where would you [...]]]></description>
			<content:encoded><![CDATA[<p>    Let&#8217;s consider a common coding situation. Suppose you have a text field in your form that accepts an email address. Suppose you need to code such that if the email address typed by the user is not valid, then you shouldn&#8217;t let the focus out of this textfield. Where would you put such a code? In the focus lost of the email-text-field? In the focus gained for all other text-fields? I don&#8217;t know what&#8217;s the best way to do this in JavaScript but there&#8217;s a super-elegant way in Swing!<br />
<span id="more-35"></span></p>
<h2>The need for InputVerifier</h2>
<p>&nbsp;&nbsp;To understand that let&#8217;s see the problems with the other common approach to solve this problem, using the focusLost() of the textfield</p>
<h2>The problem with Focus-Snatching:</h2>
<p>&nbsp;&nbsp;One of the common approaches people take to solve such validation issue is to put the code in the focuslost() of a FocusListener attached to the textfield, and if the validation fails the requesting the focus back to the source component. This approach has several problems, but the main is this:<br />
<b>&nbsp;&nbsp;&#8221;The focus lost is called after the focus is moved to the new component. So if the new component itself has some other validation as a part of it&#8217;s focus lost, snatching the focus back to the first component, will trigger the focus lost of the new component too!, this might cause a sort of UI-lock when two components are trying to pull the focus to each other when both of them have invalid data&#8221;</b><br />
 din&#8217;t get the prose? Here&#8217;s the step by step description of the problem</p>
<p><br/></p>
<h2>When two things are wrong, does it matter which was wrong first?</h2>
<ul>
<li> Assume there are two fields &#8220;email1&#8243; and &#8220;email2&#8243;. both of them cannot accept empty values </li>
<li> The user places the focus on field &#8220;email1&#8243;. No bombs here.</li>
<li> He dosen&#8217;t type anything and moves the foucs to field &#8220;email2&#8243;.</li>
<li> Now focus is given to field &#8220;email2&#8243; and the focusLost() for &#8220;email1&#8243; is called </li>
<li> The focusLost() method of &#8220;email1&#8243; validates &#8220;email1&#8243; and checks that it cannot be empty so it prompts a messsage to the user saying &#8220;email1 cannot be empty&#8221; and requests the focus back to field &#8220;email1&#8243;. </li>
<li> But by now, &#8220;email2&#8243; already has the focus and when the focus is moved back to &#8220;email1&#8243; the focus lost of &#8220;email2&#8243; is called which again fires a validation </li>
<li> The user gets a message &#8220;email2 cannot be empty&#8221; and code requests the focus back to &#8220;email2&#8243;</li>
<li> Now, once again the focusLost() of email1 is called and this continues on and on.</li>
</ul>
<p>&nbsp;&nbsp;while there are ways to code to stop this from occuring, including putting this code in the focusgained(),  of other components,and having flags to prevent this, why not use a better way provided in swing! </p>
<p></p>
<h2>Enter InputVerifier:</h2>
<p>&nbsp;&nbsp;It&#8217;s great if you could fight and claim back the focus that belongs to you after letting it go to someone else, but wouldn&#8217;t it be better if you did not let it go in the first place?<br />
&nbsp;&nbsp;Coming back to swing, Wouldn&#8217;t it be cool, if I can have a method called whenever the user tries to move the focus out of my component and before the focus has actually entered the new component. Well, that&#8217;s what InputVerifier is for. To give a chance to the code to determine if the focus can move out of the current component to which it is attached to.<br />
<br />
&nbsp;&nbsp;The InputVerifier class has one main method &#8220;verify&#8221;, that get called before the focus is moved out of any component that it&#8217;s attached to. Let&#8217;s make it a little more clearer, here are the steps of invocation<br />
</p>
<ul>
<li> The user enter&#8217;s a field(the focus is moved to that field)</li>
<li> The user does something and tries to moves the focus out of that field(either through mouse or keyboard)</li>
<li>before User&#8217;s wish is granted and the focus is moved to the new field, the &#8220;verify&#8221; method in your InputVerifier gets called.</li>
<li> Your InputVerifier&#8217;s verify() method returns &#8220;true&#8221; or &#8220;false&#8221; depending on whether the validation succedes or fails.</li>
<li> If the verify() methods returns true the user&#8217;s wish is granted and the foucs is moved to the new textfield that the user has requested focus to.</li>
<li>If the verify() method returns false, the user&#8217;s wish is not granted and the focus remains in the current field.</li>
</ul>
<p></p>
<h2>Please ..Anything but papayas</h2>
<p>&nbsp;&nbsp;Ever tasted a papaya? Some poeple like it. Some of my friends love it. It&#8217;s the fruit that I hate most! It&#8217;s so&#8230; soo.. yucky! So, as a dedication to my hatred, Let&#8217;s make a textfield accept anything, but the word &#8220;papaya&#8221;</p>
<p><br/></p>
<h2>The AnitPapayaVerifier:</h2>
<p>&nbsp;&nbsp; Here&#8217;s the code for the AnitPapayaVerifier class. See comments in the code for understanding<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #808080; font-style: italic;">//class that starts the the anti-papaya movement</span>
<span style="color: #000000; font-weight: bold;">class</span> AntiPapayaVerifier <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #aaaadd; font-weight: bold;">InputVerifier</span> <span style="color: #66cc66;">&#123;</span> 
 <span style="color: #808080; font-style: italic;">//Override the verify method to write the logic and then</span>
 <span style="color: #808080; font-style: italic;">//return true or false</span>
 @Override
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> verify<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">JComponent</span> input<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Get the text in the text field</span>
	<span style="color: #aaaadd; font-weight: bold;">JTextField</span> couldBeEvilField =  <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">JTextField</span><span style="color: #66cc66;">&#41;</span>input<span style="color: #66cc66;">;</span>
	<span style="color: #aaaadd; font-weight: bold;">String</span> text = couldBeEvilField.<span style="color: #006600;">getText</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Check if the user has typed the p-word</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>text.<span style="color: #006600;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;papaya&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">!</span>= <span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//show some arrogant message, and </span>
		<span style="color: #808080; font-style: italic;">//then return false</span>
		<span style="color: #808080; font-style: italic;">//Bad guyz eat papya</span>
		<span style="color: #aaaadd; font-weight: bold;">JOptionPane</span>.<span style="color: #006600;">showMessageDialog</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #ff0000;">&quot;my JForm &quot;</span> +
				<span style="color: #ff0000;">&quot;dosen't grow papayas&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//returning false ensures the focus, is not</span>
		<span style="color: #808080; font-style: italic;">//moved out of the attached textfield</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Nice guyz don't eat papaya</span>
		<span style="color: #808080; font-style: italic;">//return true to ensure that the </span>
		<span style="color: #808080; font-style: italic;">//the focus is moved to where the </span>
		<span style="color: #808080; font-style: italic;">//user wishes to</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>	
 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>
Now all we need to do is attach it our textfield<br />
</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #aaaadd; font-weight: bold;">JTextField</span> noPapayaField = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">JTextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
noPapayaField.<span style="color: #006600;">setInputVerifier</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> AntiPapayaVerifier<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>
&nbsp;&nbsp;That&#8217;s it. Now your evil user dosen&#8217;t stand a chance against typing papaya in your text field and getting away with that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javaswing.net/swings-inputverifier-tutorial-a-super-elegant-way-for-validation.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Setting the Default Button in Swing</title>
		<link>http://www.javaswing.net/setting-the-default-button-in-swing.html</link>
		<comments>http://www.javaswing.net/setting-the-default-button-in-swing.html#comments</comments>
		<pubDate>Thu, 03 Jul 2008 19:13:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[basic]]></category>

		<category><![CDATA[buttons]]></category>

		<category><![CDATA[starting Swing]]></category>

		<category><![CDATA[default button]]></category>

		<category><![CDATA[focus]]></category>

		<category><![CDATA[jbutton]]></category>

		<guid isPermaLink="false">http://www.javaswing.net/?p=34</guid>
		<description><![CDATA[  Consider you have a form with several text fields and some buttons. One of the buttons is usually the &#8220;default&#8221; button , which would get executed if the user pressed enter, while the focus is in any of the text fields in that form. You should have definitely seen this type of buttons. [...]]]></description>
			<content:encoded><![CDATA[<p>  Consider you have a form with several text fields and some buttons. One of the buttons is usually the &#8220;default&#8221; button , which would get executed if the user pressed enter, while the focus is in any of the text fields in that form. You should have definitely seen this type of buttons. As an example  in google.com, you can type you query in the textfield and press enter, which is same as clicking the &#8220;Google Search&#8221; button.<br />
&nbsp;&nbsp;While most people who implement this, add a keylistener to each textfield and execute the doClick() method in JButton when the Enter key is pressed, there is an easier way.  Also by implementing this by setting up the listener, you will not be able to tell swing to let it do OS level special effects like the &#8220;pulsing&#8221; effect in a MAC  or a border-effect on windows.<br />
<span id="more-34"></span><br />
&nbsp;&nbsp;So what to do?simple. The RootPane has a method setDefaultButton() to which you can pass the default button as the argument and which will be considered the default button.</p>
<p>   So here&#8217;s the code to set the default button &#8220;button1&#8243; for a  frame &#8220;frame1&#8243;</p>

<div class="wp_syntax"><div class="code"><pre class="java">frame1.<span style="color: #006600;">getRootPane</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setDefaultButton</span><span style="color: #66cc66;">&#40;</span>button1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>&nbsp;A corresponding method &#8220;isDefaultButton() &#8221; is present in the JButton class to check if this is the default button.</p>
<p> easy?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javaswing.net/setting-the-default-button-in-swing.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Using SwingWorker - A Step by Step tutorial</title>
		<link>http://www.javaswing.net/using-swingworker-a-step-by-step-tutorial.html</link>
		<comments>http://www.javaswing.net/using-swingworker-a-step-by-step-tutorial.html#comments</comments>
		<pubDate>Fri, 27 Jun 2008 15:07:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Event Dispatch Thread]]></category>

		<category><![CDATA[tipsAndTricks]]></category>

		<category><![CDATA[background task]]></category>

		<category><![CDATA[background thread]]></category>

		<category><![CDATA[swing ui frezze]]></category>

		<category><![CDATA[SwingWorker]]></category>

		<guid isPermaLink="false">http://www.javaswing.net/?p=33</guid>
		<description><![CDATA[SwingWorker is one of the most handiest utility classes provided in Swing. Most of the interviews on Swing have a question or two on this class. Let&#8217;s make sure you know all about it&#8217;s need and usage. Step by Step.

A (Quick) look at the problem with the &#8220;Event Dispatch&#8221; thread:

&#160;&#160;&#160; The &#8220;Event Dispatch&#8221; Thread(EDT) is [...]]]></description>
			<content:encoded><![CDATA[<p>SwingWorker is one of the most handiest utility classes provided in Swing. Most of the interviews on Swing have a question or two on this class. Let&#8217;s make sure you know all about it&#8217;s need and usage. Step by Step.<br />
<span id="more-33"></span></p>
<h2>A (Quick) look at the problem with the &#8220;Event Dispatch&#8221; thread:</h2>
<p>
&nbsp;&nbsp;&nbsp; The &#8220;Event Dispatch&#8221; Thread(EDT) is a special thread in Swing where all graphical events are processed. One such event is painting the screen. Also, all the listeners attached to swing components are executed in the EDT(BTW, make it an habit to call it the &#8220;EDT&#8221;, it looks cool on interviews). <br />
&nbsp;&nbsp;&nbsp;So, get this clear, the code that paints the screen and your code that executes inside a listener, both runs on the EDT. Now, by the laws of nature, within a &#8220;single&#8221; thread operations cannot execute parallely. So when your listener code is executing, the code that paints the screen cannot execute. clear? If not read the above line again. <br />
&nbsp;&nbsp;&nbsp;Now, If your listener code takes a long time to execute(for eg., fetching large data from the network), till it&#8217;s completion the screen wouldn&#8217;t get painted. This makes the application look as if it&#8217;s hung.  (You should have definitely seen this in almost all Java IDES.<br />
 If you&#8217;ve not paid attention, Wait for the time when you JAVA IDE seems to be hung,<br />
then change the window and then come back to the IDE window. you won&#8217;t see anything on the screen, just the blank form. that&#8217;s what I am talking about)</p>
<h2>Ok, how do I un-hang my Swing Application</h2>
<p>
&nbsp;&nbsp;&nbsp;Simple. Don&#8217;t let the code in your listener execute for a long time. But what if you have to write code that takes long time to execute? For eg., what if you need to fetch large data from a db table and display it to the user when the user presses a button? Again, Simple. Execute the time consuming task in a separate thread. The paint method executing in the EDT can execute parallely when your new thread is happily waiting for the data to get fetched from the DB(yes, the laws of nature allow this). Simple, right?<br />
<br />
&nbsp;&nbsp;&nbsp;But wait, it&#8217;s not that simple. What would you do after you fetch the data. You cannot display the value directly from the new thread that you just created! As I told you all graphical operations must execute in the EDT. So you need to come back to the even dispatch thread to display the fetched values&#8230;How do you do that? I will come to that in a second. but let&#8217;s make sure you have the steps clearly defined.</p>
<p>
So here are the steps,</p>
<ul>
<li>The action listener(of the button press) gets executed in the EDT </li>
<li>You make a background thread to load the data [or does any other time consuming task for that matter] </li>
<li>the background task calls code in the EDT to display the newly fetched data. </li>
</ul>
<p>
&nbsp;&nbsp;&nbsp;From the background thread you can use methods like SwingUtilties.invokeLater() to run code in the EDT.. But&#8230;.</p>
<p></p>
<h2>Wouldn&#8217;t it be dreamy if &#8230;(yes, this title was picked up those head first books)</h2>
<p>
&nbsp;&nbsp;&nbsp;Wouldn&#8217;t it be dreamy you could write just two methods, one method (m1) where you put the code to load data and the other method (m2) where you write the code to update the UI? Wouldn&#8217;t it be great, if automatically m1(loading the data) is called first and then when that completes,m2 is automatically called? Wouldn&#8217;t it be magical, if somehow m1 could be automatically get called in a background thread and m2 in the EDT?<br />
&nbsp;&nbsp;&nbsp;Well, lucky you, there&#8217;s a simple utility class ships with Java 6 that let&#8217;s you do this. and that  class is&#8230; well, no surprise here.. The SwingWorker Class.</p>
<h2>So SwingWorker is this..</h2>
<p>
A class having two main methods, </p>
<ul>
<li>method &#8220;doInBackground()&#8221; which always get executed in the background thread. This is the m1 method we were discussing in the last paragraph. Here you can put code that executes for as much time as you like!</li>
<li>method &#8220;done()&#8221; which always gets executed in the EDT, and always get executed after the doInBackground() method gets finished. Same as the m2 method in the previous paragraph. So here&#8217;s where you put the code that displays the data after the data gets loaded.</li>
</ul>
<p></p>
<h2>An Example</h2>
<p><br/></p>

<div class="wp_syntax"><div class="code"><pre class="java">SwingWorker worker = <span style="color: #000000; font-weight: bold;">new</span> SwingWorker<span style="color: #66cc66;">&lt;</span>List, Void<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
   <span style="color: #808080; font-style: italic;">//This method automatically gets executed in a background thread</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">List</span> doInBackground<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
       <span style="color: #808080; font-style: italic;">//fetch the big list from the 	</span>
       <span style="color: #aaaadd; font-weight: bold;">List</span> list = getBigListAfterExecutingForLongTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
       <span style="color: #000000; font-weight: bold;">return</span> intDoc<span style="color: #66cc66;">;</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #808080; font-style: italic;">//This methods automatically gets executed in the EDT</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> done<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">//Get method retuns the exect, same thing </span>
      <span style="color: #808080; font-style: italic;">//that the doInBackground() method returned</span>
      <span style="color: #aaaadd; font-weight: bold;">List</span> list = get<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">//Now do all UI Operations here..</span>
      updateTheUIWithThisBigList<span style="color: #66cc66;">&#40;</span>list<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">;</span>  
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">//Code that executes when the button is pressed</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> actionPerformed<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">ActionEvent</span> ae<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  worker.<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p></p>
<h2>O, No!, I don&#8217;t get to use Java 6 in my project</h2>
<p>&nbsp;&nbsp;&nbsp; Don&#8217;t worry. There&#8217;s a backport of the exact SwingWorker present in Java 6 for Java 5. You can download it from here .	&#8220;<a href="https://swingworker.dev.java.net/">https://swingworker.dev.java.net/</a>&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javaswing.net/using-swingworker-a-step-by-step-tutorial.html/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
