4

I am learning java and Swing right now and trying to develop simple programms for education purposes.

So here is the question.

I have gridlayout and fields on my frame with default text

        accNumberField = new JTextField("0", 10);
    accNumberField.addFocusListener(new FocusListener() {
        int focusCounter = 0;
        @Override
        public void focusGained(FocusEvent arg0) {
            // TODO Auto-generated method stub
            if (focusCounter > 0)
            accNumberField.setText("");
            focusCounter++;
        }

What I want is that when user click on field for the first time the default text is disappered. So I add focus listener and used accNumberField.setText(""); in focusGained method.

But the problem is that for default first field in my frame getting focus right in time of frame creation. And default text is disappearing from the begining. I used counter as you can see. But that's not what I wanted.

I want that no field would get focus in time of creation and every field would be able to get focus from the time when user would click on one of them.

Sorry if I spelled something wrong. English is not my native language.

user1685095
  • 5,787
  • 9
  • 51
  • 100
  • `new JTextField("0", 10);` Besides `1`, what might the user type in the field? What is this GUI? – Andrew Thompson Sep 20 '12 at 08:25
  • http://stackoverflow.com/questions/1738966/java-jtextfield-with-input-hint – Adeel Ansari Sep 20 '12 at 08:37
  • Any integer number. GUI is one frame with gridlayout 5 by 3. One row of the gris is User type value in the field, clicks save button and if input was right icon is changing from red to green tilt. – user1685095 Sep 20 '12 at 09:04

2 Answers2

2

Is there any reason that you use focusListener()? why not use mouseListener() as follow?

    accNumberField.addMouseListener(new MouseAdapter()
    {
        @Override
        public void mouseReleased(MouseEvent e)
        {
            accNumberField.setText("");
        }
    });

if you want to clear the text for the first click, you can simply use a boolean:

    //outside constructor
    private boolean isTextCleared = false;

    //in constructor
    accNumberField.addMouseListener(new MouseAdapter()
    {
        @Override
        public void mouseReleased(MouseEvent e)
        {
            if (!isTextCleared)
            {
                accNumberField.setText("");
                isTextCleared = true;
            }
        }
    });
Canis Majoris
  • 1,016
  • 1
  • 7
  • 18
  • No I do not =)) If I will add braces then this two lines accNumberField.setText(""); focusCounter++; will never get executed))) I repeat, "I want that no field would get focus in time of creation..." – user1685095 Sep 20 '12 at 08:34
2

Found a thread having a code example of your desired functionality, Java JTextField with input hint. Precisely, you need to provide your own implementation of JTextField which will be holding the "default-text" in a field, specially created for that.

For your second question, you can set the focus to some button or frame itself.

Community
  • 1
  • 1
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
  • And again this is not my desired functionality. Thanks for your help, but that's not completely what I whanted and what I was asking for. When program is starting execution and GUI is created first field in layout gain focus, and then "hint" or "default text" is disappearing. I want it to not disappear. More than that I want that no textField would gain focus up to the moment when user click on them. Is that clear? =) – user1685095 Sep 20 '12 at 08:58
  • @user1685095: Ahan, so the link didn't answer your second part. Am I correct? – Adeel Ansari Sep 20 '12 at 09:04
  • I tried set focus on some button, but It didn't work somehow. I'll try do that on frame. – user1685095 Sep 20 '12 at 10:01
  • I can't make it work :( MyFrame frame = new MyFrame(); frame.requestFocus(); and on button button1.requestFocus(); (This is in MyFrame constructor) Still focus is at text field. – user1685095 Sep 20 '12 at 10:05
  • pack(); button5.requestFocusInWindow(); That did the trick! And to my shame I just did not read about focus on oracle site carefully, sorry! Thanks for the answers. – user1685095 Sep 20 '12 at 10:16
  • @user1685095: Sorry I was away. But I'm glad that it helped. – Adeel Ansari Sep 21 '12 at 03:52
  • @user1685095 I was facing precisely the same problem. Thanks! pack followed by requestFocus worked for me. – Parag Doke Oct 20 '13 at 02:42