Java Swing Components and Layout basic example

This is a basic example demonstrating a Swing frame with two components and a basic interaction between them.

You might also want to take a look at the bare minimum Swing app that is demonstrating all the boilerplate needed code demonstrated in this article.

The source code looks like this:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class BasicComponentsLayouts {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new InnerFrame("Hello Swing World!");

//Pay attention on that we are implementing ActionListener
//More comments to follow below on that
class InnerFrame extends JFrame implements ActionListener {

    //state of the frame: a button and some text
    private JButton button = new JButton("Click me!");
    private JTextArea text = new JTextArea();

    InnerFrame(String title){
        //boilerplate begins
        //boilerplate ends

        //Setting an easy Layout that is positioning components based
        //on intuitive compass signs
        setLayout(new BorderLayout());

        //Pay attention that the class is implementing ActionListener
        //therefore the button is adding this to its action listener.
        //This practically means every time the button is clicked
        //the overriden actionPerformed method of this class will be
        //called and will append some text to the text component

        //Last but not least adding the two components into the
        //frame passing as well as a second parameter their
        //position inside the layout
        add(text, BorderLayout.NORTH);
        add(button, BorderLayout.SOUTH);

    public void actionPerformed(ActionEvent actionEvent) {
       //If we are here it's because the button was clicked
       //therefore it's time to do something exciting with the
       //text component
       text.append("Button Clicked!");

Some comments:

  • This time we have a state in our frame, namely a button and a text area
  • Pay attention that our JFrame child is implementing ActionListener. This interface brings us the actionPerformed() overriden method that fires when this frame is used as a listener somewhere.
  • Other than the ceremonial code of frame visibility etc we have the setLayout() method where we define a nice basic BorderLayout(). For more information on layouts please have a look at this useful link.
  • Next stop is where we are adding our frame using its identity as ActionListener (we are implementing this interface remember?) to the button component. This effectively means that when the button gets clicked the ActionListener overriden method will fire.
  • Last piece of the puzzle before the end of the frame constructor and effectively completing the initialisation it’s the addition of both components to the frame using BorderLayout compass points depicted through static intuitive fields.
  • Finally we have the sole method of ActionListener which gets overriden in the Frame child class where we are not using its parameter but rather grabbing the text field (part of the frame state remember?) and appending a basic message that will reflect in a text string every time the listener gets invoked.

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