-1

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());
            }
        });
    }
}
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
pjones235
  • 540
  • 2
  • 18
  • 3
    I've seen a few of these basic java type questions, with roughly the same variable names. Are you all bad students or do you all share a bad lecturer? – Mathemats Dec 07 '15 at 23:15
  • 1
    What is your current output? If you keep pressing ENTER to get new lines, does the JScrollPane appears or not? Please define `does not work` – Yassin Hajaj Dec 07 '15 at 23:19
  • Bad instructor indeed. Right now when I get it, everything works (minus the scrollbar, obviously) until I've filled too many lines. Once the box is full, it'll continue creating new lines, expanding the textarea, and moving all the other components up, to the point where they all leave the screen. – pjones235 Dec 07 '15 at 23:28
  • Possible duplicate of [Adding a Scrollable JTextArea (Java)](http://stackoverflow.com/questions/8849063/adding-a-scrollable-jtextarea-java) – Vogel612 Dec 08 '15 at 12:18

1 Answers1

2

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);
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • 1
    Thanks! That works. However, my professor wants the scrollbar to always be visible. How could I go about that? I know it's probably simple, just struggling with this stuff. – pjones235 Dec 07 '15 at 23:32
  • No problem. Do not forget to upvote :). You just need to add this piece of code `scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);` and it will do the trick. – Yassin Hajaj Dec 07 '15 at 23:37
  • 1
    Can't upvote yet, but I will when I get there! Works perfect. Thank you so much! – pjones235 Dec 07 '15 at 23:38