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
253 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 24
MySQL  on Tomcat 8 46
String array comparison 4 34
mysql jsp example issue 32 38
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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …

810 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