Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2006

Tip: Looking for answers? Try searching our database.

FocusTraversalPolicy

Thread view: 
Big Jim - 27 Jan 2006 00:30 GMT
Has anyone written a decent one?

I had an app that worked fine in 1.3, in 1.4 the focussing is a disaster.
I have JDesktop panes, JIternalPanes and JTabbed panes containing JTables,
JComboBoxes, JTextFields etc.

Using ContainerOrderFocusPolicy I can't get initial focus to work, it's take
2 or 3 tabs sometimes to move from one component to the next etc. I guess
it's stepping out to parent panes and then back or something even though
I've set everything to focusable(false) except the items I want to land on.
I wrote a pretty basic policy but it's not handling compound components
properly (a JComboBox I've made AutoCompletable) and I haven't covered any
of the up/down cycle stuff or checked for no enabled components. Does anyone
have anything more complete? (I'm no swing expert)

Cheers, Richard.

from memory ...

import java.awt.*;
import java.util.*;

public class MyTraversalPolicy extends FocusTraversalPolicy {
private Map map;
private java.util.List components;
private int currentKey;

public MyTraversalPolicy(){
 map = new IdentityHashMap();
 currentKey = 0;
 components = new ArrayList();
}

public void add(Component c){
 map.put(c, new Integer(currentKey++));
 components.add(c);
}

public Component getDefaultComponent(Container focusCycleRoot) {
 return getFirstComponent(focusCycleRoot); }

public Component getFirstComponent(Container focusCycleRoot) {
 Component c = components.get(0);
 return c.isEnabled() ? c : getComponentAfter(focusCycleRoot, c);
}

public Component getLastComponent(Container focusCycleRoot) {
 Component c = components.get(components.size() - 1);
 return c.isEnabled() ? c : getComponentBefore(focusCycleRoot, c);
}

public Component getComponentAfter(Container focusCycleRoot, Component
aComponent) {
 int key = ((Integer)map.get(aComponent)).intValue();
 if(key == components.size() - 1){
  return getFirstComponent(focusCycleRoot);
 }
 Component c = components.get(key + 1);
 return c.isEnabled() ? c : getComponentAfter(focusCycleRoot, c);
}

public Component getComponentBefore(Container focusCycleRoot, Component
aComponent) {
 int key = ((Integer)map.get(aComponent)).intValue();
 if(key == 0){
  return getLastComponent(focusCycleRoot);
}
 Component c = components.get(key - 1);
 return c.isEnabled() ? c : getComponentBefore(focusCycleRoot, c);
}
}
Michael Dunn - 28 Jan 2006 00:18 GMT
> Has anyone written a decent one?
>
[quoted text clipped - 11 lines]
>
> Cheers, Richard.

some of the code in this thread might suit you

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=2&t=013092
Big Jim - 28 Jan 2006 21:17 GMT
>> Has anyone written a decent one?
>>
[quoted text clipped - 17 lines]
>
> http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=2&t=013092

cheers Michael, I got some tips from that
Kleopatra - 30 Jan 2006 17:12 GMT
> Has anyone written a decent one?
>
> I had an app that worked fine in 1.3, in 1.4 the focussing is a disaster.

yeah, the focus architecture did change considerably between 1.3/1.4.
Implementing a decent FTP is not trivial: the usual examples (including
the Sun tutorial one or the one in the javaranch thread) shown are
very near to useless in the general case, because they
"forget" that they are responsible for _every_ component in
and _below_ the focusCycle until they hit another focusCycleRoot. To see
the problem, replace one of the simple comps in the examples like a
textfield or button with a compound component like f.i. an editable
combobox.

Don't have anything handy at the moment, but remember to have gotten the
best results when starting with one the pre-implemented FTPs instead of
re-implementing from scratch. F.i. you might start with the SortingFTP
and replace the comparator and/or the accept() to steer the order of
traversal and/or exclusions. Some years ago there was an example along
those lines in the Sun's tech tips - googling should bring it up.

Whatever you try I think reading and understanding the article about the
focus architecture that comes with the jdk is a must - unfortunately
it's hard reading, very densely formulated so that nearly every word is
important. Plus don't forget to browse any FTP-related bugs in Sun's Bug
Parade.

Cheers
Jeanette
Big Jim - 30 Jan 2006 20:06 GMT
>> Has anyone written a decent one?
>>
[quoted text clipped - 25 lines]
> Cheers
> Jeanette

Thanks Jeanette, I've been having a go at that. Not sure I totally
understand the spec. I'll do a bit more investigation and maybe get back
with a few specific questions.

Cheers, Richard.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.