Example text in JTextField

后端 未结 7 2064
独厮守ぢ
独厮守ぢ 2021-01-18 05:15

I am looking for a way to put example text into a swing JTextField and have it grayed out. The example text should then disappear as soon as any thing is entered into that t

7条回答
  •  孤城傲影
    2021-01-18 05:46

    Rather than overriding, put a value in the field and add a KeyListener that would remove the value when a key stroke is registered. Maybe also have it change the foreground.

    You could wrap this up into your own custom JTextField class that would take the default text in a constructor.

提交回复
热议问题