Java AWT application window padding

I'm trying to build a simple AWT application in Java. I want all of the containers in the main window to be separated by bit. I can accomplish this by setting the Hgap and Vgap in the BorderLayout constructor (see below.)

However, I can't figure out how to put a cap between the containers and the edges of the main window. How do I add a few pixels of padding to the main window?

import java.awt.*; import java.applet.Applet; public class LayoutTest extends Applet { public void init() { BorderLayout layout = new BorderLayout(8, 8); setLayout(layout); add(new Button("Left"), BorderLayout.CENTER); add(new Button("Right"), BorderLayout.EAST); } }


I agree with the other answers and would recommend using Swing (use JApplet instead), which would make all kinds of things easier (you could just call setBorder and use BorderFactory to create a border, for example), but in your case you can set insets by overriding getInsets:

@Override public Insets getInsets() { return new Insets(10,10,10,10); }

Replace 10 with whatever you like.

There doesn't appear to be a setter, or I would say to use that instead. If there is a better way to do this in the case of an AWT Applet, someone please correct me.

If you decide to use Swing, see: How to Use Borders


AWT is not the newest technology on the block. So unless you have a specific requirement to do work in AWT, I would recommend you to check out the modern replacements Swing or SWT - much more comfortable, flexible customizable and predictable in their behaviour than AWT.

One reason behind developing them was exactly that the kind of visual fine-tuning you are trying to do here is unnecessarily difficult (if not impossible) with AWT.


Whilst you could probably get away with setting the insets of the applet, I suggest moving to Swing (extend javax.swing.JApplet). Then set a JPanel as the content pane with a EmptyBorder set of the appropriate widths.

Also note, you will probably quickly have to move up to a more sophisticated layout manager, such as GridBagLayout.


  • Rad upload Java applet and z-index
  • How can I let the user select a folder from an HTML page so that all the files can be uploaded
  • Why is my JFrame not appearing in a web based Java app?
  • How to make the JDialog invisible when the JProgressBar reaches 100%?
  • Passing data from C to Java
  • Scraping flash using HtmlUnit or other java tool
  • how to get MAC address using any browser with javascript [duplicate]
  • What cross-browser technology do you use in your web applications to manipulate on client machines?
  • GridBagLayout padding
  • How to change display text in item template based on eval in gridview?
  • setSelected() with JRadioButton r[]=new JRadioButton[3] not working [duplicate]
  • How to rotate the dial in a gauge chart? Using python plotly
  • Plotting densities in R
  • WPF version of .ScaleControl?
  • Update Google Maps traffic layer without page reloading
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Installing iPhone App to iPhone
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Disable Enter in editText android
  • Android fill_parent issue
  • Pass value from viewmodel to script in zk
  • How to check if every primary key value is being referenced as foreign key in another table
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Eraser for UIBezierPath
  • jquery mobile loadPage not working
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • align graphs with different xlab
  • Compare two NSDates in iPhone
  • Cannot Parse HTML Data Using Android / JSOUP
  • Load html files in TinyMce
  • python regex in pyparsing
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • JTable with a ScrollPane misbehaving
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize