Results 1 to 7 of 7
Thread: FocusTraversalPolicy
- 07-22-2010, 03:19 AM #1
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)
-
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.
- 07-22-2010, 03:50 AM #3
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)
- 07-22-2010, 03:59 AM #4
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)
- 07-22-2010, 04:31 AM #5
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)
- 07-22-2010, 06:17 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.right to go right (equivalent to tab), and left to go left (equivalent to shift+tab).
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.Myself I'd use key binding and a 2D array of your component requiring the focus.
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.
You can't have two focus traversal policies.Now I have to implement the traversal policy on the up and down arrow keys and I don't know how to do that.
- 07-22-2010, 03:28 PM #7
Wow I can't believe I missed that.
:( So focus traversal policy is not the way?
: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().
... :(
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)


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks