It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Tab order on swing components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Tab order on swing components" Watch "Tab order on swing components" New topic
Author

Tab order on swing components

Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Hello sir, friends

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
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
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
> 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
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
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
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
see if this gets you any closer to what you want.

Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
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
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
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
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
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
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
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.

Please go through UseOneThreadPerQuestion <- link


luck, db
There are no new questions, but there may be new answers.
Doug Sorensen
Greenhorn

Joined: Feb 20, 2013
Posts: 1
When using Swing with the Netbeans designer, each "focusable" dialog control can set the "nextFocusableComponent" property (with no programming required).
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tab order on swing components
 
Similar Threads
Focus traversal issue
Focus Traversal Policy Question
Tabbing Out Of JEditorPane...
JTabbedPane - traversing focus through tabs
[URGENT]Trapping focus loosing of Editable JCombo box.