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
246 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count7 challenge 12 70
ArrayLists in ArrayLists/HashMaps? 4 40
Eclipse Neon and jdk 1.8.0 11 69
base64 decode encode 12 97
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ā€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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