Search code examples
javajframescrollbarjscrollpane

Scrollbar not scrolling


I have a program that is designed simply to make and test a user interface. The current setup is that it displays a large black screen with text, and a box for user input at the bottom, and a scrollbar on the right side. I've got everything working the way I want it except that the scrollbar absolutely will not scroll. It is there, but doesn't seem connected at all to the textarea. You can press the buttons on the bar, but they don't do anything. Any help would be appreciated!

Here is the code I have so far:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class RandomTest extends JFrame implements KeyListener{
    JPanel panel = new JPanel();
    JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    JFrame frame = new JFrame();
    static JTextArea txt;
    static JTextField inputField;
    static String text;
    static String choice;
    static boolean enter = false;
    Container positioner = frame.getContentPane();


    RandomTest(){
        text = "";
        txt = new JTextArea(text);
        txt.setEditable(false);
        inputField = new JTextField("");
        txt.setBackground(Color.black);
        txt.setFont(new Font("Courier New", Font.PLAIN, 18));
        txt.setForeground(Color.lightGray);
        inputField.setBackground(Color.black);
        inputField.setFont(new Font("Courier New", Font.PLAIN, 18));
        inputField.setForeground(Color.lightGray);
        panel.add(txt);
        panel.add(inputField);
        //Dimension d = new Dimension(500,500);
        //scrollPane.setPreferredSize(d);
        panel.add(scrollPane);
        frame.add(panel);
        frame.setSize(500,500);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        positioner.setLayout(new BorderLayout());
        positioner.add(inputField, BorderLayout.PAGE_END);
        positioner.add(scrollPane, BorderLayout.EAST);
        positioner.add(txt, BorderLayout.CENTER);
        positioner.setBackground(Color.black);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        inputField.addKeyListener(this);
    }

    public static void main(String[] arg){
        new RandomTest();
        println("Please enter the letter 'm'");
        for(;;){
            println("/\n/\n/\n/\n/\n/\n/\n/\n");
            if(input().equals("m")){
                println("Thank you.");
            }else{
                println("Try again.");
            }
        }
    }

    public static void println(String line){
        text += line + "\n";
        txt.setText(text);
    }

    public static String input(){
        for(;;){
            if(enter == true){
                enter = false;
                return choice;
            }else{
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    if(enter == true){
                        enter = false;
                        return choice;
                    }
                }
            }
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            choice = inputField.getText();
            inputField.setText("");
            enter = true;
            try{
                Thread.currentThread().interrupt();
            }catch(Exception E){

            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }
}

Solution

  • There's nothing attached to the scroll pane's view, so there is nothing for it to scroll...

    You need to supply a component view for the scroll, using something like scrollPane.setViewportView(...);

    You may want to take a look at How to use scroll panes for some more details

    There's a lot of things in you code that worry me...

    Thread.sleep in a GUI environment is always of concern, given that this could actually cause your application to become unresponsive. The use of infinite loops, for the same reason.

    The use of KeyListener which is simply performing the same function of an ActionListener

    The fact that you are adding txt and inputField and scrollPane to two different containers and txt is actually supplementing panel...

    You may like to spend some time reading through