Article View: borland.public.jbuilder.java.language
Article #18203Swing Focus Management
From: Thomas Franke
Date: Tue, 28 Jul 1998 00:00
Date: Tue, 28 Jul 1998 00:00
238 lines
10016 bytes
10016 bytes
Referring to a bug in the JDC buglist
(http://developer.javasoft.com/developer/bugParade/index.html, search
for +focus +swing +field, go to
http://developer.javasoft.com/developer/bugParade/bugs/4128659.html or
se below at ***)
i would like to know if someone
has any idea of any workaround do do low level field validation on a
JTextField component.
E.g. I dont want the user to leave a JTextfield jTextField1, if the
entered data is not the actual date i
am checking the jTextField1.getText()-result onFocusLost for
jTextField1 and do a
jTextField1. requestFocus(), if the result is not correct.
As a result of that my text-cursor starts blinking in the next focused
component (also a textField)
but when i type some letters they appear in jTextField1, where no cursor
is blinking.
***
Bug Id
4128659
Votes
16
Synopsis
requestFocus in focusLost does not work
in Swing
Category
general:classes_swing
Reported Against
1.2beta3
Release Fixed
State
In progress, bug
Submit Date
Apr 14, 1998
Description
In the following code the request focus
event is ignored. The intent of
the code is to keep the focus in the
first text field. I plan to
use this for preventing a user from
leaving a field when they have typed
in an invalid entry (ie bad date).
If the swing components are replaced
with AWT components the
code works as expected.
This bug is related to 4103289
(requestFocus() does not work for
TextFields) and 4031025 (focus not
transferred between
lightweight components).
Sample Output:
F:\JAVA\prototype>java TestFocusSwing
MyFocusAdapter:focusGained - TextField
1
MyFocusAdapter:focusGained - TextField
1
<hit tab here>
MyFocusAdapter:focusLost - TextField 1
MyFocusAdapter:focusGained - TextField
2
Also should "TextField 1" be receiving
two focus events?
Source Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.swing.*;
/**
* Simple program to demonstrate test
focus problem
* in Swing. Replace swing components
with awt and
* example works as expected.
* JDK Version = 1.2beta2 & 1.2beta3
* Platform = Windows NT (sp3)
*
* @author xxxxx@xxxxx
*/
class TestFocusSwing extends JFrame
{
public TestFocusSwing ()
{
JTextField tf;
tf = new JTextField ("TextField 1");
tf.addFocusListener (new MyFocusAdapter
("TextField 1"));
getContentPane().add (tf,
BorderLayout.NORTH);
tf = new JTextField ("TextField 2");
tf.addFocusListener (new MyFocusAdapter
("TextField 2"));
getContentPane().add (tf,
BorderLayout.SOUTH);
// event handling
addWindowListener (new MyWAdapter ());
}
public static void main (String []
args)
{
Frame f = new TestFocusSwing ();
f.pack();
f.show();
}
class MyWAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent
event)
{
System.exit (0);
}
}
class MyFocusAdapter extends
FocusAdapter
{
private String myName;
public MyFocusAdapter (String name)
{
myName = name;
}
public void focusLost (FocusEvent e)
{
System.out.println
("MyFocusAdapter:focusLost - " + myName);
if (myName.equals ("TextField 1")) {
e.getComponent().requestFocus ();
}
}
public void focusGained (FocusEvent e)
{
System.out.println
("MyFocusAdapter:focusGained - " + myName);
}
}
}
(Review ID: 27394)
Workaround
None.
Evaluation
The requestFocus also has other
unpleasant side-effects. To see that,
try clicking between the two text
fields to change focus. Very screwed
up.
xxxxx@xxxxx 1998-06-03
Your Comments
& Work-arounds
(These are NOT
written by Sun!)
Include a link with my name & email
Fri May 15 09:04:11 PDT 1998
jwangler
I have found this problem also, it
completely
prohibits one from doing field level
validation,
which is very important in most data
entry
applications. Hope this one gets fixed
soon...
Wed May 27 12:22:52 PDT 1998
paull28
Is this a problem with Swing 1.0.2 or
with version 1.1.6 of the JDK?
The requestFocus() method has
absolutely no effect in my app which
has been developed using Swing 1.0.2
and JDK 1.1.6.
Tue Jun 02 07:27:57 PDT 1998
PaMar
I had some problem with it, too.
Adding a repaint() immediately after
the focus request seems to work.
target.requestFocus();
target.repaint();
Thu Jun 04 00:38:32 PDT 1998
recat
The problem with requestFocus and JFC
is existent
in any combination of JDK1.1.4 ...
JDK1.1.6 and
Swing. To handle requestFocus in
TextFields we
simply used AWT-TextFields. The problem
must be
inside the DocumentListener. The mixing
of Swing
and AWT-Components is a marginal
problem but no
focus request is a big one. Another
very strange
effect happens if a JTextField was
entered,
leaved and after leaving requesting the
focus
again. If any menu was entered after
leaving the
JTextField, the JFrame was empty (no
component was
viewable) and after a while the
components came up
and the first JTextField was focused.
After we we
changed JTextFields to AWT-TextFields
everything
works correct.
Technical Details
Message-ID:
<35BDAE07.8054C4E9@brockhaus-ag.de>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,e78842c681e7691a,start X-Google-Attributes: gidf74b3,public From: Thomas Franke <tfranke@brockhaus-ag.de> Subject: Swing Focus Management Date: 1998/07/28 Message-ID: <35BDAE07.8054C4E9@brockhaus-ag.de> X-Deja-AN: 375613929 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii Organization: Brockhaus Software und Consulting Ing. mbH Mime-Version: 1.0 Reply-To: tfranke@brockhaus-ag.de Newsgroups: borland.public.jbuilder.java.language