Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep the highlight border of a JComponent, when custom border is set

I have a JTextField, where I've set some custom properties:

nameField.setPreferredSize(new Dimension(275,40));
nameField.setBackground(bgColor);
nameField.setForeground(txtColor);
nameField.setFont(new Font("HelveticaNeue",Font.PLAIN,22));
nameField.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));

When the component has focus, there is no highlight shown around the field. On a Mac, it is usually a blue glowing rectangle, indicating that this component has focus.

If I comment out the nameField.setBorder(...), the highlight reappears. How do I keep the highlight, but also my custom border!?

Basically, I just want the highlight-border to show when the component has focus, and no border when the component is unfocused.

Note that the original border is of type com.apple.laf.AquaTextFieldBorder.

like image 871
Frederik Wordenskjold Avatar asked Dec 27 '25 15:12

Frederik Wordenskjold


1 Answers

Basically, I just want the highlight-border to show when the component has focus, and no border when the component is unfocused.

The you need need to use a FocusListener. First you need to save the current Border. Then on focusLost you set the Border to null and on focusGained you use the saved Border.

Or you can get the default Border for the component by using the UIManager.

like image 178
camickr Avatar answered Dec 31 '25 18:12

camickr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!