• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Changing focus when going from a text field to a menu

 
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran into this problem is some code I am trying to extend.

There is a text field in the GUI and the goal was that when a user switched to a different panel in the GUI, an observable would be updated.

What was done was to put a focus listener on the text field



That works most of the time. If you select things like text fields and combo boxes, no problem.

However, the GUI has a menu bar. If I click on an item in the menubar, I see focus shift off the text field. But it goes right back to the text field once the menu action is completed.

I tried running either menubar.requestFocus() or menubar.requestFocusInWindow(), but the focus still goes back to the text field when the menu action is completed.

I tried adding


or
             
after setting the observable in the focusLost method. The focus still returns to the text field.

The same problem happens for righ-click menus attached to graphs and tables.
This is a problem because one of the observers of that observable now spends a lot of time processing the event from the observer. Once is enough!

Is there some standard method I should used to switch/remove focus from that text field?
 
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want the focus to be on a menu item after it is clicked or what?
 
Jon Swanson
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The goal was to run a method when the focus shifted off the test field. But I only want to run the method once. I thought that when I switched to another component the focus would be lost from the text field and the only way the test field would regain focus is if I clicked in it again.

Apparently this is true if I click in a different text field. That gets the focus and the method runs once.

When I click on a menu item in the menu bar, the focus is 'lost' and the method runs. Yet the text field gets the focus back. If I have a look-aside on the menu, the method runs once when I first bring up the menu (text field loses focus, then gets it back) and then runs again when I go to the look aside menu (text field loses focus and gets it back).

What I would like is some approach that once the text field has lost focus it stays lost until I enter the text field again.

And as I said, the only time I have a problem is when going to a menu item.

I tried changing the focus to the menu bar, that did not work. I tried what I thought would advance the focus to the next item in the focus hierarchy, that did not seem to work, and I tried to just clear the focus and that did not work.
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason you have to run the method when the textbox loses focus?  If you really need to do it like thata be you could just check if the textbox has input or whatever you're looking for.
 
Jon Swanson
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is what is now an expensive calculation that needs to run when the text field value changes.

Sitting on the text field and updating every keystroke is too expensive (numbers are being input, so there is no obvious way of knowing the entry is not complete)

The client rejected requiring a button to indicate the entry was complete
The client rejected requiring the Enter key be pressed to indicate the entry was complete

The 'requirement' is that the method run when the user goes to the next panel (or tab in the current panel) or any other UI element where next could be anywhere in the UI, the workflow is not sequential.

My implementation of running the method when the text field lost focus seemed to be sufficient.

Then new functionality was added and put a couple levels deep in the menu bar menu. Now the method is running multiple times and actually interferes with the new functionality, which assumes the method has completed.

So either I need a different method to determine that the user is done entering data (and running the method) or I have to understand better how to shift the focus when the user clicks anywhere in the UI (and not just most places).

 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why they don't want to press enter after entering the data considering it's a long running process.  I think it might be easier to convince them to let you use enter.  
How is it interfering?
If they are typing numbers then pushing the enter key which is right next to the numbers doesn't seem inconvenient.  I can't imagine they have a good reason for not pressing enter to indicate the number is complete.  
 
Jon Swanson
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turns out there was one case in the UI where you did need to press enter in a text field. There is a spinner where you can click the arrows to increment/decrement or enter the value directly. I did not have a listener checking for loss of focus there, so you had to press Enter. Once that was noticed it was treated as a huge bug that had to be immediately fixed.

What I don't understand is what is special when I am clicking on a menu item in the menu bar that causes focus in a text field to be lost and then regained. If the focus never left the text field, that would not have been too surprising, but once focus is lost, returning it to the text field seems a bit odd. In fact, if I click on a different part of the menu bar, where this is no menu item, focus is lost from the text field, end of story.
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't  you just check the input before you start the long process?  Does the text field never clear?  Still don't understand why pressing enter is a bug....
Probably snice the menu item is hidden  in the menu it cant keep the focus so it goes back to the last focused thing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!