java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow Focus traversal based on alphabetical ordering of button labels
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3932
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Focus traversal based on alphabetical ordering of button labels E-mail
User Rating: / 9
PoorBest 

This Java tip illustrates a special way of traversing through buttons. Normally, the traversing is done with the help of 'tab' key on keyboard in the same order in which button were designed. This method make the traversing possible on the basis of alphabetical ordering of button labels.

import java.awt.*;
import java.util.*;
import javax.swing.*;

public 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(((JButton)c).getText(), c);
            }
            
            if (instanceof Container) {  // Found a container to search.
                sortRecursive(buttons, (Container)c);
            }
        }
    }
    
    // The rest of the code implements the FocusTraversalPolicy interface.
    
    public Component getFirstComponent(Container focusCycleRoot) {
        
        SortedMap buttons = getSortedButtons(focusCycleRoot);
        if (buttons.isEmpty()) { return null}
        
        return (Component)buttons.get(buttons.firstKey());
        
    }
    
    public Component getLastComponent(Container focusCycleRoot) {
        
        SortedMap buttons = getSortedButtons(focusCycleRoot);
        if (buttons.isEmpty()) { return null}
        
        return (Component)buttons.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 = ((JButton)aComponent).getText() "\0";
        SortedMap nextButtons = buttons.tailMap(nextName);
        
        if (nextButtons.isEmpty()) {  // Wrapped back to beginning
        
            if (!buttons.isEmpty()) {
                return (Component)buttons.get(buttons.firstKey());
            }
            
            return null;  // Degenerate case of no buttons.
        }
        
        return (Component)nextButtons.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(((JButton)aComponent).getText());
        
        if (prevButtons.isEmpty()) {  // Wrapped back to end.
            
            if (!buttons.isEmpty()) {
                return (Component)buttons.get(buttons.lastKey());
            }
            
            return null;  // Degenerate case of no buttons.
        }
        
        return (Component)prevButtons.get(prevButtons.lastKey());
    }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.