BorderLayout tutorial in java swing

Border layout is one of the simple layouts used in java swing. Border layout is mostly used to layout the components in a container in five regions such as north, south, east, west and center.

When we add component using BorderLayout , BorderLayout provides five constants using which we can decide the region where we want to place the components. BorderLayout offers constant such as NORTH, SOUTH, EAST, WEST, and CENTER. By default if we do not provide any constant, then BorderLayout interpret it as CENTER as constant and place that component in CENTER.

Let see how we can use these constants:
Panel borderLayoutPanel = new Panel();
borderLayoutPanel .setLayout(new BorderLayout());
borderLayoutPanel.add(new Button("BORDER"), BorderLayout.NORTH);


JFrame borderLayoutFrame = new JFrame("BorderLayout Demo");
        JButton btn_north = new JButton("(NORTH)");
        JButton btn_center = new JButton("(CENTER)");
        JButton btn_east = new JButton("(EAST)");
        JButton btn_west = new JButton("(WEST)");
        JButton btn_south = new JButton("(SOUTH)");

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(btn_north, BorderLayout.NORTH);
        panel.add(btn_center, BorderLayout.CENTER);
        panel.add(btn_east, BorderLayout.EAST);
        panel.add(btn_west, BorderLayout.WEST);
        panel.add(btn_south, BorderLayout.SOUTH);


        borderLayoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        borderLayoutFrame.setSize(500,200);
        borderLayoutFrame.getContentPane().add(panel);
        borderLayoutFrame.setVisible(true);



In addition BorderLayout also supports relative postioning constants such as PAGE_START, PAGE_END, LINE_START, LINE_END, BEFORE_FIRST_LINE, AFTER_FIRST_LINE,BEFORE_LINE_BEGINS, AFTER_LINE_ENDS etc.
JFrame borderLayoutFrame = new JFrame("BorderLayout Demo");
        JButton btn_page_start = new JButton("(PAGE_START)");
        JButton btn_center = new JButton("(CENTER)");
        JButton btn_line_start = new JButton("(LINE_START)");
        JButton btn_page_end = new JButton("(PAGE_END)");
        JButton btn_line_end = new JButton("(LINE_END)");

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(btn_page_start, BorderLayout.PAGE_START);
        panel.add(btn_center, BorderLayout.CENTER);
        panel.add(btn_line_start, BorderLayout.LINE_START);
        panel.add(btn_page_end, BorderLayout.PAGE_END);
        panel.add(btn_line_end, BorderLayout.LINE_END);


        borderLayoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        borderLayoutFrame.setSize(500,200);
        borderLayoutFrame.getContentPane().add(panel);
        borderLayoutFrame.setVisible(true);


Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment