Results 1 to 7 of 7
  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default FocusTraversalPolicy

    I have 9 x 9 grid and I want to use the arrow keys to traverse through the board: up arrow to go up, down to go down, right to go right (equivalent to tab), and left to go left (equivalent to shift+tab). I was reading the Focus Subsystem and I stumbled upon the FocusTraversalPolicy. To do this, I would have to make two FocusTraversalPolicy: One to go up and down and another to go right and left. Is it possible for a container to have 2 FocusTraversalPolicy? If I were to do it using only one FocusTraversalPolicy, I would have to know what key is pressed. How would I let the FocusTraversalPolicy know what key is being pressed?

    With all that said, I have another question. Am I in the right path? Like would I be better off looking up Key Bindings or something? Thanks in advance!
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not pro at this, I'd wonder what Rob Camick has to say, but I've seen the focus traversal policy mainly used for tabbing. Myself I'd use key binding and a 2D array of your component requiring the focus.

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Hey! I'm one step ahead. The JTextFields are in a 2D array. :)
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    It goes up and down via Tab & Tab+Shift.

    My untested FocusTraversalPolicy:
    Java Code:
    ....
    	
    	private class SudokuFocusTraversalPolicy extends FocusTraversalPolicy {
    
    		public Component getComponentAfter(Container aContainer, Component aComponent) {
    			int xCoordinate = getIndexOf(aComponent,"x");
    			int yCoordinate = getIndexOf(aComponent,"y");
    			if (yCoordinate == 8)
    				return grid[0][xCoordinate];
    			else
    				return grid[yCoordinate+1][xCoordinate];
    		}
    
    		public Component getComponentBefore(Container aContainer, Component aComponent) {
    			int xCoordinate = getIndexOf(aComponent,"x");
    			int yCoordinate = getIndexOf(aComponent,"y");
    			if (yCoordinate == 0)
    				return grid[8][xCoordinate];
    			else
    				return grid[yCoordinate-1][xCoordinate];
    		}
    
    		public Component getDefaultComponent(Container aContainer) {
    			return grid[0][0];
    		}
    
    		public Component getFirstComponent(Container aContainer) {
    			return grid[0][0];
    		}
    
    		public Component getLastComponent(Container aContainer) {
    			return grid[8][8];
    		}
    		/**
    		 * 
    		 * @param aComponent
    		 * @param coordinate Can be only "x" or "y". "x" will return the column 
    		 * 		number of the component. "y" will return the row number of the
    		 * 		component.
    		 * @return Either the column or row number of the component.
    		 */
    		private int getIndexOf(Component aComponent, String coordinate) {
    			for (int y = 0; y < 9; y++)
    				for (int x = 0; x < 9; x++)
    					if (aComponent == grid[y][x])
    						if (coordinate.equalsIgnoreCase("x"))
    							return x;
    						else
    							return y;
    			return -1;
    		}
    	}
    }
    Last edited by Lil_Aziz1; 07-22-2010 at 04:44 AM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Tested and it works. Now I have to implement the traversal policy on the up and down arrow keys and I don't know how to do that. :(
    Last edited by Lil_Aziz1; 07-22-2010 at 04:46 AM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    right to go right (equivalent to tab), and left to go left (equivalent to shift+tab).
    FYI, read the section from the Swing tutorial on How to Use the Focus Subsystem. It will show you how to add the rightl/left keys to the existing focus traversal policy.

    Myself I'd use key binding and a 2D array of your component requiring the focus.
    Ditto. Since you need to create Key Bindings for the up/down keys you may want to be consistent and do this for the right/left keys as well.

    I don't know that you need a separate 2D array for this though. You can just get the parent panel of the text field and then use the getComponents() method to get an array of all the text fields.

    Now I have to implement the traversal policy on the up and down arrow keys and I don't know how to do that.
    You can't have two focus traversal policies.

  7. #7
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Quote Originally Posted by camickr View Post
    FYI, read the section from the Swing tutorial on How to Use the Focus Subsystem. It will show you how to add the rightl/left keys to the existing focus traversal policy.
    Wow I can't believe I missed that.

    Quote Originally Posted by camickr View Post
    Ditto. Since you need to create Key Bindings for the up/down keys you may want to be consistent and do this for the right/left keys as well.
    :( So focus traversal policy is not the way?

    Quote Originally Posted by camickr View Post
    I don't know that you need a separate 2D array for this though. You can just get the parent panel of the text field and then use the getComponents() method to get an array of all the text fields.
    :O! Didn't know that. In my SudokuGrid (which extends JPanel), I have a JTextField[][] getGrid() method. If I find that a 1D array will suffice when writing the algorithm, I will make sure to use getComponents().

    Quote Originally Posted by camickr View Post
    You can't have two focus traversal policies.
    ... :(
    As Dan Standford once put it, "Experience is what you get when you don't get what you want."
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •