Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Action Event Problem

 
Aravind Prasad
Ranch Hand
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers!!
When i press the enter key in one textfield.. it has to execute what i have written in the buttonclick action.. how can i do that!!
=-------------------------------------------------------------------------- the code is here

private void jPasswordField1KeyPressed(java.awt.event.KeyEvent evt)
{ int keycode = evt.getKeyCode();
if(keycode == 13)
new Login().jButton1MouseClicked(java.awt.event.MouseEvent);
}

private void jButton1MouseClicked(java.awt.event.MouseEvent evt)
{ name = jFormattedTextField1.getText();
pass = jPasswordField1.getText();
if( name.length() == 0)
{ JOptionPane.showMessageDialog(null, "Enter the name");
}
else if(pass.length()==0)
{ JOptionPane.showMessageDialog(null, "Enter the password");
}
else
{ processLogin();
}
}

================================================================
i think.. i have to call some approprite function.. in the keypress function!!.. which i have given in the italics..
can u please help me ranchers!!

thanks in advance!!
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare a method say sample(). Put all mouse events code inside that method. call that method from your mouse event block as well as from your key event code .
 
Aravind Prasad
Ranch Hand
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks u sunil!!

do u know.. what is the keycode of enter key..
i think it is 13..
but when i put!!

int keycode = evt.getKeyCode();
if(keycode ==13)
{ sample();
}

it is not working!!

please help me!!
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use static variable or its value

public static final int VK_ENTER 10


int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER){
//your code
}
[ April 06, 2006: Message edited by: Sunil Kumar Gupta ]
 
Aravind Prasad
Ranch Hand
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u Sunil!!
It is working perfectly!!
1 more doubt!!
if i want to set the focus.. how can i do that!!!
ie., if key pressed is enter then the textField1 should loose the focus and textField2 should gain the focus!!

i've done like this

int keycode = evt.getKeyCode();
if(keycode == evt.VK_ENTER)
new Login().jPasswordField1.setFocusable(true);

but this is not working!!

can u please help me!!
 
kalps ganvir
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this
int keycode = evt.getKeyCode();
if(keycode == evt.VK_ENTER)
new Login().jPasswordField1.requestFocus();
 
Aravind Prasad
Ranch Hand
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no yaar!!
requestFocus() is not working!!
any other option!!
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be this help you

Use requestFocus(), ignore other methods.

Then quick guesses in the order of likehood:

(a) Call to requestFocus() is done *before* window is visible.
=> requestFocus() fails

(b) Component is not yet attached into layout
=> requestFocus() fails

(c) Component is not visible
=> requestFocus() fails

Request Focus works finely if the entire control is created fully and displayed
[ April 06, 2006: Message edited by: Sunil Kumar Gupta ]
 
Aravind Prasad
Ranch Hand
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sunil..
the requestFocus() is not working properly!!

this is my code!!
u see that code and send reply


==========================================================================

private void jFormattedTextField1KeyPressed(java.awt.event.KeyEvent evt) {
int keycode = evt.getKeyCode();
if(keycode == evt.VK_ENTER)
//new Login().jPasswordField1.setFocusable(true);
new Login().jPasswordField1.requestFocus();
//new Login().jPasswordField1.grabFocus();
}
========================================================================
till now it is not focusing to the textField!!

help me..
thanks in advance!!!
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using requestFocusInWindow() instead of requestFocus()
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is jPasswordField1 the field that you want to gain focus. Your earlier posts suggest it is textfield2 in which case that is the field you should be calling the method on.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic