Java Swing Scrollbar

This is a quick demo on how to add a Java Swing scrollbar following the footpaths of this previous example if you need detailed explanation on the Swing construct so far.

Without further ado the code looks like so:

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

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

//Branching out of a JPanel to create our more complicated
//component which for now will only serve as a wrapper around
//JTextArea, with its own layout and its scrollable functionality
class MyTextComponent extends JPanel {

    //state of our custom text
    private final JTextArea text = new JTextArea();

       //this component has a layout in its own right
       setLayout(new BorderLayout());
       add(getScrollable(), BorderLayout.CENTER);

    //A fancy way to confine in a method the sole purpose
    //of this whole exercise: to wrap JTextArea around a
    //JScrollPane, serving when we are adding the
    //component into the panel
    private JScrollPane getScrollable(){
        //this is where the magic happens wrapping JScrollPane
        //around JTextArea
        return new JScrollPane(this.text);

    void append(String text){

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

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

    MyFrame(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.CENTER);
        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!");
  • All we are doing in this example following incrementally this previous example so far (you can follow the comments if you want to skip reading the article) is to add a separate class to wrap around a JTextArea component.
  • The sole state of the new class is the JTextArea and in its constructor includes a separate BorderLayout. Before adding the JTextArea into its own layout it is wrapping it around JScrollPane which gives the effect of horizontal/vertical side scrolls effect into our text area inside of the broader frame.

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