Focus Traversal Example : Focus « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » FocusScreenshots 
Focus Traversal Example
Focus Traversal Example


/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/

// FocusTraversalExample.java
//Similar to the FocusExample, this class uses the custom AlphaButtonPolicy
//focus traversal policy to navigate another small set of buttons.
//

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.awt.GridLayout;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FocusTraversalExample extends JPanel {

  public FocusTraversalExample() {
    setLayout(new GridLayout(61));
    JButton button1 = new JButton("Texas");
    JButton button2 = new JButton("Vermont");
    JButton button3 = new JButton("Florida");
    JButton button4 = new JButton("Alabama");
    JButton button5 = new JButton("Minnesota");
    JButton button6 = new JButton("California");

    setBackground(Color.lightGray);
    add(button1);
    add(button2);
    add(button3);
    add(button4);
    add(button5);
    add(button6);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("Alphabetized Button Focus Traversal");
    frame.setFocusTraversalPolicy(new AlphaButtonPolicy());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new FocusTraversalExample());
    frame.setSize(400300);
    frame.setVisible(true);
  }
}

//AlphaButtonPolicy.java
//A custom focus traversal policy that uses alphabetical ordering of button
//labels to determine "next" and "previous" buttons for keyboard traversal.
//

class AlphaButtonPolicy extends FocusTraversalPolicy {

  private SortedMap getSortedButtons(Container focusCycleRoot) {
    if (focusCycleRoot == null) {
      throw new IllegalArgumentException("focusCycleRoot can't be null");
    }
    SortedMap result = new TreeMap()// Will sort all buttons by text.
    sortRecursive(result, focusCycleRoot);
    return result;
  }

  private void sortRecursive(Map buttons, Container container) {
    for (int i = 0; i < container.getComponentCount(); i++) {
      Component c = container.getComponent(i);
      if (instanceof JButton) { // Found another button to sort.
        buttons.put(((JButtonc).getText(), c);
      }
      if (instanceof Container) { // Found a container to search.
        sortRecursive(buttons, (Containerc);
      }
    }
  }

  // The rest of the code implements the FocusTraversalPolicy interface.

  public Component getFirstComponent(Container focusCycleRoot) {
    SortedMap buttons = getSortedButtons(focusCycleRoot);
    if (buttons.isEmpty()) {
      return null;
    }
    return (Componentbuttons.get(buttons.firstKey());
  }

  public Component getLastComponent(Container focusCycleRoot) {
    SortedMap buttons = getSortedButtons(focusCycleRoot);
    if (buttons.isEmpty()) {
      return null;
    }
    return (Componentbuttons.get(buttons.lastKey());
  }

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

  public Component getComponentAfter(Container focusCycleRoot,
      Component aComponent) {
    if (!(aComponent instanceof JButton)) {
      return null;
    }
    SortedMap buttons = getSortedButtons(focusCycleRoot);
    // Find all buttons after the current one.
    String nextName = ((JButtonaComponent).getText() "\0";
    SortedMap nextButtons = buttons.tailMap(nextName);
    if (nextButtons.isEmpty()) { // Wrapped back to beginning
      if (!buttons.isEmpty()) {
        return (Componentbuttons.get(buttons.firstKey());
      }
      return null// Degenerate case of no buttons.
    }
    return (ComponentnextButtons.get(nextButtons.firstKey());
  }

  public Component getComponentBefore(Container focusCycleRoot,
      Component aComponent) {
    if (!(aComponent instanceof JButton)) {
      return null;
    }

    SortedMap buttons = getSortedButtons(focusCycleRoot);
    SortedMap prevButtons = // Find all buttons before this one.
    buttons.headMap(((JButtonaComponent).getText());
    if (prevButtons.isEmpty()) { // Wrapped back to end.
      if (!buttons.isEmpty()) {
        return (Componentbuttons.get(buttons.lastKey());
      }
      return null// Degenerate case of no buttons.
    }
    return (ComponentprevButtons.get(prevButtons.lastKey());
  }
}
           
       
Related examples in the same category
1. Focus Cycle SampleFocus Cycle Sample
2. Button FocusButton Focus
3. Focus ExampleFocus Example
4. Focus Concepts DemoFocus Concepts Demo
5. Track Focus Demo
6. Focus Traversal DemoFocus Traversal Demo
www_.___j___av___a2__s__._c__o_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.