Fous Traversal Policy

Discussion in 'Java' started by Big Jim, Jan 27, 2006.

  1. Big Jim

    Big Jim Guest

    Sorry to cross post but I've now noticed the gui group doesn't seem to gt
    much traffic.

    Has anyone written a decent one? (FocusTraversalPolicy that is)

    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);
    }
    }
     
    Big Jim, Jan 27, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wynan James
    Replies:
    1
    Views:
    1,588
    Miguel De Anda
    Oct 6, 2003
  2. Replies:
    0
    Views:
    422
  3. Jim Cobban
    Replies:
    0
    Views:
    414
    Jim Cobban
    Nov 15, 2003
  4. Xamalaek
    Replies:
    0
    Views:
    1,296
    Xamalaek
    Jun 29, 2004
  5. SpOiLeR
    Replies:
    13
    Views:
    604
    Clark S. Cox III
    Mar 4, 2005
Loading...

Share This Page