I have one doubt related to tab order. I have written a class that implements FocusTraversalPolicy. Now when my focus moves over the disabled component then getComponentAfter method of the FocusTraversalPolicy class is not getting called and hence next components doesnt receive any focus neither by pressing TAB nor by pressing SHIFT+TAB once the focus is on disabled component.
In case of enabled components everything works perfectly fine.
I need to move the tab/focus on the disabled components too. Can anyone please help me in this regards ? Is it possible to set focus on disabled component although it doesnt makes sense to focus on disabled components ?
Waiting for your suggestions
Thanks and Regards Rohit. [ January 19, 2006: Message edited by: Rohit Bhagwat ]
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
this skips the disabled components, if that is what you're after
needs additional code in getDefaultComponent() getFirstComponent() getLastComponent() if any are ever likely to be disabled.
and if firstComponent can be disabled, int focusNumber = 0; will need to be modified
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
posted
0
I'm not sure how that's going to work Michael. Doesn't the number always get reset back to 0 since you're using %. Personally I use this algorithm I came up with:
It will skip components that are disabled and not focusable and will wrap around to the beginning. If it gets back to where it started it breaks and returns null to indicate there is no suitable component. I went on memory and didn't check the code very thoroughly so their may be a typo.
EDIT: Disabled smilies. [ January 19, 2006: Message edited by: Ken Blair ]
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
> Doesn't the number always get reset back to 0 since you're using %
it does, but the if() on the following line checks whether focusList[0] is enabled if not, componentAfter/Before is re-called and focusNumber becomes 1/(len-1)
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
posted
0
Originally posted by Michael Dunn: > Doesn't the number always get reset back to 0 since you're using %
it does, but the if() on the following line checks whether focusList[0] is enabled if not, componentAfter/Before is re-called and focusNumber becomes 1/(len-1)
So then it gets reset back to 0 again. This looks like an infinite loop.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
it works on this focusNumber = (focusNumber+1) % focusList.length;
where focusNumber is a class variable if the % sets focusNumber to 0, and focusList[0] is disabled, it loops and focusNumer becomes 1 etc
Rohit Bhagwat
Ranch Hand
Joined: Dec 19, 2004
Posts: 205
posted
0
Thanks all for the reply.
However my question is little bit different. Actually the focus should go on next component no matter it is enabled or not. If the component is disabled still the focus should go on disabled component and should not skip the disabled component. What I mean by this is pressing the tab should go on the disabled component however I should not show the line that appears around the component i.e I will not call requestFocus. The effect will be like you press the tab but you wonder where the focus is !! Here's a sample example of what exactly should happen 1. Pressing tab moves the focus on enabled compoenent. 2. Again pressing the tab moves the focus on disabled component however I dont see any line around the disabled component so the effect is you dont know where the focus is. 3. Again pressing the tab moves the focus on enabled component.
The problem I am facing is once the focus is on disabled component then getComponentAfter method is not called.
Waiting for your replies..
Thanks and Regards Rohit
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
see if this gets you any closer to what you want.
Rohit Bhagwat
Ranch Hand
Joined: Dec 19, 2004
Posts: 205
posted
0
Thanks a lot for the reply. Now I can see that the focus is on the disabled component and traversing is working in either direction but I should not be able to see the line which indicates that now the focus is on the diabled component.
Waiting for your reply
Thanks and Regards Rohit
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
create a method to loop through focusList, and if the component is an instance of javax.swing.AbstractButton use setFocusPainted(boolean) to set or remove the focus line.
call the method whenever you disable/re-enable a component.
Rohit Bhagwat
Ranch Hand
Joined: Dec 19, 2004
Posts: 205
posted
0
But then I understand that this method is only in AbstractButton. Actually I have other JComponents for which I require the same functionality.
Thanks and Regards Rohit.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
check the api docs for the other components, and look for the method that handles the focus indication, and add code to handle this in the new method mentioned in my previous reply.
Rohit Bhagwat
Ranch Hand
Joined: Dec 19, 2004
Posts: 205
posted
0
Originally posted by Michael Dunn: check the api docs for the other components, and look for the method that handles the focus indication, and add code to handle this in the new method mentioned in my previous reply.
Thanks for your help Michael...You have helped me a lot..Thanks very much
Thanks and Regards Rohit.
Syed Muhammad Mubashir Ahmed
Greenhorn
Joined: May 20, 2012
Posts: 2
posted
0
Dear Michaael when I am using this class for the jtable then it is not working that i am not getting Focus out of the jtable to the next or previous component.
Thanks in advance for any help
Syed Muhammad Mubashir Ahmed wrote:Dear Michaael when I am using this class for the jtable then it is not working that i am not getting Focus out of the jtable to the next or previous component.
Thanks in advance for any help
Syed, you already started a thread for this, and you haven't replied to my response posted there nearly an hour before you bumped this 6 year old thread.
luck, db
There are no new questions, but there may be new answers.
Doug Sorensen
Greenhorn
Joined: Feb 20, 2013
Posts: 1
posted
0
When using Swing with the Netbeans designer, each "focusable" dialog control can set the "nextFocusableComponent" property (with no programming required).
Doug Sorensen wrote:When using Swing with the Netbeans designer, each "focusable" dialog control can set the "nextFocusableComponent" property (with no programming required).
Maybe so, but using a visual designer / code generator without understanding the code it produces isn't a good idea at all.