Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JButton with "Enter Key" keyboard action

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default JButton with "Enter Key" keyboard action

    i would like to share this one that i gathered because of my frustration with my school project...

    this will show you how to make a keypress event (Enter key) in a simple JButton

    import java.awt.event.KeyEvent;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
     
    /**
     *
     * @author
     */
     
     
    public class JButtonWithEnterKeyPress {
     
        public static void main(String[] args) {
     
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
     
            // set this panel's layout to null for absolute positioning of components
            panel.setLayout(null);
     
            JButton button = new JButton();
     
            button.registerKeyboardAction(button.getActionForKeyStroke(
                                          KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
                                          KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
                                          JComponent.WHEN_FOCUSED);
     
            button.registerKeyboardAction(button.getActionForKeyStroke(
                                          KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
                                          KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
                                          JComponent.WHEN_FOCUSED);
     
            button.setText("Press The Enter Key");
            button.setBounds(0, 0, 200, 120);
            panel.add(button);
     
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(panel);
            frame.setVisible(true);
            frame.setSize(200, 120);
        }
    }

    you can use this by defining your own dialogs that will act just as like a JOptionPane dialogs...

    i hope you will like this

  2. The Following 3 Users Say Thank You to chronoz13 For This Useful Post:

    Archibold9 (December 21st, 2011), Hamedroozbehani (March 11th, 2013), sriharivaleti (March 27th, 2012)


  3. #2
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: JButton with "Enter Key" keyboard action

    Thanks for the useful tip.

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 04:07 PM.

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JButton with "Enter Key" keyboard action

    Thank you so much! Very very useful :-)

Similar Threads

  1. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  2. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 02:25 AM
  3. Replies: 1
    Last Post: October 25th, 2009, 12:54 PM
  4. Replies: 4
    Last Post: August 13th, 2009, 06:54 AM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 11:26 AM