Question

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?

Was it helpful?

Solution

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
    }  
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top