radekEm
radekEm

Reputation: 4647

JTextField constant text when disabled

I'm looking for some smart way to solve my problem. I didn't find any solution in the Web. Actually I would ask you for any tip, clue...

I've got a dialog with many JTextFields. They represent some object's state (let's call it "model"). There are some cases in which some of these fields should be disabled. Despite the fact, that "model" contains values for these fields I do not want to display them (they are irrelevant). So, I would like to set "n/A" value in each disabled field (always when it's disabled).

The only idea I have is to create CustomJTextBox extends JTextBox and override the setText() method :

class CustomTextField extends JTextField {
    //... constructors
    @Override
    public void setText(String s){
        if(!this.isEnabled()){
            super.setText("n/A");
        } else {
            super.setText(s);
        }
    }
}

But it has lots of weak points :D

I guess it should exist some other approch... could you give any tips?

Upvotes: 1

Views: 159

Answers (1)

trashgod
trashgod

Reputation: 205885

Instead of extending a JTextComponent, use a static factory method, discussed here. In the factory, add a DocumentListener that enforces your requirement.

Upvotes: 2

Related Questions