DamDev
DamDev

Reputation: 123

JTextField - how to set background color?

I want to know how to set color of my JTextField component. I tried this way:

setBackground(Color.white)

and it sets a white color, but when the field is marked. I want the field to be white immediately after my program starts and without any user interaction.

Thanks for help!

Upvotes: 5

Views: 39586

Answers (2)

Raza Ali Poonja
Raza Ali Poonja

Reputation: 1106

You can change background color by this code

textField.setBackground(Color.RED);

You should also check this Change JTextField enabled background color

Upvotes: 6

user1982116
user1982116

Reputation: 141

I cannot replicate the problem you're having. Perhaps you are initially seeing the background color of the component holding the JTextField, rather than the JTextField's background.

JFrame f = new JFrame();
f.setBackground(Color.BLUE);
f.setLayout(new GridLayout(1, 1));

JTextField tf = new JTextField();
tf.setBackground(Color.GREEN);
f.add(tf);

f.setSize(500, 500);
f.setVisible(true);

Thread.sleep(2500);
tf.setBackground(Color.RED);

Upvotes: 0

Related Questions