JTextArea and JTextField internal padding on text

n002213f picture n002213f · Feb 18, 2010 · Viewed 10.8k times · Source

I would like to increase the spacing / padding / insets for the JTextField and JTextArea. Effectively increase the spacing between the two red lines in the image below:

alt text

Answer

Luhar picture Luhar · Feb 18, 2010

You could also try using an EmptyBorder to put in gaps between the two components. If you already have a Border, you can use that in conjunction with an EmptyBorder to create a CompoundBorder. In the code fragment below, a new CompoundBorder is created that has a TitledBorder and an EmptyBorder which enforces a padding of 1 pixed around the component.

testPanel.setBorder(
   javax.swing.BorderFactory.createCompoundBorder(
      javax.swing.BorderFactory.createTitledBorder(
         null, "Border Title",
         javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
         javax.swing.border.TitledBorder.DEFAULT_POSITION,
         new java.awt.Font("Verdana", 1, 11)
      ),
      javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1)
   )
);