Solved

Making a JTextField keep its focus in a web page

Posted on 2001-06-20
2
290 Views
Last Modified: 2010-03-31
We are building messaging-based web application that combines an applet and a couple of JSP pages in browser.  When the applet receives certain messages from the server, it creates a URL that is to be directed to the JSP pages in a different frame of the same browser.  When the JSP receives the request, it updates the page with the information contained in the URL.

The problem we have seen is that if a user is in a JTextField while the frame containing the JSP is being refreshed, it looses the focus and does not return to the field.  

Is there a way for the applet (particularly the JTextField the user is currently entering text) to maintain the focus during the JSP refresh in a separate window?

We are using JDK v1.3.002 with IE 5.x on a Wintel platform if this helps.

Thanks in advance.
0
Comment
Question by:mwalker
2 Comments
 
LVL 2

Accepted Solution

by:
vpadma earned 100 total points
ID: 6210733
Hi,
I found a good explanation in the tutorial.
With Swing components, you can override the isManagingFocus method to return true and get Tab and Shift-Tab keystrokes sent to the component's key listener. This allows the component to manage focus traversal when the component has the focus. The following code implements a text field that doesn't allow the focus to traverse to the next component until valid data is entered:

class MyTextField extends JTextField {
    boolean validDataEntered = false;

    // Constructors
    public MyTextField () {
        super();
        init();
    }
    public MyTextField (String text) {
        super(text);
        init();
    }

    // Private initialization routine to be run at construct time
    private void init() {
        // add a key event listener that will consume tab keys until valid data entered in field
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent event) {
                // look for tab keys
                if(event.getKeyCode() == KeyEvent.VK_TAB
                || event.getKeyChar() == '\t') {
                    // if no valid data entered in field, consume event
                    // so that it won't be passed on to focus manager
                    if(!validDataEntered) {
                        event.consume();
                    }
                }
                else {
                    // assume any key other than tab is valid data
                    validDataEntered = true;
                }
            }
        });
    }

    // Override to inform focus manager that component is managing focus changes
    public boolean isManagingFocus() { return true; }
}

In this example, any keystrokes other than Tab keystrokes are considered to be valid data. If no valid data has been entered, the text field's key listener consumes Tab keystrokes, preventing them from reaching the focus manager. Of course, if you were implementing this technique in a real application, you would want to present a message to the user and inform them as to why they are being prevented from tabbing away from the text field.

One limitation to managing focus traversal at the component level is that you can't control focus changes initiated by mouse actions. In fact, you can't even do this by writing your own custom focus manager. To control focus changes initiated by mouse actions, you'll have to modify the low-level mouse handling for all of your components.

Hope it helps,
Padma.

0
 

Author Comment

by:mwalker
ID: 6219417
This did the trick.  Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 39
topping3 challenge 14 70
Eclipse Neon and jdk 1.8.0 11 143
backtracking recursion  code 19 54
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now