[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

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...
0
Nicolas_St_Amour
Asked:
Nicolas_St_Amour
  • 3
  • 2
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now