Handling Focus on JTextField

J

Jason Cavett

I'm attempting to handle focus on a JTextField. Basically, when the
user tabs to the text field, I want all the text to be highlighted.
When the user uses the mouse to give the JTextField focus, I just want
the field to behave normally.

I attempted to add a FocusListener (see below), but it doesn't seem to
be working the way I intended. Can anybody provide some insight?
(Maybe I should use something other than a FocusListener?)


private FocusListener focus = new FocusListener() {

@Override
public void focusGained(FocusEvent e) {
if (/*Not sure of the check to put in here. Tried a number of
things that haven't worked.*/) {
selectAll();
}
}

@Override
public void focusLost(FocusEvent e) {
}

};
 
K

Knute Johnson

Jason said:
I'm attempting to handle focus on a JTextField. Basically, when the
user tabs to the text field, I want all the text to be highlighted.
When the user uses the mouse to give the JTextField focus, I just want
the field to behave normally.

I attempted to add a FocusListener (see below), but it doesn't seem to
be working the way I intended. Can anybody provide some insight?
(Maybe I should use something other than a FocusListener?)


private FocusListener focus = new FocusListener() {

@Override
public void focusGained(FocusEvent e) {
if (/*Not sure of the check to put in here. Tried a number of
things that haven't worked.*/) {
selectAll();
}
}

@Override
public void focusLost(FocusEvent e) {
}

};

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

public class FunnyJTextField extends JTextField {
private boolean mouseFlag;

public FunnyJTextField(String text) {
super(text);

addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
mouseFlag = true;
}
});

addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent fe) {
if (mouseFlag)
mouseFlag = false;
else
selectAll();
}
});
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextField tf = new JTextField("normal field");
f.add(tf,BorderLayout.CENTER);

FunnyJTextField ftf =
new FunnyJTextField("funny text field");
f.add(ftf,BorderLayout.SOUTH);

f.pack();
f.setVisible(true);
}
});
}
}
 
J

Jason Cavett

Jason said:
I'm attempting to handle focus on a JTextField.  Basically, when the
user tabs to the text field, I want all the text to be highlighted.
When the user uses the mouse to give the JTextField focus, I just want
the field to behave normally.
I attempted to add a FocusListener (see below), but it doesn't seem to
be working the way I intended.  Can anybody provide some insight?
(Maybe I should use something other than a FocusListener?)
   private FocusListener focus = new FocusListener() {
           @Override
           public void focusGained(FocusEvent e) {
                   if (/*Not sure of the check to put in here.  Tried a number of
things that haven't worked.*/) {
                           selectAll();
                   }
           }
           @Override
           public void focusLost(FocusEvent e) {
           }

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

public class FunnyJTextField extends JTextField {
     private boolean mouseFlag;

     public FunnyJTextField(String text) {
         super(text);

         addMouseListener(new MouseAdapter() {
             public void mousePressed(MouseEvent me) {
                 mouseFlag = true;
             }
         });

         addFocusListener(new FocusAdapter() {
             public void focusGained(FocusEvent fe) {
                 if (mouseFlag)
                     mouseFlag = false;
                 else
                     selectAll();
             }
         });
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 JTextField tf = new JTextField("normal field");
                 f.add(tf,BorderLayout.CENTER);

                 FunnyJTextField ftf =
                  new FunnyJTextField("funny text field");
                 f.add(ftf,BorderLayout.SOUTH);

                 f.pack();
                 f.setVisible(true);
             }
         });
     }

}

Ahhh...okay. I had considered using two listeners, but I wasn't sure
how to get them to talk. I was looking for too complicated of a
solution. Simple boolean works great.


Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top