Read about How to use toolbars .
The following code is taken directly from doc .
public ToolBarDemo() { super(new BorderLayout()); ... JToolBar toolBar = new JToolBar("Still draggable"); addButtons(toolBar); ... setPreferredSize(new Dimension(450, 130)); add(toolBar, BorderLayout.PAGE_START); add(scrollPane, BorderLayout.CENTER); }
See here BorderLayout . And make the necessary changes to your code.
UPDATE:
I tried using your code that shows the output as follows. I used the addSeparator method with dimension. This is just a try to solve the problem. I am not sure whether this approach is the correct way.

public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(new BorderLayout()); JToolBar toolBar = new JToolBar(); panel.add(toolBar,BorderLayout.PAGE_START); toolBar.addSeparator(new Dimension(150, 0)); JButton button1 = new JButton("Click Me"); toolBar.add(button1); frame.setLayout(new BorderLayout()); frame.add(panel, BorderLayout.CENTER); frame.setSize(new Dimension(400, 100)); frame.setVisible(true); }
source share