Solved

Cursor leaves java applet when you tab through the field, can I have it cycle back to the first field?

Posted on 2007-12-03
7
259 Views
Last Modified: 2008-02-01
I have a number of Java Applets and have rewritten a number of them in Swing from AWT.  In the process I would like to have the tab key cycle back to the first entry field when you get to the end.  Current default functionality is to have it move to the next object outside the applet on the HTML page.

AWT did the same thing, but just noticing the issue now and wondering if this is possible to intercept.

Thanks!
0
Comment
Question by:ebertk
[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
  • 4
  • 3
7 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 20401213
0
 
LVL 5

Author Comment

by:ebertk
ID: 20417047
Any other examples of how this might be done?  I took a look and not sure where to begin from the example above.
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 500 total points
ID: 20417765
see the below policy code,

    JTextField tf1, tf2, tf3, tf4, tf5, tf6;
    JTable table;
    ...
    public class MyOwnFocusTraversalPolicy
                 extends FocusTraversalPolicy {

        public Component getComponentAfter(Container focusCycleRoot,
                                           Component aComponent) {
            if (aComponent.equals(tf1)) {
                return tf2;
            } else if (aComponent.equals(tf2)) {
                return tf3;
            } else if (aComponent.equals(tf3)) {
                return tf4;
            } else if (aComponent.equals(tf4)) {
                return tf5;
            } else if (aComponent.equals(tf5)) {
                return tf6;
            } else if (aComponent.equals(tf6)) {
                return table;
            } else if (aComponent.equals(table)) {
                return tf1;
            }
            return tf1;
        }

        public Component getComponentBefore(Container focusCycleRoot,
                                       Component aComponent) {
            if (aComponent.equals(tf1)) {
                return table;
            } else if (aComponent.equals(tf2)) {
                return tf1;
            } else if (aComponent.equals(tf3)) {
                return tf2;
            } else if (aComponent.equals(tf4)) {
                return tf3;
            } else if (aComponent.equals(tf5)) {
                return tf4;
            } else if (aComponent.equals(tf6)) {
                return tf5;
            } else if (aComponent.equals(table)) {
                return tf6;
            }
            return tf1;
        }

        public Component getDefaultComponent(Container focusCycleRoot) {
            return tf1;
        }

        public Component getLastComponent(Container focusCycleRoot) {
            return table;
        }

        public Component getFirstComponent(Container focusCycleRoot) {
            return tf1;
        }
    }

To use a custom FocusTraversalPolicy, use code like the following on any focus cycle root.

    MyOwnFocusTraversalPolicy newPolicy = new MyOwnFocusTraversalPolicy();
    frame.setFocusTraversalPolicy(newPolicy);
//here instead of frame, you would set the policy to your applet!

you have to return the first component in the getComponentAfter method if the current focused comp is the last component in the screen!
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 5

Author Comment

by:ebertk
ID: 20420016
Cool.  One more quick question - in the method getComponentAfter, what would I do if I didn't want to have all of the components listed out in an if statement but instead just have it return whatever component would normally get the focus unless we were in the last field?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 20420039
then just have the last component!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 20420049
I mean, check the last component and return the first compoennt!
0
 
LVL 5

Author Comment

by:ebertk
ID: 20421962
Great! Thanks!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

696 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