Soluzione uno: farlo attraverso l'evento di messa a fuoco. Non è la soluzione migliore.
public static void main(final String[] args) {
// simple window preparation
final JFrame f = new JFrame();
f.setBounds(200, 200, 400, 400);
f.setVisible(true);
{ // this sleep part shall simulate a user doing some stuff
try {
Thread.sleep(2345);
} catch (final InterruptedException ignore) {}
}
{ // here's the interesting part for you, this is what you put inside your button listener or whatever
final JTextField t = new JTextField("Hello World!");
t.addFocusListener(new FocusListener() {
@Override public void focusLost(final FocusEvent pE) {}
@Override public void focusGained(final FocusEvent pE) {
t.selectAll();
}
});
f.add(t);
f.validate();
t.requestFocus();
}
}
È quello che stai cercando? http://stackoverflow.com/questions/1178312/how-to-select-all-text-in-a-formattedtextfield-when-it-gets-focus/1178596#1178596 – SomeJavaGuy
Ho provato questo entrambi prima che facessi questa domanda, il problema con questa risposta è che non ha funzionato. anche l'altro problema con questa risposta è che il campo di testo diventa attivo solo dopo aver fatto clic sul testo e non quando appare –
Vedere http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html su come mettere a fuoco un componente Swing –