Reputation: 123
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
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
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