File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to make the Java Default Button to trigger only on Enter Key Release Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to make the Java Default Button to trigger only on Enter Key Release" Watch "How to make the Java Default Button to trigger only on Enter Key Release" New topic
Author

How to make the Java Default Button to trigger only on Enter Key Release

Hirantha Ranasinghe
Greenhorn

Joined: Sep 06, 2012
Posts: 1
I want to Java Default button to work only on Enter Key Release event. Normally it works with Enter Pressed as well. I want to stop it.

Following is the code.



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

public class ButtonTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
buildFrame();
}
});
}

private static void buildFrame() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

JButton button = new JButton(new AbstractAction("Button") {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ButtonTest::actionPerformed: CALLED");
}
});

JButton button2 = new JButton("Button 2");
InputMap im = button.getInputMap();
im.put(KeyStroke.getKeyStroke("ENTER"), "pressed");
im.put(KeyStroke.getKeyStroke("released ENTER"), "released");

f.setLayout(new GridBagLayout());
f.add(button);
f.add(button2);
f.getRootPane().setDefaultButton(button);

f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Hirantha, please BeForthrightWhenCrossPostingToOtherSites
http://stackoverflow.com/questions/12260962/how-to-set-the-java-default-button-to-react-on-enter-key-released

The advice you got on that thread is right on the money. If you didn't understand it enough to implement it, you could have asked for clarification instead of starting a new thread on another forum without telling us about the advice you already got.

Also, please learn to UseCodeTags.


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Study the section of the Swing tutorial that deals with How to Use Key Bindings and you'll be able to do this yourself.

I would question the rationale behind this. What practical difference does it make whether the action is triggered by releasing, not pressing, the <Enter> key? What's different about your application that requires non-standard behavior?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make the Java Default Button to trigger only on Enter Key Release
 
Similar Threads
Two key bindings for a single button
Action Listener for Enter Key pressing on CheckBox
JButton on multiple instances of applet
Help on Tooltip
Pressing ENTER when Button is selected doesnt fire buttonActionPerformed...