• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Focusing Radio Button

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've the following scenario:

TextField1
TextField2
Radio buttons1 Radio buttons2 Radio buttons3
Button1

I've FocusListeners set on the text fields. The event I'm interested is focusLost. While lossing focus from textfield, based on some condition, I'm enabling or disabling all radio buttons. Some condition is that, if there is some text in both the text fields enable radio buttons, otherwise disable them.

I'm entering some text in TextField1, tabing out, entering some text in TextField2, If I tab out now, I want the radio buttons to be enabled and the focus should be on the radio button.
But the actual result I'm getting is radio buttons are enabled and the focus goes to the next component - Button1!
I don't know to fix it.

I simplified the problem to understand. The actual code looks like

private class MyFocusAdapter extends FocusAdapter
{
public final void focusLost(FocusEvent e)
{
doSomething();
enableDisableRadioButtons();
doSomeOtherThing();
}
}
private void enableDisableRadioButtons()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
radioB1.setEnabled(shouldEnableRadioButtons());
radioB2.setEnabled(shouldEnableRadioButtons());
}
}


The problem is focus path is not working fine.

The reason I guess is when we tab out from second text field after entering some text there, it should enable the radio buttons, then it should focus the selected radio buton. I guess this is not happening in the same order. At the time of tab out from second text field, as the radio buttons are disabled, the focus goes to the next component - the button. Then the radio buttons gets enabled. I'm not sure. The problem comes only when the radio buttons are disabled. If the radio buttons are already enabled, the the focus path is fine.

Someone suggested to use document listener instead of focus listener.

Previously we had the enable/diable functionality in the keyReleased event of the textfields. So it was working fine earlier.
Later the requirement is changed to do the same in focus lost event. The reason is there will be large number of call to enable/disable that is for each key release event. The same thing will happen if I use document listener! Any idea please!

Sorry for long message!

Thanks
Anu
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your focusLost method, check to see which textfield you are in. If you are in the second textfield, requestFocus on the desired component (JRadioButton in your case)
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic