java - How to select all text in a JFormattedTextField when it gets focus? -
I have a small Java desktop app that uses swing to create a data entry dialog with some different input fields (Jetextfield, Jessimbox, JSPinner, JFormattextfield). When I activate JFormattedTextFields by tapping through the form or clicking with the mouse, I would like to select all the text that I want. In this way, users can simply start typing and overwrite the default values.
How can I do this? I have used a focus listener / focus adapter which selects all () calls on JFormattedTextField, but it does not select anything, although the Focus adapter's focus is called the grain () method (see code sample below) .
Private javax.swing.JFormattedTextField pricePerLiter; // {...} "" "" "" "" "" "" "" "" "" "" "" ... "0.000")); Formatter.setValueClass (double class);} return formater;}}); // ... PricePerLiter.addFocusListener (new java.awt.event.FocusAdapter () {Public Zero was focused (java.awt.event.FocusEvent evt) {pricePerLiter.selectAll ();}});
Any ideas? The interesting thing is that by choosing all your text, apparently the default behavior for both is JTextField and JSpinner, at least when tabbing through the form.
Wrap your call with SwingUtilities.invokeLater, after processing all pending att event This will be:
pricePerLiter.addFocusListener (new java.awt.event.FocusAdapter () {Public Zero Focused (Java.avt.evolution.focus event evt) {SwingUtilities.invokeLater (New Runnabal) {@Override Public run zero () {pricePerLiter.selectAll ();}}}}}});
Comments
Post a Comment