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
256 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
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.

 
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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

840 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