Results 1 to 6 of 6

Thread: What is the use of FocusTraversalPolicy class?

  1. #1
    Join Date
    Jan 2010
    Posts
    112

    What is the use of FocusTraversalPolicy class?

    Hello friends,

    I need your help to sort out my query related to the advanced java classes. I want to be aware about FocusTraversalPolicy class of java. I don't have any idea about the use FocusTraversalPolicy class. The methods of this class are also strange for me. If you have sound knowledge about the FocusTraversalPolicy class of java, then please let me know that. If possible then also give proper example for FocusTraversalPolicy class. I am waiting for your reply.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What is the use of FocusTraversalPolicy class?

    A FocusTraversalPolicy defines the order in which Components with a particular focus cycle root are traversed. Instances can apply the policy to arbitrary focus cycle roots, allowing themselves to be shared across Containers. They do not need to be reinitialized when the focus cycle roots of a Component hierarchy change. The core responsibility of a FocusTraversalPolicy is to provide algorithms determining the next and previous Components to focus when traversing forward or backward in a UI. Each FocusTraversalPolicy must also provide algorithms for determining the first, last, and default Components in a traversal cycle.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: What is the use of FocusTraversalPolicy class?

    Following java example will show you the use of FocusTraversalPolicy class:
    Code:
    import java.awt.FocusTraversalPolicy;
    import javax.swing.SortingFocusTraversalPolicy;
    
    public class FocusTDemo{
    
      public static void main(String argus[]) {
        JFrame FNT = new JFrame("Reverse Sample");
        FNT.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        FNT.setLayout(new GridLayout(3, 3));
    
        for (int i1 = 9; i1 > 0; i1--) {
          JButton button1 = new JButton(Integer.toString(i));
          FNT.add(button1, 0);
        }
    
        final Container contentPane1 = FNT.getContentPane();
        Comparator<Component> comp1 = new Comparator<Component>() {
          public int compare(Component c1, Component c2) {
            Component comps[] = contentPane1.getComponents();
            List listG = Arrays.asList(comps);
            int first1 = listG.indexOf(c1);
            int second1 = listG.indexOf(c2);
            return second1 - firs1t;
          }
        };
        FocusTraversalPolicy policy = new SortingFocusTraversalPolicy(comp1);
        FNT.setFocusTraversalPolicy(policy);
    
        FNT.setSize(300, 200);
        FNT.setVisible(true);
      }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: What is the use of FocusTraversalPolicy class?

    Hi,

    Before using properties of FocusTraversalPolicy class, you need to be aware about following it's methods:
    • getLastComponent(Container focusCycleRt)
    • getInitialComponent(Window window)
    • getFirstComponent(Container focusCycleRt)
    • getDefaultComponent(Container focusCycleRt)
    • getComponentBefore(Container focusCycleRt, Component Comp)

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: What is the use of FocusTraversalPolicy class?

    Hi friend,

    To know about the use of FocusTraversalPolicy class, I recommend you to review following java code:
    Code:
    import java.awt.FocusTraversalPolicy;
    import javax.swing.JComponent;
    
    public class FocusTraversalPolicyDM {
      public static Component smartRequestFocus(Component comp) {
        if (requestFocus(comp))
          return comp;
        if (comp instanceof JComponent) {
          FocusTraversalPolicy policy = ((JComponent) comp).getFocusTraversalPolicy();
    
          if (policy != null) {
            Component focusComponent = policy.getDefaultComponent((Container) comp);
    
            if (focusComponent != null && requestFocus(focusComponent)) {
              return focusComponent;
            }      }
        }
        if (comp instanceof Container) {
          Component[] children = ((Container) comp).getComponents();
    
          for (int ik = 0; ik < children.length; ik++) {
            comp = smartRequestFocus(children[i]);
    
            if (comp != null)
              return comp;
          }    }
    
        return null;
      }
    
      public static boolean requestFocus(Component comp) {
        return KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == comp ||
               comp.requestFocusInWindow();
      }
    }

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What is the use of FocusTraversalPolicy class?

    A FocusTraversalPolicy class comprises only one constructor known as 'FocusTraversalPolicy()'. A FocusTraversalPolicy class is taken from the 'java.awt.FocusTraversalPolicy' package. Which is subpart of 'java.lang.Object' package of java programming. Thre are two subclasses available with FocusTraversalPolicy class, which are ContainerOrderFocusTraversalPolicy and InternalFrameFocusTraversalPolicy. You can access properties of this java class using it's various methods.

Similar Threads

  1. Replies: 8
    Last Post: 08-10-2011, 11:06 PM
  2. What is the difference between Local class and global class in C++?
    By Dëfrim in forum Software Development
    Replies: 4
    Last Post: 03-01-2011, 10:44 PM
  3. Replies: 5
    Last Post: 12-02-2010, 06:23 PM
  4. Ultra solid drives:Imation M-Class and S-Class
    By Regina in forum Portable Devices
    Replies: 1
    Last Post: 03-04-2009, 10:34 AM
  5. Good news for CBSE CLASS X & CLASS IX - visit learnnext
    By surya_expert in forum Education Career and Job Discussions
    Replies: 0
    Last Post: 13-12-2008, 12:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,149,561.66487 seconds with 16 queries