Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Focus Traversal Policy Questions.

Status
Not open for further replies.

chunkII123

IS-IT--Management
Mar 18, 2009
78
0
0
US
I have poured over all the notes pertaining on how to use the focus traversal policy, and I am honestly drawing a blank. I don't understand how it work - I know it's a method, but i'm uncertain as how to implement it into a program that already has very well established nextFocusableComponent() commands.

Basically What I would like to know is this -

1. What would the policy provider be (for instance the parent container or a main class)?

2. Does this way implement action/ document/ key listeners to each different component?

3. How long do I have to implement this before I can no longer compile with the old deprecated nextFocusableComponent()?

4. Does each individual component need to have a long drawn out method to know when to transfer focus to the next?

I'm very lost...

Thank you everyone for taking the time to help me out here, I really do appreciate it.

Beware of hackers bearing executables. Happy Hunting. 'irc.2600.net'
 
I can give you an example from sun
Please use JDK 1.5 or later to compile FocusTraversalDemo.java
I will tell you more on the class FocusTraversalPolicy.

To use FocusTraversalPolicy for handling focus, you have to
write the coding for the methods extended from FocusTraversalPolicy like the example.
Code:
        public Component getComponentAfter(Container,Component) {...}
        public Component getComponentBefore(Container,Component) {...}
        public Component getDefaultComponent(Container) {...}
        public Component getLastComponent(Container) {...}
        public Component getFirstComponent(Container) {...}
There are Field 1, Field 3 and Field5 in the example.
For the method getComponentAfter, you have to return Field 3 if the component inputted is Field 1.
If the component inputted is Field 3, you have to return Field 5.
In the example, an Vector order is used to store all the components added to the Graphic User Interface so that
the component should be returned can be calculated using the Vector order.
 
I have a question: what are you trying to accomplish by implementing this?

Cheers,
Dian
 
Dian,

I read on Sun's developer center a time ago that the 'nextFocusableComponent()' code would be deprecated in a "near future realease of JDK. Considering the way they described using deprecated code -

"A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous..." or there is a better alternative.

Although with this focus traversal policy business, I don't understand how this is even remotely "better code", considering that a programmer has to add a ton of extra code instead of adding per-say
Code:
nextFocusableComponent("emailAddressField")
.

Who knows though, maybe it will work out better in the end. And to give you a more direct answer, I am trying to get away from the deprecated code.

Beware of hackers bearing executables. Happy Hunting. 'irc.2600.net'
 
I understand your problem. My point is that if you're not trying to do complicated things, maybe adding the elements to the container in the correct order will do the job and the focus will navigate according to your needs

Cheers,
Dian
 
That would probably be the easiest Dian. Essentially, I have a page whene the user fills out information - First Name, Last Name, Address, City, State, etc... And originally I threw in the nextFocusableComponent() so they could tab through it, and effectively save time. I downloaded Update 13, and it was throwing a warning when running or compiling, telling me that the nextFocusableComponent() was being deprecated. I did some reseach regarding that issue, and it essentially pointed to writing a Focus Traversal Policy, and then I got confused, basically I was finding the whole thing to be a little beyond my abilities.

Now by adding the elements to the container, it would essentially figure out it's own traversal? Or am I missing something here?

By the way, thank you both for the help thus far!

Beware of hackers bearing executables. Happy Hunting. 'irc.2600.net'
 
Now by adding the elements to the container, it would essentially figure out it's own traversal? Or am I missing something here?
You are correct here.

If you want to use FocusTraversalPolicy, you may extends FocusTraversalPolicy and using the code
Code:
yourJFrame.setFocusTraversalPolicy(yourExtendedPolicy),
yourJPanel.setFocusTraversalPolicy(yourExtendedPolicy) or 
yourJComponent.setFocusTraversalPolicy(yourExtendedPolicy)

You may write more program with Swing, you will be more familiar with Swing.

You may sometimes need to read the source code of some JComponents so that you can know what you need to write exactly when you are extending that class.

Just to remind that the source code file is called src.zip and you can find it in your JDK directory
 
I use focus management when I need to do something "special", ie, setting the focus in a specific component when somethings happens.

If the behaviour is the "traditional", ie, the users fill in the fields from beginning to end, the Tab will work just by default, no need for focus management

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top