Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel - RequestFocus on JTextField  RSS feed

 
Sri Rangan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • 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
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
hi,
try requestFocus().
chantal
 
Sri Rangan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mmmm. Tried requestFocus(). Still does't work.
 
Eugene Ho
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!