Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Making a JTextField keep its focus in a web page

Posted on 2001-06-20
2
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
vpadma earned 300 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

705 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