Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add space between JFrame and JPanel

how I can add space between JFrame and a JPanel inserts into this JFrame? I would insert space in the way that the elements inside the JPanel didn't appear too near at the JFrame

like image 311
Mazzy Avatar asked Jan 14 '12 15:01

Mazzy


People also ask

How do you add spacing to a Java Swing?

There are a number of ways in a Swing GUI to provide a separation between components, and white space around components: JToolBar has the methods addSeparator() & addSeparator(Dimension) . JMenu uses a spacing component better suited to menus, available through addSeparator() .

How do I add a space between buttons in Java?

JToolBar(). add(component) − To add a component to the toolbar. JToolBar(). addSeparator(dimension) − To add required spacing between button on the toolbar.

How do you create a vertical space in Java?

Use vertical glue in a vertical BoxLayout and horizontal glue in a horizontal layout. For example, this will allow extra vertical space between two buttons. JPanel p = new JPanel(); p. setLayout(new BoxLayout(p, BoxLayout.


1 Answers

Set the JPanel's border with an EmptyBorder with appropriate parameters.

i.e.,

// caveat: code not tested
myPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 

If the JPanel already has a border, then you can either use a compound border or wrap the JPanel in another JPanel, say using BorderLayout and in the BorderLayout.CENTER position, and give the wrapper JPanel an empty border.

like image 175
Hovercraft Full Of Eels Avatar answered Oct 02 '22 13:10

Hovercraft Full Of Eels