Skip to content

More on GUI and layout manager

September 12, 2011

I have added to the previous example where I used a FlowLayout manager to assist in GUI layout, replacing with the BorderLayout, which provides a bit more flexibility and allows you to work with more elements in the GUI.

The BorderLayout manager has a concept of regions, where you specify which region (north, south, center, east, west) you want the element to appear in.  BorderLayout only supports ONE item per region.  If you have a region, say the top of the GUI or NORTH region where you will want to add multiple GUI elements, like buttons, you will have to introduce a panel.  A panel will allow you to add multiple elements to it and will even allow you to add its own layout manger.  Then you can add the panel to the outer container frame.

Also, i am showing all the class library files included in this example, as apposed to just including the javax.swing.*; libraries.  This is provide a more clear picture of all the classes at play in this example.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Buttons_implemented {

    public Buttons_implemented(){
        JButton btn = new JButton("Button 1");
        JButton btn2 = new JButton("Button 2");
        //register button listener events
        btn.addActionListener(new Btn_ClickHandler());
        btn2.addActionListener(new Btn2ClickHandler());
        //define textarea and a label
        JLabel label = new JLabel("Enter something below:");
        JTextArea txt_area = new JTextArea(4,40);
        //add a container element to place buttons, textarea and label
        JFrame main_frame = new JFrame();
        //use the BorderLayout to manage layout of elements
        // BorderLayout has a concept of regions, like North,South,East,West,Center
        main_frame.setLayout(new BorderLayout());
        since BorderLayout will only allow 1 item per region, 
        I am creating a panel and adding the buttons to the panel
        and then adding the panel to the frame
        JPanel button_panel = new JPanel();
        //add the button panel, textarea and label to the frame
        main_frame.add(BorderLayout.NORTH, button_panel);//top of the frame
        main_frame.add(BorderLayout.CENTER, label);//center
        main_frame.add(BorderLayout.SOUTH, txt_area);//bottom
        //prepare the frame for display
        main_frame.setTitle("Some title for this GUI window");
    public static void main(String args[]){
        //create new object to demo
        new Buttons_implemented();

 class Btn_ClickHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        //some action
        JOptionPane.showMessageDialog(null, "button 1 clicked", "title", JOptionPane.ERROR_MESSAGE);
 class Btn2ClickHandler implements ActionListener{
     public void actionPerformed(ActionEvent e){
         JOptionPane.showMessageDialog(null, "button 2 clicked", "title", JOptionPane.ERROR_MESSAGE);


From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: