简体   繁体   中英

How to disable automatic focus on textField

When I was creating a bunch of JTextFields I saw that first one is selected. I want to deselect it, because I have focus listener and it's running automatically.
Any clues?

SSCCE:

JTextField tf = new JTextField("hello");
tf.setForeground(Color.decode("0x8C8C8C")); // for nice comment inside the text field
textFieldKwotaWplacona.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) 
        {

            if(tf.getForeground() != Color.BLACK)
            {
            tf.setText("");
            tf.setForeground(Color.BLACK);
            }
        }   @Override
        public void focusLost(FocusEvent arg0) {}});
//for deleting "nice comment" after click

tf.setBounds(//some bounds);
add(tf);

Repeat that process for another text field

EDIT2 : actual code (I believe its sscce :P)

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class Main extends JFrame implements ActionListener
{
JTextField textFieldKwotaWplacona, textFieldOprocentowanie, textFieldDlugoscLokaty, textFieldKwotaOtrzymana;

Main()
{   setSize(500,300);
    setLayout(null);
    setTitle("Program do liczenia procentu składanego");
setDefaultCloseOperation(EXIT_ON_CLOSE);


    textFieldKwotaWplacona = new JTextField("Ilość pieniędzy wpłaconych");
    textFieldKwotaWplacona.setForeground(Color.decode("0x8C8C8C"));
    textFieldKwotaWplacona.addActionListener(this);
    textFieldKwotaWplacona.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) 
        {

            if(textFieldKwotaWplacona.getForeground() != Color.BLACK)
            {
            textFieldKwotaWplacona.setText("");
            textFieldKwotaWplacona.setForeground(Color.BLACK);
            }
        }   @Override
        public void focusLost(FocusEvent arg0) {}});

    textFieldKwotaWplacona.setBounds(10, 10, 100, 20);
    add(textFieldKwotaWplacona);

    textFieldOprocentowanie = new JTextField("Oprocentowanie");
    textFieldOprocentowanie.setForeground(Color.decode("0x8C8C8C"));
    textFieldOprocentowanie.addActionListener(this);


    textFieldOprocentowanie.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) 
        {

            if(textFieldOprocentowanie.getForeground() != Color.BLACK)
            {
            textFieldOprocentowanie.setText("");
            textFieldOprocentowanie.setForeground(Color.BLACK);
            }
        }

        @Override
        public void focusLost(FocusEvent arg0) {}});
    textFieldOprocentowanie.setBounds(10, 40, 100, 20);
    add(textFieldOprocentowanie);



}




@Override
public void actionPerformed(ActionEvent arg0) 
{
    // TODO Auto-generated method stub
}

    public static void main(String[] args) 
{
    Main a=new Main();
    a.setVisible(true);


}
}

I want to set focus to window or sth else, in order to prevent text from disappearing.

In your constructor, you could use the method requestFocusInWindow() .

This is what was working for me here-

After creating the JFrame, call frame.requestFocusinWindow(); . This will make sure your text field is not focused. Then, when you focus on the text field, the event is being fired.

As discussed in the comments, I added a radio button to take the focus instead:

public class Main extends JFrame {

    JTextField textFieldKwotaWplacona, textFieldOprocentowanie;

    Main() {

        setTitle("Program do liczenia procentu składanego");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        textFieldKwotaWplacona = new JTextField("Ilość pieniędzy wpłaconych");
        textFieldKwotaWplacona.setForeground(Color.decode("0x8C8C8C"));
        textFieldKwotaWplacona.addFocusListener(new FieldFocusListener(textFieldKwotaWplacona));
        add(textFieldKwotaWplacona);

        textFieldOprocentowanie = new JTextField("Oprocentowanie");
        textFieldOprocentowanie.setForeground(Color.decode("0x8C8C8C"));
        textFieldOprocentowanie.addFocusListener(new FieldFocusListener(textFieldOprocentowanie));
        add(textFieldOprocentowanie);

        JRadioButton btn = new JRadioButton("text");
        add(btn);

        pack();
        btn.requestFocusInWindow();
    }

    private class FieldFocusListener extends FocusAdapter {

        private JTextField field;

        FieldFocusListener(JTextField field) {

            this.field = field;
        }

        @Override
        public void focusGained(FocusEvent e) {

            if (field.getForeground() != Color.BLACK) {
                field.setText("");
                field.setForeground(Color.BLACK);
            }
        }
    }

    public static void main(String[] args) {

        Main a = new Main();
        a.setVisible(true);
    }
}

Explanation

From the tutorial :

If you want to ensure that a particular component gains the focus the first time a window is activated, you can call the requestFocusInWindow method on the component after the component has been realized, but before the frame is displayed.

That means btn.requestFocusInWindow() must appear after pack() and before a.setVisible(true) .

The reason you need another component to take the focus is that when a window is focused, a component inside it must gain the focus.

Notes:

  • If you want a better text field hint, see @camickr's answer .
  • Don't use null layout. Pick one that serves your GUI design (I picked FlowLayout just because it's fast to use, though probably not what you need).
  • Instead of setting the size of the frame, pack() after all components had been added.
  • Instead of creating the same focus listener for every text field, just create it as a class and reuse it. I show one way with passing the component to a constructor, but you can get rid of that and use e.getComponent() to get the text field instance.
tf.setForeground(Color.decode("0x8C8C8C")); // for nice comment inside the text field

Maybe you are trying to set a prompt for the text field that disappears when the text field gains focus?

If so, check out Text Field Prompt for a solution.

If not, then post a proper SSCCE, because I still can't guess what you are trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM