Article View: borland.public.jbuilder.java.swing
Article #9604Re: InputVerifier class (clarification)
From: Jason Burton
Date: Thu, 17 Feb 2000 00:00
Date: Thu, 17 Feb 2000 00:00
398 lines
16607 bytes
16607 bytes
This is a multi-part message in MIME format.
------=_NextPart_000_0078_01BF7952.04C71BA0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Make a JCheckBox or JButton descendant, and use that instead of =
JCheckBox or JButton. I never use anything directly out of Swing or any =
third party API, instead I make a descendant in my own packages, even if =
it doesn't add any new functionality. This way I can add functionality =
(such as a bug fix like this) to all of my applications in one central =
point.
Anyway, if you add the following code to a JButton or JCheckBox to add =
this simple piece of code, it fixes the problem you report:
protected void fireActionPerformed(ActionEvent e)
{
if (!isRequestFocusEnabled() || hasFocus())
super.fireActionPerformed(e);
}
I'm not sure if Sun would see this as a bug or not, as it allows you to =
add your own custom handling. Consider a JButton that is intended as =
the "Cancel" button for a screen. Depending on the application, the =
developer may or may not want InputVerifier's triggered when the =
"Cancel" button is clicked. The user may be clicking "Cancel" because =
the InputVerifier is nagging them, and the need to go to some other =
screen to correct something. Either way, the above piece of code will =
only trigger InputVerifier's if the button you are trying to click has =
its requestFocusEnabled property set to true.
Hope this helps,
Jason Burton
Chris Vinson <nospamVinsonCL@pebio.com> wrote in message =
news:38AAFB14.719BDE80@pebio.com...
Clarification:=20
Yes, the focusGained() event _is not_ sent to the JCheckBox (as =
advertised in Sun's javadoc).=20
However, the actionPerformed event is sent to the JCheckBox!=20
The state of the JCheckBox component changes even though the data in =
the component with focus is invalid!=20
So, I'd like to rephrase my question::=20
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?=20
I would like to prevent ALL components from receiving focus receiving =
an actionPerformed() event if validation fails. Is this possible?=20
-CV=20
=20
=20
Chris Vinson wrote:=20
I'm one of the many who would like Swing to provide a useful =
validation=20
solution on JTextField components. On the web yesterday, I read =
that=20
kestrel, jdk13rc1, contains a new class called InputVerifier so I=20
downloaded it and took it for a spin around the block.=20
Today I ran their test program and verified that focus just not get=20
shifted to another JTextField if validation fails. However, I added =
a=20
toolbar and button to the same panel in their test program and found =
out=20
that this new class does not prevent other components to gain focus =
when=20
the validation fails.=20
Has anyone found a way prevent other components from gaining focus =
when=20
a user's entry in a JTextField fails validation?=20
I would like to prevent ALL components from receiving focus if=20
validation fails. Is this possible?=20
-CV=20
vinson C L @ pebio.com (no spaces)=20
//----------------------code=20
below------------------------------------------=20
import java.awt.*;=20
import java.util.*;=20
import java.awt.event.*;=20
import javax.swing.*;=20
// This program demonstrates the use of the Swing InputVerifier =
class.=20
// It creates two text fields; the first of the text fields expects =
the=20
// string "pass" as input, and will allow focus to advance out of =
it=20
// only after that string is typed in by the user.=20
public class VerifierTest extends JFrame=20
{=20
JTextField jTextField1 =3D new JTextField();=20
JTextField jTextField2 =3D new JTextField();=20
JToolBar jToolBar1 =3D new JToolBar();=20
JButton jButton1 =3D new JButton();=20
public VerifierTest ()=20
{=20
addWindowListener (new MyWAdapter ());=20
try=20
{=20
jbInit();=20
}=20
catch(Exception e)=20
{=20
e.printStackTrace();=20
}=20
}=20
public static void main (String [] args)=20
{=20
Frame f =3D new VerifierTest ();=20
f.pack();=20
f.show();=20
}=20
class MyWAdapter extends WindowAdapter=20
{=20
public void windowClosing(WindowEvent event)=20
{=20
System.exit (0);=20
}=20
}=20
class PassVerifier extends InputVerifier=20
{=20
public boolean verify(JComponent input)=20
{=20
JTextField tf =3D (JTextField) input;=20
String pass =3D tf.getText();=20
boolean bRet =3D false;=20
if (pass.equals("pass"))=20
bRet =3D true;=20
System.out.println("#verify <" + pass + ">, boolean =3D " + bRet );=20
return bRet;=20
}=20
// public boolean shouldYieldFocus(JComponent input)=20
// {=20
// JTextField tf =3D (JTextField) input;=20
// String pass =3D tf.getText();=20
// boolean bRet =3D false;=20
//=20
// if (pass.equals("pass"))=20
// bRet =3D true;=20
//=20
//System.out.println("#shouldYieldFocus <" + pass + ">, boolean =3D =
" +=20
bRet );=20
//=20
// return bRet;=20
// }=20
}=20
private void jbInit() throws Exception=20
{=20
jTextField1.setText("jTextField1");=20
jTextField2.setText("jTextField2");=20
jButton1.setText("jButton1");=20
jButton1.addActionListener(new java.awt.event.ActionListener()=20
{=20
public void actionPerformed(ActionEvent e)=20
{=20
jButton1_actionPerformed(e);=20
}=20
});=20
this.getContentPane().add(jTextField1, BorderLayout.CENTER);=20
this.getContentPane().add(jTextField2, BorderLayout.SOUTH);=20
this.getContentPane().add(jToolBar1, BorderLayout.NORTH);=20
jToolBar1.add(jButton1, null);=20
jTextField1.setInputVerifier(new PassVerifier());=20
jTextField2.setInputVerifier(new PassVerifier());=20
}=20
void jButton1_actionPerformed(ActionEvent e)=20
{=20
System.out.println("Button was clicked");=20
}=20
}
------=_NextPart_000_0078_01BF7952.04C71BA0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Make a JCheckBox or JButton descendant, =
and use=20
that instead of JCheckBox or JButton. I never use anything =
directly out of=20
Swing or any third party API, instead I make a descendant in my own =
packages,=20
even if it doesn't add any new functionality. This way I can add=20
functionality (such as a bug fix like this) to all of my applications in =
one=20
central point.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Anyway, if you add the following code =
to a JButton=20
or JCheckBox to add this simple piece of code, it fixes the problem you=20
report:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>protected void =
fireActionPerformed(ActionEvent=20
e)<BR>{<BR> if (!isRequestFocusEnabled() ||=20
hasFocus())<BR> =20
super.fireActionPerformed(e);<BR>}<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I'm not sure if Sun would see this as a =
bug or not,=20
as it allows you to add your own custom handling. Consider a =
JButton that=20
is intended as the "Cancel" button for a screen. Depending on the=20
application, the developer may or may not want InputVerifier's triggered =
when=20
the "Cancel" button is clicked. The user may be clicking "Cancel" =
because=20
the InputVerifier is nagging them, and the need to go to some other =
screen to=20
correct something. Either way, the above piece of code will only =
trigger=20
InputVerifier's if the button you are trying to click has its=20
requestFocusEnabled property set to true.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Hope this helps,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Jason Burton</DIV></FONT>
<BLOCKQUOTE=20
style=3D"BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: =
0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
<DIV>Chris Vinson <<A=20
=
href=3D"mailto:nospamVinsonCL@pebio.com">nospamVinsonCL@pebio.com</A>>=
wrote=20
in message <A=20
=
href=3D"news:38AAFB14.719BDE80@pebio.com">news:38AAFB14.719BDE80@pebio.co=
m</A>...</DIV>Clarification:=20
<BR>Yes, the focusGained() event _is not_ sent to the JCheckBox (as =
advertised=20
in Sun's javadoc). <BR>However, the actionPerformed event is sent to =
the=20
JCheckBox! <BR>The state of the JCheckBox component changes even =
though the=20
data in the component with focus is invalid! <BR>So, I'd like to =
rephrase my=20
question::=20
<P>Has anyone found a way prevent other components from <STRIKE><FONT=20
face=3D"">gaining focus</FONT></STRIKE> receiving an actionPerformed() =
event=20
when a user's entry in a JTextField fails validation? <BR>I would like =
to=20
prevent ALL components from <STRIKE>receiving focus</STRIKE> receiving =
an=20
actionPerformed() event if validation fails. Is this possible?=20
<P>-CV <BR> <BR> =20
<P>Chris Vinson wrote:=20
<BLOCKQUOTE TYPE=3D"CITE">I'm one of the many who would like Swing to =
provide=20
a useful validation <BR>solution on JTextField components. On =
the web=20
yesterday, I read that <BR>kestrel, jdk13rc1, contains a new class =
called=20
InputVerifier so I <BR>downloaded it and took it for a spin around =
the=20
block.=20
<P>Today I ran their test program and verified that focus just not =
get=20
<BR>shifted to another JTextField if validation fails. =
However, I=20
added a <BR>toolbar and button to the same panel in their test =
program and=20
found out <BR>that this new class does not prevent other components =
to gain=20
focus when <BR>the validation fails.=20
<P>Has anyone found a way prevent other components from gaining =
focus when=20
<BR>a user's entry in a JTextField fails validation? <BR>I would =
like to=20
prevent ALL components from receiving focus if <BR>validation =
fails. =20
Is this possible?=20
<P>-CV <BR>vinson C L @ pebio.com (no spaces)=20
<P>//----------------------code=20
<BR>below------------------------------------------=20
<P>import java.awt.*; <BR>import java.util.*; <BR>import =
java.awt.event.*;=20
<BR>import javax.swing.*;=20
<P> // This program demonstrates the use of the Swing =
InputVerifier=20
class. <BR> // It creates two text fields; the first of the =
text fields=20
expects the=20
<P> // string "pass" as input, and will allow focus to advance =
out of=20
it <BR> // only after that string is typed in by the user.=20
<P>public class VerifierTest extends JFrame <BR>{ <BR> =
JTextField=20
jTextField1 =3D new JTextField(); <BR> JTextField jTextField2 =
=3D new=20
JTextField(); <BR> JToolBar jToolBar1 =3D new JToolBar(); =
<BR> =20
JButton jButton1 =3D new JButton();=20
<P> public VerifierTest () <BR> { <BR> =20
addWindowListener (new MyWAdapter ());=20
<P> try <BR> {=20
<BR> jbInit(); <BR> =
}=20
<BR> catch(Exception e) <BR> {=20
<BR> e.printStackTrace();=20
<BR> } <BR> }=20
<P> public static void main (String [] args) <BR> {=20
<BR> Frame f =3D new VerifierTest (); =
<BR> =20
f.pack(); <BR> f.show(); <BR> }=20
<P> class MyWAdapter extends WindowAdapter <BR> {=20
<BR> public void windowClosing(WindowEvent event)=20
<BR> { <BR> =
System.exit (0);=20
<BR> } <BR> }=20
<P> class PassVerifier extends InputVerifier <BR> {=20
<BR> public boolean verify(JComponent input)=20
<BR> { <BR> =
JTextField tf =3D=20
(JTextField) input; <BR> String pass =
=3D=20
tf.getText();=20
<P> boolean bRet =3D false;=20
<P> if (pass.equals("pass"))=20
<BR> bRet =3D true;=20
<P>System.out.println("#verify <" + pass + ">, boolean =3D " + =
bRet );=20
<P> return bRet; =
<BR> }=20
<P>// public boolean shouldYieldFocus(JComponent =
input)=20
<BR>// { <BR>// =
JTextField=20
tf =3D (JTextField) input; <BR>// =
String pass =3D=20
tf.getText(); <BR>// boolean bRet =3D =
false;=20
<BR>// <BR>// if (pass.equals("pass")) =
<BR>// bRet =3D =
true; <BR>//=20
<BR>//System.out.println("#shouldYieldFocus <" + pass + ">, =
boolean =3D=20
" + <BR>bRet ); <BR>// <BR>// return =
bRet;=20
<BR>// } <BR> }=20
<P> private void jbInit() throws Exception <BR> {=20
<BR> jTextField1.setText("jTextField1");=20
<BR> jTextField2.setText("jTextField2");=20
<BR> jButton1.setText("jButton1"); =
<BR> =20
jButton1.addActionListener(new java.awt.event.ActionListener()=20
<BR> { <BR> public =
void=20
actionPerformed(ActionEvent e) <BR> {=20
<BR> =
jButton1_actionPerformed(e);=20
<BR> } <BR> });=20
<P> this.getContentPane().add(jTextField1,=20
BorderLayout.CENTER); <BR> =20
this.getContentPane().add(jTextField2, BorderLayout.SOUTH);=20
<BR> this.getContentPane().add(jToolBar1,=20
BorderLayout.NORTH); <BR> jToolBar1.add(jButton1, =
null);=20
<P> jTextField1.setInputVerifier(new =
PassVerifier());=20
<BR> jTextField2.setInputVerifier(new =
PassVerifier());=20
<BR> }=20
<P> void jButton1_actionPerformed(ActionEvent e) <BR> {=20
<BR>System.out.println("Button was clicked"); <BR> }=20
<BR>}</P></BLOCKQUOTE></BLOCKQUOTE></BODY></HTML>
------=_NextPart_000_0078_01BF7952.04C71BA0--
Technical Details
Message-ID:
<88hl15$obp8@bornews.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<38A9C5A8.BA3BC930@pebio.com> <38AAFB14.719BDE80@pebio.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,d35caa8f8e7a3b38 X-Google-Attributes: gid111d45,public From: "Jason Burton" <Jason.Burton@McLeodSoftware.com> Subject: Re: InputVerifier class (clarification) Date: 2000/02/17 Message-ID: <88hl15$obp8@bornews.borland.com> X-Deja-AN: 587064213 References: <38A9C5A8.BA3BC930@pebio.com> <38AAFB14.719BDE80@pebio.com> X-Priority: 3 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0078_01BF7952.04C71BA0" X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Organization: Another Netscape Collabra Server User X-MSMail-Priority: Normal Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing