Search code examples
javaswingawtjscrollpanejtextarea

Can't add JScrollPane to JTextArea


I've searched half the internet and I can't find anybody who has had this same problem.

I've tried several different ways to add a vertical scroll bar, but in vain. Every other post I've seen describing this problem did the following:

    JTextArea ta = new JTextArea();
    JScrollPane sc = new JScrollPane(ta);

and that seemed to work for them. But I'm out of luck and have tried at least thirty different ways that I've found. I'm new to java and the class I'm in is killing me. Thanks for your time.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui extends JFrame
private JTextArea outputArea {
    private JButton eastButton;
    private JButton westButton;
    private JButton northButton;
    private JButton southButton;
    private JButton helpButton;
    private JButton pickupButton;
    private JButton dropButton;
    private JButton eatButton;
    private JButton lookButton;
    private JButton listButton;
    private JScrollPane scroll;
    Gui() {
        int x = 0;
        int y = 0;
        int Width = 1;
        int Height = 2;
        int anchor;
        GridBagConstraints gbc = new GridBagConstraints();
        JLabel actionsLabel = null;
        JLabel directionsLabel = null;
        getContentPane().setBackground(Color.black);
        setTitle("Castle Quest");
        actionsLabel = new JLabel("Actions");
        actionsLabel.setForeground(Color.red);
        directionsLabel = new JLabel("Directions");
        directionsLabel.setForeground(Color.red);
        outputArea = new JTextArea(25, 35);
        scroll = new JScrollPane(outputArea);
        eastButton = new JButton("east");
        eastButton.setSize(100, 30);
        westButton = new JButton("west");
        westButton.setSize(100, 30);
        northButton = new JButton("north");
        northButton.setSize(100, 30);
        southButton = new JButton("south");
        southButton.setSize(100, 30);
        helpButton = new JButton("help");
        helpButton.setSize(100, 30);
        pickupButton = new JButton("pickup");
        pickupButton.setSize(100, 30);
        dropButton = new JButton("Drop");
        dropButton.setSize(100, 30);
        eatButton = new JButton("eat");
        lookButton = new JButton("look");
        lookButton.setSize(100, 30);
        listButton = new JButton("list");
        listButton.setSize(100, 30);
        outputArea.setEditable(true);
        GridBagLayout gbl = new GridBagLayout();
        setLayout(gbl);
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        add(actionsLabel, gbc);
        gbc.gridy += 2;
        add(helpButton, gbc);
        gbc.gridy += 2;
        add(pickupButton, gbc);
        gbc.gridy += 2;
        add(dropButton, gbc);
        gbc.gridy += 2;
        add(eatButton, gbc);
        gbc.gridy += 2;
        add(lookButton, gbc);
        gbc.gridy += 2;
        add(listButton, gbc);
        gbc.gridy = 1;
        gbc.gridx = 2;
        gbc.gridheight = 50;
        outputArea.setBackground(Color.red);
        add(outputArea, gbc);
        gbc.gridheight = 2;
        gbc.gridx = 3;
        gbc.gridy = 1;
        add(directionsLabel, gbc);
        gbc.gridy = 3;
        add(eastButton, gbc);
        gbc.gridy += 2;
        add(westButton, gbc);
        gbc.gridy += 2;
        add(northButton, gbc);
        gbc.gridy += 2;
        add(southButton, gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
    }
    public static void main(String[] args) {
        Gui newGame = new Gui();
        Game funTime = new Game();
        newGame.setSize(400, 150);
        newGame.setBackground(Color.black);
        newGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        newGame.pack();
        newGame.setVisible(true);
        newGame.outputArea.setLineWrap(true);
        newGame.outputArea.setWrapStyleWord(true);
        newGame.outputArea.append(funTime.getMessage() + "\n");
        newGame.eastButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                funTime.move("east");
                newGame.outputArea.append(funTime.getMessage() + "\n");
            }
        });
        newGame.westButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                funTime.move("west");
                newGame.outputArea.append(funTime.getMessage() + "\n");
            }
        });
        newGame.northButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                funTime.move("north");
                newGame.outputArea.append(funTime.getMessage() + "\n");
            }
        });
        newGame.southButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                funTime.move("south");
                newGame.outputArea.append(funTime.getMessage() + "\n");
            }
        });
        newGame.helpButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                funTime.getHelp();
                newGame.outputArea.append(funTime.getMessage() + "\n");
            }
        });
        newGame.pickupButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (funTime.currentLocation.hasItem()) {
                    funTime.pickupItem();
                    newGame.outputArea.append(funTime.getMessage() + "\n");
                }
            }
        });
        newGame.listButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < funTime.items.size(); i++) {
                    System.out.println(funTime.items.get(i));
                }
                System.out.println(funTime.items);
                System.out.println(funTime.currentLocation.getRoomItem1() + " & " + funTime.currentLocation.getRoomItem2());
            }
        });
    }
}

Solution

  • The problem you're facing (the JTextArea expanding) is caused by the fact that you added the JTextArea to the container instead of the JScrollPane.

    Here is the logic behind what you should do :

    JTextArea is contained by JScrollPane and JScrollPane is contained by the JFrame.

    Change this line

    add(outputArea, gbc);
    

    to this

    add(scroll, gbc);