Handling Focus on JTextField

Discussion in 'Java' started by Jason Cavett, Dec 2, 2008.

  1. Jason Cavett

    Jason Cavett Guest

    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) {
    }

    };
    Jason Cavett, Dec 2, 2008
    #1
    1. Advertising

  2. Jason Cavett wrote:
    > 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);
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Dec 2, 2008
    #2
    1. Advertising

  3. Jason Cavett

    Jason Cavett Guest

    On Dec 2, 12:04 pm, Knute Johnson <>
    wrote:
    > Jason Cavett wrote:
    > > 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);
    >              }
    >          });
    >      }
    >
    > }
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute2008/
    >
    > --
    > Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    >              ------->>>>>>http://www.NewsDemon.com<<<<<<------
    > Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


    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!
    Jason Cavett, Dec 2, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter

    JTextField JPassword

    Peter, Jul 11, 2003, in forum: Java
    Replies:
    0
    Views:
    3,294
    Peter
    Jul 11, 2003
  2. blue
    Replies:
    1
    Views:
    1,177
    Babu Kalakrishnan
    Dec 11, 2004
  3. Roger
    Replies:
    3
    Views:
    306
  4. Paolo

    focus on jtextfield

    Paolo, Sep 4, 2012, in forum: Python
    Replies:
    5
    Views:
    178
    Mark Lawrence
    Sep 5, 2012
  5. baz83

    jtextfield focus

    baz83, Sep 4, 2012, in forum: Java
    Replies:
    1
    Views:
    300
    Eric Sosman
    Sep 4, 2012
Loading...

Share This Page