views:

86

answers:

2

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.

+1  A: 

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.

camickr
Both of your answers works, to some extend. But there is a problem. The original border seems to be a compound-border, with the highlight as the outerborder, and a white 1px line border as the inner. I dont want the inner border! Note: I've on OS X, and the type of the original border is actually com.apple.laf.AquaTextFieldBorder
Frederik Wordenskjold
+1  A: 

You might be able to do this with a CompoundBorder.

Under Windows with the Nimbus look and feel I can reproduce the problem. I can get both borders to display with the following code:

  Border lineBorder = BorderFactory.createLineBorder(Color.WHITE, 2);
  Border originalBorder = nameField.getBorder();
  CompoundBorder compoundBorder = new CompoundBorder(lineBorder, originalBorder);
  nameField.setBorder(compoundBorder);
Aaron