Results 1 to 2 of 2
  1. #1
    amitabh is offline Member
    Join Date
    Apr 2010
    Posts
    51
    Rep Power
    0

    Default Event On jButton

    Hello Every One
    I have a project in netbeans.
    My problem is that i want to write event on enter key means i want to save my data into database by hiting enter key.
    can it is posible by setManemonic() method if y then please tell me how i use it in netbeans because i try a lot to set on jbutton but it gives error .
    in properties of jButtoni setmanemonic by customize code and put VK_ENTER but it gives error
    so where i am wrong please tell me
    Thank You

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    1. When user click ENTER will it fire the button event even if the focus is not in the button?
    if yes then you have to set what is the default button. then look at this site.
    Enter Key and Button Java Tips Weblog

    2. When user click ENTER and the focus is in the button then the event will fire.
    * Implement KeyListner then
    * AddKeylistener in the button
    * code in keyPressed or keyReleased or keyTyped event
    Java Code:
        public void keyTyped(KeyEvent e)
        {
            if(getFocusOwner() instanceof JButton)
            {   if (e.getKeyCode() == KeyEvent.VK_ENTER)
                {//Your code here}
            }
        }
    
        public void keyPressed(KeyEvent e)
        {
            if(getFocusOwner() instanceof JButton)
            {   if (e.getKeyCode() == KeyEvent.VK_ENTER)
                {//Your code here}
            }
        }
    
        public void keyReleased(KeyEvent e)
        {
            if(getFocusOwner() instanceof JButton)
            {   if (e.getKeyCode() == KeyEvent.VK_ENTER)
                {//Your code here}
            }
        }
    OR click JButton and on its properties goto Events>keyPressed or keyReleased or keyTyped>name the handler>click OK>then type your code.

    Hope this help.

    Goodluck,
    geje

Similar Threads

  1. JButton help :)
    By yasmin k in forum AWT / Swing
    Replies: 7
    Last Post: 11-12-2009, 10:53 PM
  2. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 02:08 AM
  3. JList and JButton event handler not working
    By H3rtaherta in forum AWT / Swing
    Replies: 3
    Last Post: 11-22-2008, 01:00 AM
  4. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 11:48 AM
  5. Need help with JButton event
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 02:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •