2

I have some text field on my form. And I like focusGained and focusLost event. Doing this with 2 or 3 text field is easy. But, after 18 text field, well, it's kinda confusing. Is there any way to shorten the focusGained and focusLost event? Example:

txtSomeTextFocusGained(java.awt.event FocusEvent evt){
    if(txtSomeText.getText().equals("Text here!"){
        txtSomeText.setText("");
    }
}

txtSomeTextFocusLost(java.awt.event FocusEvent evt){
    if(txtSomeText.getText().equals(""){
        txtSomeText.setText("Text here!");
    }
}

That's one text field, I've problem handling with about 18 text field. Any way to simplify that? Thanks.

4

4 回答 4

3

The methods are simple enough, so I can't think of a way to simplify them any further. What you can do, though, is prevent code repetition by declaring one FocusListener instance and then add it using addFocusListener(...) to all text-fields.

It would look something like this:

// Instantiate a FocusListener ONCE
java.awt.event.FocusListener myFocusListener = new java.awt.event.FocusListener() {
    public void focusGained(java.awt.event.FocusEvent focusEvent) {
        try {
            JTextField src = (JTextField)focusEvent.getSource();
            if (src.getText().equals("Text here!") {
                src.setText("");
            }
        } catch (ClassCastException ignored) {
            /* I only listen to JTextFields */
        }
    }

    public void focusLost(java.awt.event.FocusEvent focusEvent) {
        try {
            JTextField src = (JTextField)focusEvent.getSource();
            if (src.getText().equals("") {
                src.setText("Text here!");
            }
        } catch (ClassCastException ignored) {
            /* I only listen to JTextFields */
        }
    }
};

(You could omit the try-catch blocks if you were absolutely sure that the source of the event would always be a JTextField, but it is always a bad practice to rely on such assumptions.)

Then, for every JTextField you only need to add the same FocusListener:

...
someTextField.addFocusListener(myFocusListener);
...

(It's only half a line - difficult to get any shorter than that.)


Another alternative would be to subclass JTextField, adding a FocusListener in the constructor, but I don't see any advantage over the first solution (unless you want a more flexible/powerful solution, e.g. different text for each JTextField etc).

于 2013-06-05T18:59:14.110 回答
1

If you want just to set some text in field which gets focused you could write separated event handler class which implements FocusListener and then override focusGained and focusLost methods. Something like this:

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JTextField;

public class CustomFocusListener implements FocusListener {
    JTextField txt;
    String textWhenFocusGained;
    String textWhenFocusLost;

    public CustomFocusListener(JTextField txt, String textWhenFocusGained,
            String textWhenFocusLost) {
        this.txt = txt;
        this.textWhenFocusGained = textWhenFocusGained;
        this.textWhenFocusLost = textWhenFocusLost;
    }

    @Override
    public void focusGained(FocusEvent arg0) {
        txt.setText(textWhenFocusGained);
    }

    @Override
    public void focusLost(FocusEvent arg0) {
        txt.setText(textWhenFocusLost);
    }


}
于 2013-06-05T18:57:12.517 回答
0

Use this.

if(txtSomeText.getText().equals("Text here!")){
    txtSomeText.setText("");
于 2018-03-22T03:05:16.163 回答
0
/////////////// It's problem of lower and supper case you should make ur text in lower case in code. and for desing of jtextfield it's ok
    txtIdDeVoyageur = new JTextField();
    txtIdDeVoyageur.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
             if(txtIdDeVoyageur.getText().trim().toLowerCase().equals("id de voyageur")){
                 txtIdDeVoyageur.setText("");
                 txtIdDeVoyageur.setForeground(Color.black);
                }

        }
        @Override
        public void focusLost(FocusEvent e) {
            if (txtIdDeVoyageur.getText().trim().equals("") || 
                    txtIdDeVoyageur.getText().trim().toLowerCase().equals("id de voyageur"))
                {

                txtIdDeVoyageur.setText("id de voyageur");
                txtIdDeVoyageur.setForeground(new Color (153,153,153));

                }
        }
    });
于 2020-06-09T17:34:10.220 回答