Swing Tutorial 4: JPanel and JScrollBar

Guest Posts ljc1999

In this tutorial we are going to add a JPanel to our window and add a JScrollBar to our text area. A JPanel helps a JFrame by managing itself and everything on it better and separate from the JFrame. A JPanel also helps the coder because it splits up the code for different components into separate classes, that would otherwise be in one very large JFrame class. A JScrollBar is just a normal scroll bar that appears when needed on many components.

To get started create a new class called something like "TextPanel" then click finish. You need to make your new class extend JPanel and import it, your new class should now look like this:

import javax.swing.JPanel;

public class TextPanel extends JPanel {

}



Give your new class a constructor:

public TextPanel() {
    
}



Lets add a JTextArea to this JPanel class, the same way we did before, your class should now look like:

import javax.swing.JPanel;
import javax.swing.JTextArea;

public class TextPanel extends JPanel {
    private JTextArea textArea;
    
    public TextPanel() {
        textArea = new JTextArea();
    }
}



JPanels also uses Layout Managers, lets give it the border layout and import it:

setLayout(new BorderLayout());



Now lets add the JTextArea we created to the panel using the BorderLayout.CENTER.

Before, though, we did this:

add(textArea, BorderLayout.CENTER);



But to give a component scroll bars, you need to do it like this and import JScrollPane:

add(new JScrollPane(textArea), BorderLayout.CENTER);



To make our new JTextArea add a string when we click the button, lets add a new method to our JPanel class:

public void addText(String text) {
    textArea.append(text);
}



If you run this now there isn't a difference, thats because we've created our JPanel class but we haven't told our JFrame to use it!

As we have created a new JTextArea in our JPanel class, we can delete all the previous JTextArea code from our MainFrame class.


Now create a new instance of our JPanel class in our MainFrame class, under the button instance:

private TextPanel textPanel;



Then initialize it in the constructor:

textPanel = new TextPanel();



Then add it to our JFrame:

add(textPanel, BorderLayout.CENTER);



And finally, to make the text be added to the JTextArea when we click the button, put this inside the actionPerformed method:

textPanel.addText("Hello\n");



Now if you run it, it will work exactly the same as before, except, it has its own JPanel and the JTextArea will have scroll bars appear when the text is too long!

MainFrame.class

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class MainFrame extends JFrame {
    
    private JButton btn;
    private TextPanel textPanel;
    
    public MainFrame() {
        super("Hello World");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(600, 500);
        setLayout(new BorderLayout());
        
        btn = new JButton("Click Me!");
        textPanel = new TextPanel();
        
        add(btn, BorderLayout.SOUTH);
        add(textPanel, BorderLayout.CENTER);
        
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textPanel.addText("Hello\n");
            }
        });
    }
}



TextPanel.class

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class TextPanel extends JPanel {
    private JTextArea textArea;
    
    public TextPanel() {
        textArea = new JTextArea();
        setLayout(new BorderLayout());
        add(new JScrollPane(textArea), BorderLayout.CENTER);
    }
    
    public void addText(String text) {
        textArea.append(text);
    }
}