Hey all. I'm making a program which uses many buttons and a textfield. I need help and some explaining on how to organise components within a panel.
For example,using

mainPanel.add(BorderLayout.NORTH,display);
mainPanel.add(BorderLayout.CENTER,background);
mainPanel.add(BorderLayout.EAST,buttonBox);				
mainPanel.add(BorderLayout.WEST,buttonBox2 );

All components (the buttons within the background panel,the buttons within buttonbox1/2 and the display field) are "sticked". How can i add a gap between .NORTH and .CENTER?
i have tried :

mainPanel.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));

but nope.
Also,The buttons in buttonboxes (box class) are Y_AXIS aligned but they also have no gap.
i tried buttonBox.createGlue(); but also nothing.
Ive been reading and messing around with gridLayouts,boxLayouts,borderFactory for a good 3 hours now...
Anyway,apart from my program,any small "guide" on how to easily play with components arrangement would be awesome.Thanks!

Dani AI

Generated

Short diagnosis and a few practical fixes.

— the layout manager (or the component’s own padding) is what controls gaps. An empty border on the outer panel creates an outer margin, not space between a NORTH child and the CENTER child; likewise createGlue() has no effect unless you add the returned component into the container. Two simple approaches usually solve this cleanly: insert a spacer component where you want a gap, or give the child component its own padding/margin.

Examples you can apply right away:

JPanel northWrap = new JPanel();
northWrap.setLayout(new BoxLayout(northWrap, BoxLayout.Y_AXIS));
northWrap.add(displayField);
northWrap.add(Box.createVerticalStrut(12)); // 12px gap between display and center
mainPanel.add(northWrap, BorderLayout.NORTH);

For vertically stacked buttons use struts or glue (add the glue to the box) and set button margins/alignment:

buttonBox.setLayout(new BoxLayout(buttonBox, BoxLayout.Y_AXIS));
buttonBox.add(btn1);
buttonBox.add(Box.createVerticalStrut(6));
buttonBox.add(btn2);
btn1.setAlignmentX(Component.CENTER_ALIGNMENT);
btn1.setMargin(new Insets(4, 8, 4, 8)); // internal padding for each button

If the center area is a grid of controls, give the grid layout gaps instead of forcing sizes:

background.setLayout(new GridLayout(0, 3, 8, 8)); // cols=3, hgap=8, vgap=8

Quick debugging tips: temporarily add visible borders to panels to see bounds (for example, a colored line border), and avoid hard-coded preferred sizes when you want flexible spacing. As suggested, GridBagLayout gives pixel-precise control for complex UIs but is more verbose; as noted, BorderLayout also supports horizontal/vertical gaps — often the simplest, most maintainable solution is a few small nested panels (BoxLayout/GridLayout) with struts/margins rather than one giant manager.

Recommended Answers

All 2 Replies

mainPanel.setLayout(new BorderLayout(10, 10));
mainPanel.add(display, BorderLayout.NORTH);
mainPanel.add(background, BorderLayout.CENTER);
etc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.