• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPanel - RequestFocus on JTextField

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My logon class extends from Jpanel and it is set on JSplitPane, using setRightComponent(new Logon())
First time When I set the logon panel, blinking cursor appears on the user name JTextField as desired.
In my logoff menu item function, I create a new Logon Panel and set as the right componenet to the SplitPane. This time the JTextField DOES NOT have the cursor blinking.
In the initialize() method on the LogonPanel I use userTextField.requestfoucsInWindow();
I understand that if it is a JFrame, I could add WindowListener and implement the windowOpened() method.
Any ideas?
Thanks
Sri
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use the componentShown() method of ComponentListener to call the requestFocus() method...

Then whenever your panel becomes shown, it will request focus for the text field.
 
Sri Rangan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried adding ComponentListener to the JPanel by implementing componentshown(). It does not work and this method is never fired. should I have to do differently?
As I said, I am creating a new JPanel and setting as a rightComponenet to the JSplitPane
Thanks
Sri
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use the AncestorListener to listen to when the JPanel has been added to its parent. after this event was triggered, requesting the focus works.
chantal
 
Sri Rangan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried add the following in the Logon Panel initialize method. But did not work. Am I doing some thing wrong?
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
try requestFocus().
chantal
 
Sri Rangan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mmmm. Tried requestFocus(). Still does't work.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create a separate Thread to run it, and then invoke SwingUtilities.invokeLater.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what jvm version are you using?
the java bugparade lists problems with focus and JSplitPane but the bugs should have been fixed some time ago. if you can build a simple testcase representing you problem you should file a bug report.
i used requestFocus in a ancestor listener and it worked (for example with JOptionPane) but I did not use it in combination with JSplitPane.
chantal
 
pie. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic