wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Focus: cant find the relevant method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Focus: cant find the relevant method" Watch "Focus: cant find the relevant method" New topic
Author

Focus: cant find the relevant method

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i've this application with several tabs
to one of them i wrote a custom FocusTraversalPolicy class
the idea was pressing the tab key to move across several text fields, what i got done (forward and backwards too)
now i wanted to have the first text field with the cursor in it, so that i could start writing without having to click it first

i tried several ideas but without success:

also, i tried using the method getInitialComponent:

i just cant see how i can solve this
can anyone please help me?

thaks in advance


java amateur
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2154
    
    7
Sounds like Tabbed Pane Focus will solve your problem.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:Sounds like Tabbed Pane Focus will solve your problem.

thanks for the help, Rob
i'm feeling a bit dumb ...

your class indeed "remembers" which comonent had a focus, but doesnt set the focus to my textfield
i tried:

with no results

maybe i'm not getting the main idea
can you further help, please?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2154
    
    7
There is no need to use the addException(...) method. Other than that I have no idea why it doesn't work for you.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
the example bellow ilustrates just one point: no cursor inside first textfield when tab is opened (so i cant just start typping inside the mentioned textfield...)

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2154
    
    7
That is not a SSCCE.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:That is not a SSCCE.

hmmm ... hope you enjoy this one
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2154
    
    7
That is not a SSCCE. Read the link provided.

As I understand the question you are complaining that the test field on the tab does not have focus when you switch tabs. So create a simple demo with a tabed pane with two tabs. Add two text fields to each tab. Thats it, thats all you need.

The custom focus traversal policy is not relevant. That is first prove the code works with the custom policy. Then you add back in the policy to see if it still works. That is how you narrow down problems. Start with simple before making custom changes. The whole SSCCE (not including the class I suggested you try) will probably only be about 20-30 lines of code. I'm not going to read through 200 lines of code to debug your problem.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
let me see if i got what you suggest:
first, without my class policy, but with your class
then, with both

is that it?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2154
    
    7
Well, the first step is a simple demo with no custom code, just a couple of tabs with a couple of test fields to see if focus works properly.

I claim it works properly as demonstrated by the demo below. When the program starts up focus is on the first tab. Then you can use the Tab key or click on the text field. Now when you click on a tab, the focus will always be on the first text field, proving that the default behavour works.

Here is a proper SSCCE:



Then for some reason you think you need to write a custom FocusTraversalPolicy. So try adding that code. If it doesn't work then you know the problem is with your custom code. Start simple, make one change at a time until it stops working.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:Well, the first step is a simple demo with no custom code, just a couple of tabs with a couple of test fields to see if focus works properly.

I claim it works properly as demonstrated by the demo below. When the program starts up focus is on the first tab. Then you can use the Tab key or click on the text field. Now when you click on a tab, the focus will always be on the first text field, proving that the default behavour works.

(...)
Then for some reason you think you need to write a custom FocusTraversalPolicy. So try adding that code. If it doesn't work then you know the problem is with your custom code. Start simple, make one change at a time until it stops working.

i get your point, but mine is this:
my panels have lots of components
using tab and ctr+tab keys with my custom focus policy work perfectly, except for the cursor on the first textfield, hence the reason of this topic
if you remember i didnt blame the code - its correct - but instead asked for some method to force the prompt inside my first element...

ok, too late here, i'll come back tomorrow
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
if you add a button anywhere you fall in my case - no cursor inside textfield!:
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2154
    
    7
> using tab and ctr+tab keys with my custom focus policy work perfectly, except for the cursor on the first textfield, hence the reason of this topic

Did the custom FocusTraversalPolicy cause the problem? If not then why mention it, that information is NOT relevant to the problem so don't confuse us with unnecessary information. That is why you create the SSCCE to demonstrate the problem.

What does the button have to do with anything? In the original code I gave you focus is on the "tab", not the text field on the tab, so I don't understand why you added the button.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
What does the button have to do with anything? In the original code I gave you focus is on the "tab", not the text field on the tab, so I don't understand why you added the button.

i added the button because you said:
Now when you click on a tab, the focus will always be on the first text field, proving that the default behavour works.


i guess we are losing perspective: what is really needed is not finding an error - i guess there isnt one - but grabbing a way to force focus on the textfield

one conclusion i can draw is that default behaviour vanishes the moment more components are added (my case)
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2154
    
    7
one conclusion i can draw is that default behaviour vanishes the moment more components are added (my case)


No, that is NOT true. Focus was not of the text field in my example either. Focus was on the "Tab". That is the default behaviour. So adding the button did NOT break the default behaviour.

The reason the WindowListener didn't work is because you didn't build the GUI in the EDT, which can cause unexpected problems.

So here is the proper SSCCE, if I now understand your requirement:


miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i'm getting puzzled and very tired, sorry

what means EDT?

i'm trying hard with your last code and have some doubts - maybe you can help (again)

so, i guess i'm getting back to start point: am i correct with those indices? - i mean the 2nd refers to the TAB and the 3rd to the components inside my panel, correct?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2154
    
    7
what means EDT?


Event Dispatch Thread. Read the section from the Swing tutorial on Concurrency

i mean the 2nd refers to the TAB and the 3rd to the components inside my panel, correct?


Why are you using "11" as the component? My SSCCE does not have 11 components. Have I not taught you anything about using a SSCCE to test new concepts? I gave you a simple SSCCE to us for you test cases. Why are you plugging the code into your real program before testing it on a SSCCE. You don't know if the problem is your code or the base code.

Yes, but you can't just request focus on the component because the tab is not visible. You need to do:

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
after sleeping a few hours i'm back and trying to be objective and concise
  • my requirement:
    this application has several tabs
    the second tab has several components, among which there is this textfield
    the application should always open with the first tab selected
    when one cliks the second tab the mentioned textfield should have focus
  • departure point: Rob's code

  • report: all goes accordingly with Rob's plain code

  • first increment: adding a button before the textfields

  • report: as expected is the button the one to grab focus

  • second increment: altering the values inside window listener, as comments show:
  • report: if i want first tab to have focus on 2nd element (txtfield), i get it

  • third increment: trying to grab focus on textfield from 2nd tab; the used changes are written in the code

  • report: if i want the same behaviour on 2nd tab, i just cant

    so i'm back from where i started: cant figure how to make - in this case - having TAB_1 displayed when one opens the app and, at the same time, seeing the textfield grabing focus when switching to the 2nd TAB

    hope i made myself clear

    the code:
    Rob Camick
    Ranch Hand

    Joined: Jun 13, 2009
    Posts: 2154
        
        7
    Yes you have a problem, you can't set the default field for every tab up front. You you can add a ChangeListener. When you the user clicks on tab2, you manually set focus to the component you want.
    miguel lisboa
    Ranch Hand

    Joined: Feb 08, 2004
    Posts: 1281
    at last: case solved!


    thanks a lot, Rob
     
     
    subject: Focus: cant find the relevant method
     
    Similar Threads
    first()' method of ResultSetMetaData clas
    Focus problem
    JTable, JTextField and focus
    Tabbing bewteen textfields
    how to manage class relationship?