Is this your first time here? SwingWiki is a Java Swing Developer community site with an big archive of Swing-related usenet groups and mailing lists, but also tips, tricks and articles and book reviews written by your colleagues from around the world. If you came here through a search engine and did not find what you were looking for, make sure to check the wiki table of contents.

You can define a JButton as the default-button for a Form like this:

JButton btnRun = new JButton("run");
btnRun.setDefaultCapable(true);

frame.getRootPane().setDefaultButton(btnRun);

If the user is pressing enter in that form, the default-button is clicked. If you are in a JTextField, the button does not get clicked.

To work around that, you can remove the key-mapping from the JTextField:

InputMap map = txtField.getInputMap();
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
while (map != null) {
  map.remove(enter);
  map = map.getParent();
}

 

Comments? Corrections? Contact us or Login to edit pages directly (registration is free and takes less than displaying a JLabel)
  workaround/jtextfield_default-button_and_enter.txt · Last modified: 2005/04/18 06:13 by 195.202.41.134 (chbeer78)
 
Recent changes | RSS changes | Table of contents | News Archive | Terms And Conditions | Register | The Quest For Software++| Ruby Resources

Sedo - Buy and Sell Domain Names and Websites project info: swingwiki.org Statistics for project swingwiki.org etracker� web controlling instead of log file analysis