title: Transparent border of JTextField in Swing
body: <p>I have a searchfield in a JPanel. The JPanel has a background image. I want to place a JTextField in this panel, but the JTextField should have a tranparent border, so it has some padding to the top and bottom where you should see the background image of the panel.</p>
  
  <p>I have a SSCCE here, where the panel has a blue background instead of a background image. I want the textfield to be white, but with a border around it, which let's you see the blue panel below it. A transparent border would be great, but EmptyBorder doesn't work. Here is the SSCCE:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Color;
  
  import javax.swing.BorderFactory;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  
  public class TransparentBorder
  {
  
      public TransparentBorder()
      {
          //Create and set up the window.
          final JFrame frame = new JFrame("Transparent Border");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(600, 100);
  
          // set window size
          frame.setUndecorated(false);
  
          //P1
          final JPanel panel = new JPanel(new BorderLayout());
          panel.setBackground(Color.BLUE);
  
          final JButton btn = new JButton("Search");
          btn.setOpaque(false);
          panel.add(btn, BorderLayout.EAST);
  
          final JTextField field = new JTextField();
  
          field.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
          //        field.setOpaque(false);
          panel.add(field);
  
          frame.add(panel);
          //Display the window.
          //        frame.pack();
          frame.setVisible(true);
  
      }
  
      public static void main(final String[] args)
      {
          //Schedule a job for the event-dispatching thread:
          //creating and showing this application's GUI.
          javax.swing.SwingUtilities.invokeLater(new Runnable()
          {
              public void run()
              {
                  new TransparentBorder();
              }
          });
      }
  }
  </code></pre>
  
  <p>thanks a lot.</p>
  
  <hr>
  
  <p>Thanks for your answer Guillaume. My JTextfield is inside a JPanel with Borderlayout but the NORTH and SOUTH part were not occupied. So I used Box.createVerticalStrut(10) and added it to NORTH and SOUTH. This also worked for me. But I keep your suggestion in mind, so I have another solution in case I use the NORTH and SOUTH for other components.</p>