easy question...

I want to give a button priority when the user hits the enter key, something like if key_pressed == "enter" goto btnLogin_clicked... (is there any way to "translate" that in Java?)

I want the button to be my default when the user hits Enter...
Nicolas_St_AmourAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jwilcoxCommented:
You could create a keyListener, check if the key was enter, and if so call your btnLogin_clicked method, I suppose, do you need an example of keyListener?
0
Nicolas_St_AmourAuthor Commented:
How can I set it so that only the Enter key is used to be the default?
0
kylarCommented:
Try something like this:

//~~~~~~~BEGIN CODE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class mainPanel extends JPanel implements KeyListener{

  public mainPanel(){
       super();
       JButton drawThings = new JButton("drawThings");
       drawThings.addKeyListener(this);
       this.add(drawThings);
 }

//KeyListener INTERFACE
 public void keyTyped(KeyEvent e){
   if(e.getKeyChar()=='\n'){
         //Do your things in here
 
  }
 public void keyPressed(KeyEvent e){};
 public void keyReleased(KeyEvent e){};

}
//~~~~END CODE EXAMPLE

that is a quick and dirty way of doing it. the technically correct way is to check like this: if(e.getKeyChar() == KeyEvent.VK_ENTER){} I think. but the other way works just fine for me.

Kylar
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Nicolas_St_AmourAuthor Commented:
Yes, but i want to know how to make it so that when I press enter it'll be the same as if I'd clicked a certain button...


Thanks
0
kylarCommented:
You have to add a Key Listener to all the fields that you want to be active. then when they press Enter on any of the active fields it will fall through to the same key listener. There's no way to make it a generic thing, unfortunately.

Kylar
0
Nicolas_St_AmourAuthor Commented:
oh ok, thanks, I'll try to figure a way out of this...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.