Article View: borland.public.jbuilder.java.swing
Article #9690Re: InputVerifier class
From: Chris Vinson
Date: Wed, 16 Feb 2000 00:00
Date: Wed, 16 Feb 2000 00:00
393 lines
15928 bytes
15928 bytes
--------------83396019D4CCCE3F003A57DB
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hi Jason,
Thanks for your response:
Re: JDK1.3 and JBuilder3: Yes, it works under JBuilder3 Pro.
1) I went to sun and downloaded kestrel, and installed it on my workstation.
2) In JBuilder3, Project/DefaultProperties click the define button.
3) Click New, then specify path to kestrel's java.exe.
4) Build your test app that uses setInputVerifier().
5) Before compile, verify that the properties has 'java version "1.3.0rc1"'
in the Target JDK drop-list.
Re: JButton:
You asked "is it just JButton that is not behaving correctly"? I contend that
the InputVerifier is not behaving correctly as per my previous message.
Reason? The class documentation says "Before focus is transferred to another
Swing component that requests it, the input verifier's 'shouldYieldFocus()'
method is called. Focus is transferred only if that method returns true." I'd
prefer to find out how to use the InputVerifier class effectively, as it was
intended to be used. (Subclassing every swing component in our applications in
order to catch key click events (and keypress events to catch <Tab>) sounds
doable but brute-force.)
I'm writing this message to ask the world if Sun's InputVerifier class and Sun's
example (with additional swing components) provide the intended solution to
input validation.
-CV
Jason Burton wrote:
> I have been wanting to try this for quite a while now. Are you able to get
> calls to setInputVerifier() to compile under JBuilder? It always tells me
> that the method is not found. I suspect that it is because it is a
> completely new method to JDK 1.3. I would assume that defining your target
> JDK as JDK 1.3 would do it, but that doesn't seem to work. If you are able
> to compile this under JBuilder 3 (I use Enterprise), please try to give me
> some clue as to how to do it.
>
> As for the problem you are experiencing, is it just JButton that is not
> behaving correctly? I think by default the JButton requestFocusEnabled
> property is false. It also might be possible that the "click" mouse event
> might be generated even if the JButton does not have focus. You might try
> making a JButton descendant that blocks the click event if the button
> doesn't have focus.
>
> Thanks,
> Jason Burton
>
> Chris Vinson <nospamVinsonCL@pebio.com> wrote in message
> news:38A9C5A8.BA3BC930@pebio.com...
> > 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)
> >
> > file://----------------------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;
> > //
> > file://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");
> > }
> > }
> >
> >
--------------83396019D4CCCE3F003A57DB
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hi Jason,
<p>Thanks for your response:
<br>Re: JDK1.3 and JBuilder3: Yes, it works under JBuilder3 Pro.
<br> 1) I went to sun and downloaded kestrel, and installed
it on my workstation.
<br> 2) In JBuilder3, Project/DefaultProperties click
the define button.
<br> 3) Click New, then specify path to kestrel's java.exe.
<br> 4) Build your test app that uses setInputVerifier().
<br> 5) Before compile, verify that the properties has
'java version "1.3.0rc1"' in the Target JDK drop-list.
<p>Re: JButton:
<br>You asked "is it just JButton that is not behaving correctly"?
I contend that the InputVerifier is not behaving correctly as per my previous
message. Reason? The class documentation says <u>"Before focus
is transferred to another Swing component that requests it, the input verifier's
'shouldYieldFocus()' method is called. Focus is transferred only
if that method returns true."</u> I'd prefer to find out how to use
the InputVerifier class effectively, as it was intended to be used.
(Subclassing every swing component in our applications in order to catch
key click events (and keypress events to catch <Tab>) sounds doable
but brute-force.)
<p>I'm writing this message to ask the world if Sun's InputVerifier class
and Sun's example (with additional swing components) provide the
intended solution to input validation.
<p>-CV
<p>Jason Burton wrote:
<blockquote TYPE=CITE>I have been wanting to try this for quite a while
now. Are you able to get
<br>calls to setInputVerifier() to compile under JBuilder? It always
tells me
<br>that the method is not found. I suspect that it is because it
is a
<br>completely new method to JDK 1.3. I would assume that defining
your target
<br>JDK as JDK 1.3 would do it, but that doesn't seem to work. If
you are able
<br>to compile this under JBuilder 3 (I use Enterprise), please try to
give me
<br>some clue as to how to do it.
<p>As for the problem you are experiencing, is it just JButton that is
not
<br>behaving correctly? I think by default the JButton requestFocusEnabled
<br>property is false. It also might be possible that the "click"
mouse event
<br>might be generated even if the JButton does not have focus. You
might try
<br>making a JButton descendant that blocks the click event if the button
<br>doesn't have focus.
<p>Thanks,
<br>Jason Burton
<p>Chris Vinson <nospamVinsonCL@pebio.com> wrote in message
<br><a href="news:38A9C5A8.BA3BC930@pebio.com">news:38A9C5A8.BA3BC930@pebio.com</a>...
<br>> 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.
<br>>
<br>> 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.
<br>>
<br>> 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?
<br>>
<br>> -CV
<br>> vinson C L @ pebio.com (no spaces)
<br>>
<br>> <a href="file://----------------------code">file://----------------------code</a>
<br>> below------------------------------------------
<br>>
<br>> import java.awt.*;
<br>> import java.util.*;
<br>> import java.awt.event.*;
<br>> import javax.swing.*;
<br>>
<br>> // This program demonstrates the use of the Swing InputVerifier
class.
<br>> // It creates two text fields; the first of the text fields
expects the
<br>>
<br>> // string "pass" as input, and will allow focus to advance
out of it
<br>> // only after that string is typed in by the user.
<br>>
<br>> 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();
<br>>
<br>> public VerifierTest ()
<br>> {
<br>> addWindowListener (new MyWAdapter ());
<br>>
<br>> try
<br>> {
<br>> jbInit();
<br>> }
<br>> catch(Exception e)
<br>> {
<br>> e.printStackTrace();
<br>> }
<br>> }
<br>>
<br>> public static void main (String [] args)
<br>> {
<br>> Frame f = new VerifierTest ();
<br>> f.pack();
<br>> f.show();
<br>> }
<br>>
<br>> class MyWAdapter extends WindowAdapter
<br>> {
<br>> public void windowClosing(WindowEvent event)
<br>> {
<br>> System.exit (0);
<br>> }
<br>> }
<br>>
<br>> class PassVerifier extends InputVerifier
<br>> {
<br>> public boolean verify(JComponent input)
<br>> {
<br>> JTextField tf = (JTextField)
input;
<br>> String pass = tf.getText();
<br>>
<br>> boolean bRet = false;
<br>>
<br>> if (pass.equals("pass"))
<br>> bRet = true;
<br>>
<br>> System.out.println("#verify <" + pass + ">, boolean = " + bRet
);
<br>>
<br>> return bRet;
<br>> }
<br>>
<br>> // 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>> <a href="file://System.out.println(" #shouldYieldFocus">file://System.out.println("#shouldYieldFocus</a>
<" + pass + ">, boolean = " +
<br>> bRet );
<br>> //
<br>> // return bRet;
<br>> // }
<br>> }
<br>>
<br>> 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>> });
<br>>
<br>> 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);
<br>>
<br>> jTextField1.setInputVerifier(new PassVerifier());
<br>> jTextField2.setInputVerifier(new PassVerifier());
<br>> }
<br>>
<br>> void jButton1_actionPerformed(ActionEvent e)
<br>> {
<br>> System.out.println("Button was clicked");
<br>> }
<br>> }
<br>>
<br>></blockquote>
</html>
--------------83396019D4CCCE3F003A57DB--
Technical Details
Message-ID:
<38AAF1C6.C22AB747@pebio.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<38A9C5A8.BA3BC930@pebio.com> <88efep$lk03@bornews.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,d714fcce6015d43e X-Google-Attributes: gid111d45,public From: Chris Vinson <nospamVinsonCL@pebio.com> Subject: Re: InputVerifier class Date: 2000/02/16 Message-ID: <38AAF1C6.C22AB747@pebio.com> X-Deja-AN: 586630139 References: <38A9C5A8.BA3BC930@pebio.com> <88efep$lk03@bornews.borland.com> X-Accept-Language: en Content-Type: multipart/alternative; boundary="------------83396019D4CCCE3F003A57DB" Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing