Suraj Suraj - 7 months ago 28
Java Question

How to add JScrollPane on JTextArea

I am going to make a Simple Notepad. I have used

JTextArea
for writing some text in it and I want
scrollbar
on
JTextArea
. I have written few lines of code.

package project.notepad;

import javax.swing.*;
import java.awt.*;

public class Notepad extends JFrame {
private JTextArea area;
private JMenu filemenu;
private JMenu editmenu;
private JMenu formatmenu;
private JMenu helpmenu;
private JScrollPane scroll;

private JMenuBar menubar;
private JMenuItem newmenuitem;
private JMenuItem openmenuitem;
private JMenuItem savemenuitem;
private JMenuItem exitmenuitem;


public Notepad() {
initComponents();
setComponents();

setTitle("Simple Notepad");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(true);
setSize(600, 600);
setJMenuBar(menubar);


menubar.add(filemenu);
menubar.add(editmenu);
menubar.add(formatmenu);
menubar.add(helpmenu);
filemenu.add(newmenuitem);
filemenu.add(openmenuitem);
filemenu.add(savemenuitem);
filemenu.add(exitmenuitem);

add(area);
add(scroll);
}


public final void initComponents() {
scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
area = new JTextArea();
menubar = new JMenuBar();
filemenu = new JMenu(" File");
editmenu = new JMenu(" Edit");
formatmenu = new JMenu(" Format");
helpmenu = new JMenu(" Help");
newmenuitem = new JMenuItem(" New");
openmenuitem = new JMenuItem(" Open");
savemenuitem = new JMenuItem(" Save");
exitmenuitem = new JMenuItem(" Exit");
}

public final void setComponents() {
area.setSize(600, 600);
area.setBackground(Color.WHITE);
}


public static void main(String[] args) {
new Notepad();
}
}


I'm not sure where the issue lies.

Answer

There are three problems here :

1) You add the area to the JScrollPane, before area is initialized.

So you end up with a JScrollPane containing a nullcomponent.

To fix this, instantiate area before adding it to the JScrollPane.

2) You add area to the JFrame, then you add the JScrollPane containing area.

This is wrong, a Component can't be added several times. The last addition will win, so you end up with your JFrame containing a mix between a JTextArea, and a JScrollPane now containing null .

To fix this, juste remove add(area); .

3) You call setVisible too early

You should call setVisible(true), only when all components have been added.

The following code shows the according modifications to the two relevant methods (comments have been added for changes) :

public Notepad() {
    initComponents();
    setComponents();

    setTitle("Simple Notepad");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setResizable(true);
    setSize(600, 600);
    setJMenuBar(menubar);

    menubar.add(filemenu);
    menubar.add(editmenu);
    menubar.add(formatmenu);
    menubar.add(helpmenu);
    filemenu.add(newmenuitem);
    filemenu.add(openmenuitem);
    filemenu.add(savemenuitem);
    filemenu.add(exitmenuitem);

    //add(area); // remove this, the textarea is already added to the scrollpane
    add(scroll);

    // set the frame visible, only once all components have been added
    setVisible(true);
}

public final void initComponents() {

    area = new JTextArea(); // instantiate the textarea, before adding to the scrollpane
    scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    menubar = new JMenuBar();
    filemenu = new JMenu("    File");
    editmenu = new JMenu("    Edit");
    formatmenu = new JMenu("    Format");
    helpmenu = new JMenu("    Help");
    newmenuitem = new JMenuItem("    New");
    openmenuitem = new JMenuItem("    Open");
    savemenuitem = new JMenuItem("    Save");
    exitmenuitem = new JMenuItem("    Exit");
}