Patrick
Patrick

Reputation: 351

Overwriting getText of JTextField

I wish to overwrite getText() so that it can return a null value instead of a a blank value. I noticed that getText() is a part of JTextComponent and I've been unable to successfully overwrite it. What is the best way to get the desired result?

Upvotes: 3

Views: 993

Answers (1)

mre
mre

Reputation: 44240

You can subclass JTextField and override the getText method, as such

public final class JCustomTextField extends JTextField{
    .
    .
    .
    @Override
    public String getText(){
        // do stuff
    }
}

Or you can inline the override, as such

JTextField textField = new JTextField(){
    .
    .
    .
    @Override
    public String getText(){
        // do stuff
    }  
};

Upvotes: 3

Related Questions