Article View: borland.public.jbuilder.java.swing
Article #9691Re: InputVerifier class (clarification)
From: Chris Vinson
Date: Wed, 16 Feb 2000 00:00
Date: Wed, 16 Feb 2000 00:00
298 lines
11156 bytes
11156 bytes
--------------DC723A8754043F13E237DD64
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Clarification:
Yes, the focusGained() event _is not_ sent to the JCheckBox (as advertised
in Sun's javadoc).
However, the actionPerformed event is sent to the JCheckBox!
The state of the JCheckBox component changes even though the data in the
component with focus is invalid!
So, I'd like to rephrase my question::
Has anyone found a way prevent other components from gaining focus receiving
an actionPerformed() event when a user's entry in a JTextField fails
validation?
I would like to prevent ALL components from receiving focus receiving an
actionPerformed() event if validation fails. Is this possible?
-CV
Chris Vinson wrote:
> I'm one of the many who would like Swing to provide a useful validation
> solution on JTextField components. On the web yesterday, I read that
> kestrel, jdk13rc1, contains a new class called InputVerifier so I
> downloaded it and took it for a spin around the block.
>
> Today I ran their test program and verified that focus just not get
> shifted to another JTextField if validation fails. However, I added a
> toolbar and button to the same panel in their test program and found out
> that this new class does not prevent other components to gain focus when
> the validation fails.
>
> Has anyone found a way prevent other components from gaining focus when
> a user's entry in a JTextField fails validation?
> I would like to prevent ALL components from receiving focus if
> validation fails. Is this possible?
>
> -CV
> vinson C L @ pebio.com (no spaces)
>
> //----------------------code
> below------------------------------------------
>
> import java.awt.*;
> import java.util.*;
> import java.awt.event.*;
> import javax.swing.*;
>
> // This program demonstrates the use of the Swing InputVerifier class.
> // It creates two text fields; the first of the text fields expects the
>
> // string "pass" as input, and will allow focus to advance out of it
> // only after that string is typed in by the user.
>
> public class VerifierTest extends JFrame
> {
> JTextField jTextField1 = new JTextField();
> JTextField jTextField2 = new JTextField();
> JToolBar jToolBar1 = new JToolBar();
> JButton jButton1 = new JButton();
>
> public VerifierTest ()
> {
> addWindowListener (new MyWAdapter ());
>
> try
> {
> jbInit();
> }
> catch(Exception e)
> {
> e.printStackTrace();
> }
> }
>
> public static void main (String [] args)
> {
> Frame f = new VerifierTest ();
> f.pack();
> f.show();
> }
>
> class MyWAdapter extends WindowAdapter
> {
> public void windowClosing(WindowEvent event)
> {
> System.exit (0);
> }
> }
>
> class PassVerifier extends InputVerifier
> {
> public boolean verify(JComponent input)
> {
> JTextField tf = (JTextField) input;
> String pass = tf.getText();
>
> boolean bRet = false;
>
> if (pass.equals("pass"))
> bRet = true;
>
> System.out.println("#verify <" + pass + ">, boolean = " + bRet );
>
> return bRet;
> }
>
> // public boolean shouldYieldFocus(JComponent input)
> // {
> // JTextField tf = (JTextField) input;
> // String pass = tf.getText();
> // boolean bRet = false;
> //
> // if (pass.equals("pass"))
> // bRet = true;
> //
> //System.out.println("#shouldYieldFocus <" + pass + ">, boolean = " +
> bRet );
> //
> // return bRet;
> // }
> }
>
> private void jbInit() throws Exception
> {
> jTextField1.setText("jTextField1");
> jTextField2.setText("jTextField2");
> jButton1.setText("jButton1");
> jButton1.addActionListener(new java.awt.event.ActionListener()
> {
> public void actionPerformed(ActionEvent e)
> {
> jButton1_actionPerformed(e);
> }
> });
>
> this.getContentPane().add(jTextField1, BorderLayout.CENTER);
> this.getContentPane().add(jTextField2, BorderLayout.SOUTH);
> this.getContentPane().add(jToolBar1, BorderLayout.NORTH);
> jToolBar1.add(jButton1, null);
>
> jTextField1.setInputVerifier(new PassVerifier());
> jTextField2.setInputVerifier(new PassVerifier());
> }
>
> void jButton1_actionPerformed(ActionEvent e)
> {
> System.out.println("Button was clicked");
> }
> }
--------------DC723A8754043F13E237DD64
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Clarification:
<br>Yes, the focusGained() event _is not_ sent to the JCheckBox (as advertised
in Sun's javadoc).
<br>However, the actionPerformed event is sent to the JCheckBox!
<br>The state of the JCheckBox component changes even though the data in
the component with focus is invalid!
<br>So, I'd like to rephrase my question::
<p>Has anyone found a way prevent other components from <strike><font face="">gaining
focus</font></strike> receiving an actionPerformed() event when a user's
entry in a JTextField fails validation?
<br>I would like to prevent ALL components from <strike>receiving focus</strike>
receiving an actionPerformed() event if validation fails. Is this
possible?
<p>-CV
<br>
<br>
<p>Chris Vinson wrote:
<blockquote TYPE=CITE>I'm one of the many who would like Swing to provide
a useful validation
<br>solution on JTextField components. On the web yesterday, I read
that
<br>kestrel, jdk13rc1, contains a new class called InputVerifier so I
<br>downloaded it and took it for a spin around the block.
<p>Today I ran their test program and verified that focus just not get
<br>shifted to another JTextField if validation fails. However, I
added a
<br>toolbar and button to the same panel in their test program and found
out
<br>that this new class does not prevent other components to gain focus
when
<br>the validation fails.
<p>Has anyone found a way prevent other components from gaining focus when
<br>a user's entry in a JTextField fails validation?
<br>I would like to prevent ALL components from receiving focus if
<br>validation fails. Is this possible?
<p>-CV
<br>vinson C L @ pebio.com (no spaces)
<p>//----------------------code
<br>below------------------------------------------
<p>import java.awt.*;
<br>import java.util.*;
<br>import java.awt.event.*;
<br>import javax.swing.*;
<p> // This program demonstrates the use of the Swing InputVerifier
class.
<br> // It creates two text fields; the first of the text fields expects
the
<p> // string "pass" as input, and will allow focus to advance out
of it
<br> // only after that string is typed in by the user.
<p>public class VerifierTest extends JFrame
<br>{
<br> JTextField jTextField1 = new JTextField();
<br> JTextField jTextField2 = new JTextField();
<br> JToolBar jToolBar1 = new JToolBar();
<br> JButton jButton1 = new JButton();
<p> public VerifierTest ()
<br> {
<br> addWindowListener (new MyWAdapter ());
<p> try
<br> {
<br> jbInit();
<br> }
<br> catch(Exception e)
<br> {
<br> e.printStackTrace();
<br> }
<br> }
<p> public static void main (String [] args)
<br> {
<br> Frame f = new VerifierTest ();
<br> f.pack();
<br> f.show();
<br> }
<p> class MyWAdapter extends WindowAdapter
<br> {
<br> public void windowClosing(WindowEvent event)
<br> {
<br> System.exit (0);
<br> }
<br> }
<p> class PassVerifier extends InputVerifier
<br> {
<br> public boolean verify(JComponent input)
<br> {
<br> JTextField tf = (JTextField) input;
<br> String pass = tf.getText();
<p> boolean bRet = false;
<p> if (pass.equals("pass"))
<br> bRet = true;
<p>System.out.println("#verify <" + pass + ">, boolean = " + bRet );
<p> return bRet;
<br> }
<p>// public boolean shouldYieldFocus(JComponent input)
<br>// {
<br>// JTextField tf = (JTextField) input;
<br>// String pass = tf.getText();
<br>// boolean bRet = false;
<br>//
<br>// if (pass.equals("pass"))
<br>// bRet = true;
<br>//
<br>//System.out.println("#shouldYieldFocus <" + pass + ">, boolean
= " +
<br>bRet );
<br>//
<br>// return bRet;
<br>// }
<br> }
<p> private void jbInit() throws Exception
<br> {
<br> jTextField1.setText("jTextField1");
<br> jTextField2.setText("jTextField2");
<br> jButton1.setText("jButton1");
<br> jButton1.addActionListener(new java.awt.event.ActionListener()
<br> {
<br> public void actionPerformed(ActionEvent
e)
<br> {
<br> jButton1_actionPerformed(e);
<br> }
<br> });
<p> this.getContentPane().add(jTextField1, BorderLayout.CENTER);
<br> this.getContentPane().add(jTextField2, BorderLayout.SOUTH);
<br> this.getContentPane().add(jToolBar1, BorderLayout.NORTH);
<br> jToolBar1.add(jButton1, null);
<p> jTextField1.setInputVerifier(new PassVerifier());
<br> jTextField2.setInputVerifier(new PassVerifier());
<br> }
<p> void jButton1_actionPerformed(ActionEvent e)
<br> {
<br>System.out.println("Button was clicked");
<br> }
<br>}</blockquote>
</html>
--------------DC723A8754043F13E237DD64--
Technical Details
Message-ID:
<38AAFB14.719BDE80@pebio.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<38A9C5A8.BA3BC930@pebio.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,d35caa8f8e7a3b38,start X-Google-Attributes: gid111d45,public From: Chris Vinson <nospamVinsonCL@pebio.com> Subject: Re: InputVerifier class (clarification) Date: 2000/02/16 Message-ID: <38AAFB14.719BDE80@pebio.com> X-Deja-AN: 586639325 References: <38A9C5A8.BA3BC930@pebio.com> X-Accept-Language: en Content-Type: multipart/alternative; boundary="------------DC723A8754043F13E237DD64" Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing