Re: Auto Select text on textfield focus
- From: therodet@xxxxxxxxx
- Date: 13 Jul 2006 09:20:18 -0700
andrewthommo@xxxxxxxxx wrote:
therodet@xxxxxxxxx wrote:
I want to make JTextFields in my application automatically select all
text when they receive focus, whether by mouse or by keyboard. More
importantly, I don't want to have to manually install listeners on
every JTextField I create.
Here's one way, try this for a few laughs..
<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class AutoSelectTextField
extends JTextField
implements FocusListener {
AutoSelectTextField(String text) {
super(text);
addFocusListener(this);
}
public void focusLost(FocusEvent fe) {}
public void focusGained(FocusEvent fe) {
setCaretPosition(0);
if (getText()!=null) {
moveCaretPosition( getText().length() );
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
Container c = f.getContentPane();
c.setLayout(new GridLayout(0,1));
StringBuffer sb = new StringBuffer();
for (int ii=0; ii<10; ii++) {
c.add( new AutoSelectTextField(sb.toString()) );
sb.append("Ha ");
}
f.pack();
f.setVisible(true);
}
}
</sscce>
HTH
Andrew T.
I had considered that option, but that would require altering quite a
few classes that alreay have JTextFields and JFormattedTextFields,
which I would prefer to avoid.
.
- References:
- Auto Select text on textfield focus
- From: therodet
- Re: Auto Select text on textfield focus
- From: andrewthommo
- Auto Select text on textfield focus
- Prev by Date: Re: Auto Select text on textfield focus
- Next by Date: Re: JFFMPEG + JMF + RTP
- Previous by thread: Re: Auto Select text on textfield focus
- Next by thread: Re: Auto Select text on textfield focus
- Index(es):
Relevant Pages
|
|