hi,

i have a JButton object and i added an actionListener to it. it works well with a mouse click, but if use the keyboard enter key while the focus is on the JButton, nothing happens. do i need to add a keypress listener on top of the actionListener to the JButton so that it would work both for a keypress and a mouse click? any help would be appreciated.

thanks..

Recommended Answers

All 8 Replies

hi,

i have a JButton object and i added an actionListener to it. it works well with a mouse click, but if use the keyboard enter key while the focus is on the JButton, nothing happens. do i need to add a keypress listener on top of the actionListener to the JButton so that it would work both for a keypress and a mouse click? any help would be appreciated.

thanks..

Yes you can use KeyEvent.

jButton.addKeyListener(new java.awt.event.KeyAdapter() {
          public void keyPressed(java.awt.event.KeyEvent evt) {
             if(evt.getKeyCode() ==java.awt.event.KeyEvent.VK_ENTER){ //To check enter key    
                                                                      // has been pressed or  
                                                                      //not. If no condition 
                                                                      //this will work for all 
                                                                      //keys.  

               // some actions
        
                }
            }
        });

if i use the above code, will a mouse click on the jbutton still work?
my intention actually is for the jbutton to respond to both a mouse click and an enter key.

i'll try it out. thanks..

well, it did work if i use keyboard enter key on the JButton. but now, it does not work if i use the mouse to click on the JButton.

what i intend is to process some information after clicking an 'okey' JButton or (if the user intends to use the keyboard instead of the mouse) when a user presses the enter key while the focus is on the 'okey' JButton. i mean, the JButton must handle both mouse click and keyboard enter keypressed.

thanks..

How that will not work man?????

Here is the sample code for your doubts..

import java.awt.event.KeyAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JTextField;

public class Test_event_on_button extends javax.swing.JFrame{
    
    JButton jbutton1;
    JTextField jtextfield;
    static int count;
    
   public Test_event_on_button() {
     setLayout(null);
     jbutton1=new JButton("Button to handle both mouse and any key event");
     jtextfield=new JTextField();
     
     jbutton1.addActionListener(new ActionListener(){   //Registering button for  
                                                        //ActionListener which will support 
                                                        //mouse for buuton,no need to use 
                                                        //MouseEvent for button
                public void actionPerformed(ActionEvent e){
                    
                    jtextfield.setText("Event by mouse "+count++);
                }
                }
             );
      jbutton1.addKeyListener(new KeyAdapter() { // Registering button for keyListener
          
               public void keyPressed(KeyEvent e){
                   jtextfield.setText("Event by Key "+count++);
               }
                }
              );
         jtextfield.setBounds(50,50,300,30);
         jbutton1.setBounds(50,100,300,30);
     add(jbutton1);         
     add(jtextfield);
    }
    public static void main(String[] args) {
        Test_event_on_button obj=new Test_event_on_button();
        obj.setSize(400,300);
        obj.setVisible(true);
    }
    
}

well, i'll be..
the error was attributed to the way i structured my codes, not on your suggested codes. my apologies. it's working now.
thank you for your patience in helping me.

@Muralidharan.E only changed your all Un-Swing ***

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test_event_on_button extends javax.swing.JFrame {

    private static final long serialVersionUID = 1L;
    private JButton jbutton1;
    private JTextField jtextfield;
    private int count;

    public Test_event_on_button() {
        setLayout(new GridLayout(0, 1, 5, 5));
        jbutton1 = new JButton("Button to handle both mouse and any key event");
        jbutton1.addActionListener(new ActionListener() {   //Registering button for  
            // ActionListener which will support mouse for button,
            // no need to use mouse Event for button

            @Override
            public void actionPerformed(ActionEvent e) {
                jtextfield.setText("Event by mouse " + count++);
            }
        });
        jbutton1.addKeyListener(new KeyAdapter() { // Registering button for keyListener

            @Override
            public void keyPressed(KeyEvent e) {
                jtextfield.setText("Event by Key " + count++);
            }
        });
        jbutton1.setPreferredSize(new Dimension(300, 30));
        jbutton1.setFocusPainted(false);
        add(jbutton1);
        jtextfield = new JTextField();
        jtextfield.setPreferredSize(new Dimension(300, 30));
        add(jtextfield);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(200, 200);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        Test_event_on_button obj = new Test_event_on_button();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.