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
250 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
  • 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
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.

 
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

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

16 Experts available now in Live!

Get 1:1 Help Now