my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Action Event Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Action Event Problem" Watch "Action Event Problem" New topic
Author

Action Event Problem

Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 263
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

Joined: Aug 26, 2005
Posts: 824
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 .


Lack of will power has caused more failure than lack of intelligence or ability.
My Blog | Red5 Resources | Technology Update | Daily Technology Tips
Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 263
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

Joined: Aug 26, 2005
Posts: 824
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

Joined: Dec 28, 2005
Posts: 263
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

Joined: Mar 29, 2006
Posts: 8
try this
int keycode = evt.getKeyCode();
if(keycode == evt.VK_ENTER)
new Login().jPasswordField1.requestFocus();
Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 263
no yaar!!
requestFocus() is not working!!
any other option!!
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
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

Joined: Dec 28, 2005
Posts: 263
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

Joined: Aug 26, 2005
Posts: 824
try using requestFocusInWindow() instead of requestFocus()
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3487
    
  13
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.


Joanne
 
 
subject: Action Event Problem