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 5 of 5

Thread: JButton by pressing the enter key.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile JButton by pressing the enter key.

    Hi friends.
    How can I do ActionListener to a button with pressing the Enter key in keyboard?
    My code is this:
    import javax.swing.*;
    import java.awt.*;
    public class myclass{
    	public static void main(String args[]){
    		JFrame window = new JFrame();
    		JTextField username, password;
    		JLabel usernamelabel, passwordlabel, sentence;
    		JButton button;
     
    		window.setSize(400, 200);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setBackground(Color.white);
    		window.setResizable(false);
    		window.setLayout(null);
    		window.setTitle("Wellcome.");
     
    		usernamelabel = new JLabel("Username: ");
    		usernamelabel.setBounds(100, 40,  80, 30);
    		window.add(usernamelabel);
     
    		username = new JTextField("");
    		username.setBounds(180, 40, 120, 30);
    		window.add(username);
     
    		passwordlabel = new JLabel("Password: ");
    		passwordlabel.setBounds(100, 80, 80, 30);
    		window.add(passwordlabel);
     
    		password = new JTextField("");
    		password.setBounds(180, 80, 120, 30);
    		window.add(password);
     
    		button = new JButton("  OK  ");
    		button.setBounds(160, 120, 80, 30);
    		window.add(button);
     
    		sentence = new JLabel("Please enter your username and password");
    		sentence.setBounds(65, 8, 270, 30);
    		sentence.setForeground(Color.DARK_GRAY);
    		window.add(sentence);
    		window.setVisible(true);
     
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JButton by pressing the enter key.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: JButton by pressing the enter key.

    You may want to re-organize your code to set it up for creating a listener, capturing the event and registering the listener to the appropriate JComponents. There are a few ways to do this, such as inner classes. Alternatively, you can use anonymous classes or keep them in separate classes/packages.

    ActionListeners are not suitable for capturing keyboard events (KeyEvents). Instead, you should use a key listener, either by the KeyListener or KeyAdapter. The main method can be in the same class or different class.

    Here is a short outline of how you may want to re-arrange your code:

    package com.blah.gui;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class MyClass extend JFrame {
         // write all of the JComponents that will be used in any of your listeners (do not have duplicates in your constructor)     
     
         public class MyActionListener implements ActionListener {
            // override actionPerformed(ActionEvent event) {...}
         }
     
        public class MyKeyListener implements KeyListener {
           // override the 3 methods OR extend a KeyAdapter then override whichever methods you need
        }
     
         public MyClass() {
            // place your GUI components here and add to the JFrame
           // register the appropriate JComponents to the listener classes. 
        }
     
    }

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: JButton by pressing the enter key.

    Quote Originally Posted by SunshineInABag View Post
    You may want to re-organize your code to set it up for creating a listener, capturing the event and registering the listener to the appropriate JComponents. There are a few ways to do this, such as inner classes. Alternatively, you can use anonymous classes or keep them in separate classes/packages.
    Agree.

    ActionListeners are not suitable for capturing keyboard events (KeyEvents). Instead, you should use a key listener, either by the KeyListener or KeyAdapter. The main method can be in the same class or different class.
    Disagree.

    KeyListeners are quite low-level listeners and in general should be avoided with Swing applications (as is stated in the tutorials). Instead you could:

    • Create an ActionListener and give the same listener to your JTextFields as you give your JButton. A JTextField's ActionListener will be triggered by pressing ENTER while it has focus.
    • Make your JButton the default button for the JFrame's RootPane. This way if any component in the GUI has focus and doesn't swallow the ENTER key, then pressing enter will trigger your JButton to be pressed. For example:
      myFrame.getRootPane().setDefaultButton(myButton);

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: JButton by pressing the enter key.

    Quote Originally Posted by Hamedroozbehani View Post
    Hi friends.
    How can I do ActionListener to a button with pressing the Enter key in keyboard?
    I hope this tutorial is help to you...Refer this

    http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

Similar Threads

  1. playing sound by pressing key
    By pebri in forum Android Development
    Replies: 0
    Last Post: March 5th, 2013, 12:10 PM
  2. On Pressing JButton, JAVA Crashes'
    By suyog53 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 18th, 2012, 12:53 AM
  3. Replies: 8
    Last Post: May 28th, 2012, 11:29 AM
  4. JButton with "Enter Key" keyboard action
    By chronoz13 in forum Java Swing Tutorials
    Replies: 2
    Last Post: March 14th, 2012, 06:49 AM
  5. JButton with "Enter Key" keyboard action
    By chronoz13 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: January 26th, 2010, 10:53 AM

Tags for this Thread