How to leave only one border in JTextField?

2

I would like to know how to just leave a border in a JTextField , similar to the data entry at the time of logging in Gmail, that is, with a border just at the bottom.

    
asked by anonymous 14.07.2017 / 01:37

1 answer

5

You can create borders with the MatteBorder as follows:

textField.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.BLUE));

The createMatteBorder method allows you to set the thickness of each edge of the component as well as the color. Just enter 0 in all but the third parameter, which is equivalent to the size of the bottom border.

See an example:

Of course, to get the effect of the image above, you need to apply the same background color as your screen to JTextField , something like:

textField.setBackground(seuFrame.getBackground());

or

textField.setBackground(painelDoTexField.getBackground());

If you only have one visible border, the background will be white, highlighting the field on the rest of the screen.

    
14.07.2017 / 01:40