• Post Reply Bookmark Topic Watch Topic
  • New Topic

Focus traversal issue  RSS feed

 
Nadin Jovan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new to swing and am trying to understand how the new 1.4 focus framework works. Please help.
I have a JPanel that contains 9 components (in order of addition JTextField hours, JTextField minutes, JTextField seconds, JRadioButton countUp, JRadioButton countDown, JButton start, JButton pause, JButton stop, JButton new).
JButtons start, pause, stop and new are not active all the time. For example, buttons pause and stop are not active initially until the start button is pressed, than they become active and start button is deactivated.
The initially focused component in this window is hours. When I tab through the components everything works just fine (I can see that getComponentAfter in my focus traversal policy is being called) until I get to the start button (which is active). The next component should be pause button based on my focus traversal policy, but since it is inactive it is skipped. Moreover, it seems that the getComponentAfter in never even called. Same happens with stop button. The focus is sent to the new button, which is correct but from here on the focus is not sent to the hours field but to the panel itself (again getComponentAfter is not called). All this makes me think that after the inactive buttons were encountered somehow my focus traversal policy is not being used anymore. Can anyone tell me what I am doing wrong?
Here is the code for my focus traversal policy.
class LocalFocusTraversalPolicy extends FocusTraversalPolicy {

private Component[] _components = null;

public LocalFocusTraversalPolicy() {
_components = new Component[9];
_components[0] = durationPN.getHoursTextField();
_components[1] = durationPN.getMinutesTextField();
_components[2] = durationPN.getSecondsTextField();
_components[3] = dynamicTimerPN.countsUpRB;
_components[4] = dynamicTimerPN.countsDownRB;
_components[5] = getButton(1); //Start
_components[6] = getButton(2); //Pause
_components[7] = getButton(3); //Stop
_components[8] = getButton(4); //New

}

public Component getComponentAfter(Container focusCycleRoot,
Component aComponent) {
for (int i=0; i<_components.length; i++) {
if (aComponent == _components[i]) {
int j = (i+1) % _components.length;
return _components[j];
}
}
return null;
}

public Component getComponentBefore(Container focusCycleRoot,
Component aComponent) {
for (int i=0; i<_components.length; i++) {
if (aComponent == _components[i]) {
int j = (i-1+_components.length) % _components.length;
return _components[j];
}
}
return null;
}

public Component getDefaultComponent(Container focusCycleRoot) {
return durationPN.getHoursTextField();
}

public Component getFirstComponent(Container focusCycleRoot) {
return durationPN.getHoursTextField();
}

public Component getInitialComponent(Window window) {
return durationPN.getHoursTextField();
}

public Component getLastComponent(Container focusCycleRoot) {
return getButton(4);
}

}
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your UI code also.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!