Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Change color of JTextfield border while preserving L&F

843805Oct 11 2006 — edited Oct 11 2006
In my JTextfield validation code, I'd like to change the JTextfield's border to red if there's an error - like the user attempted to enter "blah" instead of a numeric value "342.33".

So I'm doing something like this:
Border defaultBorder = (LineBorder)textField.getBorder();
textField.setBorder(new LineBorder(Color.red));
However, the red border doesn't have the same padding as the default L&F border, so the user's input gets moved 4-5 pixels to the left. I can probaly hack away at the code until I create a border with the same padding as the default JTextField but the spacing is probably different across different L&F's.

What I really want to do is this:
Border redBorder = new LineBorder(Color.red);
redBorder.setPadding(defaultBorder.getPadding());
Or..
Border redBorder = defaultBorder.clone();
redBorder.setForeground(Color.red);
But neither the Border Interface or the BorderFactory class seems to support anything like this.

To sum it up, all I want to do is change the JTextfield's border color to red without changing any other properties of the Border.

Any suggestions?

Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 8 2006
Added on Oct 11 2006
5 comments
324 views