-4

I have Login JButton on a panel and I need to execute it when I press ENTER key.

Do we have any code snippet for that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1CzDx
  • 57
  • 1
  • 2
  • 8
  • 4
    *"Do we have any code snippet for that?"* Do we have any sign of effort on we's part? Or is we just treating SO as a code factory? – Andrew Thompson Feb 06 '14 at 06:50
  • 3
    See [How to use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – Paul Samsotha Feb 06 '14 at 06:52
  • 1
    http://docs.oracle.com/javase/7/docs/api/javax/swing/JRootPane.html#setDefaultButton%28javax.swing.JButton%29 – JB Nizet Feb 06 '14 at 06:58
  • @JBNizet As I was musing over the wisdom of your link, I was thinking how it would be great if there was a [stack app](http://stackapps.com/) to automatically translate such links to links with text constructed as follows. Fetch the page title `JRootPane (Java Platform SE 7 )` append `: ` + (the link text) `setDefaultButton` To appear as **`JRootPane (Java Platform SE 7 ): setDefaultButton`**. If I get around tuit, I must see if I can get something together that is workable. I think it would need to maintain a local cache of titles and and anchor texts, which might be problematic.. – Andrew Thompson Feb 06 '14 at 07:13
  • Or.. [JRootPane (Java Platform SE 7 ): setDefaultButton](http://docs.oracle.com/javase/7/docs/api/javax/swing/JRootPane.html#setDefaultButton%28javax.swing.JButton%29). At the end of the last comment there were 7 chars spare, not enough to add the actual link! – Andrew Thompson Feb 06 '14 at 07:16
  • 4
    Lots of efforts to deal with my laziness :) My comments usually show an effort that is proportional to the effort put in the commented question... – JB Nizet Feb 06 '14 at 07:17
  • @JBNizet LOL! ;) Actually I think the brevity of it adds to the charm of the comment. Straight to the point. :) – Andrew Thompson Feb 06 '14 at 07:19
  • Also consider `setDefaultButton()`, seen [here](http://stackoverflow.com/a/7457102/230513). – trashgod Feb 06 '14 at 09:04
  • Apologies for the vague question! My intention is to create a keylistener kind of thing for a Jbutton, which executes when I press the ENTER key. I just saw the action map and I hope it'll solve my issue. Let me implement and revert back. Thanks for sharing the solutions though. – 1CzDx Feb 06 '14 at 16:17

2 Answers2

2

To have initial focus on your button you can do something like this:

frame.getRootPane().setDefaultButton(buttonName);
buttonName.requestFocus();

//Or you can bind your Enter key to JComponent and JButton as:

AbstractAction action = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() instanceof JButton){
        JButton button = (JButton) e.getSource();
        button.doClick();        
        } else if(e.getSource() instanceof JComponent){
            JComponent component = (JComponent) e.getSource();
            component.transferFocus();
        }
    }
    };

//You can bind key to JComponent like:

jComponent.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus");
jComponent.getActionMap().put("TransferFocus", action);

//You can bind key to JButton like:

jButton.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "DoClick");
jButton.getActionMap().put("DoClick", action);

Useful Link

How to Use the Focus Subsystem

ravibagul91
  • 20,072
  • 5
  • 36
  • 59
2

You can use InputMap and ActionMap to do this.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ac1
{
    public static void main(String args[])
    {
    JFrame f=new JFrame();
    f.setVisible(true);
    f.setSize(400,400);
    f.setLayout(new FlowLayout());

    final JButton b=new JButton("button");
    f.add(b);



f.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDO

W).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),"clickButton");

f.getRootPane().getActionMap().put("clickButton",new AbstractAction(){
        public void actionPerformed(ActionEvent ae)
        {
    b.doClick();
    System.out.println("button clicked");
        }
    });
    }
}
JavaTechnical
  • 8,846
  • 8
  • 61
  • 97