Java Swing How to - Compare border layout position options








Question

We would like to know how to compare border layout position options.

Answer

import java.awt.BorderLayout;
//from   w w  w .j  a  v a  2s .  c  o m
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class Main {

  public static void showFrameWithToolBar(String toolBarPosition) {

    JPanel gui = new JPanel(new BorderLayout());

    JToolBar tb = new JToolBar();

    gui.add(tb, toolBarPosition);
    tb.add(new JButton("Button 1"));
    tb.add(new JButton("Button 2"));
    tb.addSeparator();
    tb.add(new JButton("Button 3"));
    tb.add(new JCheckBox("Check 1", true));

    JFrame f = new JFrame(toolBarPosition);
    f.setContentPane(gui);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationByPlatform(true);
    f.pack();

    f.setSize(400, 120);
    f.setVisible(true);
  }

  public static void main(String[] args) {

    showFrameWithToolBar(BorderLayout.PAGE_START);
    showFrameWithToolBar(BorderLayout.PAGE_END);
    showFrameWithToolBar(BorderLayout.LINE_START);
    showFrameWithToolBar(BorderLayout.LINE_END);
    showFrameWithToolBar(BorderLayout.CENTER);

  }
}