Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 26-01-2010, 02:53 PM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
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

Java Code
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



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
Reply

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
"java.lang.NoSuchMethodError: main" and "fatal exception occured." joachim89 What's Wrong With My Code? 2 10-01-2010 12:35 PM
Please help! Exception in thread "main" java.lang.NullPointerException Arutha2321 What's Wrong With My Code? 2 18-11-2009 06:25 AM
Exception in thread "main" java.lang.NoClassDefFoundError: AVTransmit2 NARs Java Networking 1 25-10-2009 03:54 PM
Request.getParameter() value becoming null when i use entype="multipart/form-data" radhikavk JavaServer Pages: JSP & JSTL 4 13-08-2009 10:54 AM
[SOLVED] Problem with Java: "GridLayout" laying out the top part of my assignment? antitru5t AWT / Java Swing 3 16-04-2009 03:26 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:01 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.