Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 21-07-2008, 11:18 AM   #1 (permalink)
Broken In
 
abhinav_bipnesh's Avatar
 
Join Date: Jul 2007
Posts: 113
Question Adding Keyboard Event to JButton


Hi Guy,

I have started learning Swing programming in java and for that i am creating a sample calculator application. For this i have made the JtextField a non focus object so when i click any of JButton textField take the input without any problem. Now i want to add keyboard event to these button so that when i press any numeric key on keyboard the textFiekd have the input for it.

Say for example if I press Esc key then all the text in textField get clear or when I press Num 7 key then textField has text as 7. But I am not able to add key event to the JButton. So please tell me how to do so.

Any help will be appricated.

Thanks in advance.
abhinav_bipnesh is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 21-07-2008, 12:48 PM   #2 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Adding Keyboard Event to JButton

Create a KeyListener instance which does the handling for keys. Here you can check the key pressed and append the appropriate character to JTextField.

Then add this KeyListener instance to all the JButton instances which should handle the event using the addKeyListener() method.

If you want all the components of the container to act in the same fashion you can use this method http://java.sun.com/javase/6/docs/ap...getComponents() to retrieve all components and add the listener to all of them by iterating through them.
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 21-07-2008, 01:41 PM   #3 (permalink)
Think Zen.
 
ray|raven's Avatar
 
Join Date: Dec 2005
Posts: 1,498
Default Re: Adding Keyboard Event to JButton

^Or , you could add a Mnemonic using JButton.setMnemonic

It works with the KeyCodes mentioned here : http://java.sun.com/javase/6/docs/ap.../KeyEvent.html
__________________
Do what you will; but not because you must. -- Zen Quote
ray|raven is offline  
Old 21-07-2008, 02:33 PM   #4 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Adding Keyboard Event to JButton

Quote:
Originally Posted by ray|raven View Post
^Or , you could add a Mnemonic using JButton.setMnemonic
Mnemonics may not work out for him as mnemonic means hitting Alt+<some_key>. But he wanted to just press 7 on JButton and get it in the Textfield, I guess somewhat like how various calculator apps behave.
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 21-07-2008, 02:37 PM   #5 (permalink)
Think Zen.
 
ray|raven's Avatar
 
Join Date: Dec 2005
Posts: 1,498
Default Re: Adding Keyboard Event to JButton

^Ah. Missed that part. Thanx for correcting me.
__________________
Do what you will; but not because you must. -- Zen Quote
ray|raven is offline  
Old 21-07-2008, 04:00 PM   #6 (permalink)
Broken In
 
abhinav_bipnesh's Avatar
 
Join Date: Jul 2007
Posts: 113
Default Re: Adding Keyboard Event to JButton

thanks guys,

But can you provide me a small code snapshot so that i can get some more clear picture.
abhinav_bipnesh is offline  
Old 21-07-2008, 11:26 PM   #7 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Adding Keyboard Event to JButton

Try this code. It is a simple one and may not be really well-written but should give you an idea.
Code:
package demo;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class DemoFrame extends JFrame {
    private JTextField textField;
    private JButton button1, button2;

    public DemoFrame() {
        textField = new JTextField(10);
        button1 = new JButton("1");
        button2 = new JButton("2");

        setSize(250, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new FlowLayout());
        textField.setEditable(false);

        getContentPane().add(textField);
        getContentPane().add(button1);
        getContentPane().add(button2);

        KeyListener listener = new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                int keyChar = e.getKeyChar();

                switch (keyChar) {
                case KeyEvent.VK_1:
                    textField.setText(textField.getText() + "1");
                    break;
                case KeyEvent.VK_2:
                    textField.setText(textField.getText() + "2");
                    break;
                case KeyEvent.VK_ESCAPE:
                    textField.setText("");
                }
            }
        };
        
        Component [] components = getContentPane().getComponents();
        for(Component component : components)
            component.addKeyListener(listener);
    }

    public static void main(String[] args) {
        new DemoFrame().setVisible(true);
    }
}
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 22-07-2008, 10:32 AM   #8 (permalink)
Broken In
 
abhinav_bipnesh's Avatar
 
Join Date: Jul 2007
Posts: 113
Default Re: Adding Keyboard Event to JButton

thanks man i will try it and this will help me out..........
abhinav_bipnesh is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chasis Event Occured lalit_nagalkar Software Q&A 1 06-07-2008 03:01 PM
Event Viewer girishrn Software Q&A 1 13-03-2008 04:05 PM
registering xp after the event !! sg1 Software Q&A 8 15-12-2007 08:17 AM
browser close event? daivagna Software Q&A 1 26-09-2006 10:13 PM

 
Latest Threads
- by Sujeet
- by clmlbx
- by Sujeet
- by icebags

Advertisement




All times are GMT +5.5. The time now is 11:06 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2