Search code examples
javaswingborderjtextfield

Why my JTextField has changed after set new Border


I would like to know why my element JTextField has change height after setting a new Border:

  packagesAddField.setBorder(BorderFactory.createLineBorder(Color.RED));

after this code my JTextField changing a height (about 1px from upper and 1px from footer). It is possible, when i set some border he can change appearance of component?


Solution

  • In a word, don't. The setBorder() API recommends that you add the border to the enclosing container:

    Although technically you can set the border on any object that inherits from JComponent, the look and feel implementation of many standard Swing components doesn't work well with user-set borders. In general, when you want to set a border on a standard Swing component other than JPanel or JLabel, we recommend that you put the component in a JPanel and set the border on the JPanel.

    Alternatively, consider an InputVerifier.