You must nest JPanels and use a combination of layouts. Placing panels containing JButtons in another JPanel that uses GridLayout (1, 0) (1 row, variable number of columns) can work, and placing this JPanel in the BorderLayout.NORTH position of BorderLayout-using JPanel can work.
for instance
import java.awt.*; import javax.swing.*; public class Foo003 { private static void createAndShowGui() { JButton btnA = new JButton("A"); JButton btnB = new JButton("B"); btnA.setPreferredSize(new Dimension(50, 25)); btnB.setPreferredSize(new Dimension(100, 25)); JPanel btnAPanel = new JPanel();
source share