Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am working on a project for my college course. I was just wondering if anyone knew how to add a scrollBar to a JTextArea. At present I have the GUI laid out correctly, the only thing missing is the scroll bar.

This is what the GUI looks like. As you can see on the second TextArea I would like to add the Scrollbar.

GUI

This is my code where I create the pane. But nothing seems to happen... t2 is the JTextArea I want to add it to.

scroll = new JScrollPane(t2);
    scroll.setBounds(10,60,780,500);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

Any help would be great, thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

The Scroll Bar comes when your text goes beyond the bounds of your view area. Don't use Absolute Positioning, for such a small talk at hand, always prefer Layout Managers, do read the first para of the first link, to know the advantage of using a Layout Manager.

What you simply need to do is use this thingy :

JTextArea msgArea = new JTextArea(10, 10);
msgArea.setWrapStyleWord(true);
msgArea.setLineWrap(true);      

JScrollPane msgScroller = new JScrollPane();        
msgScroller.setBorder(
    BorderFactory.createTitledBorder("Messages"));
msgScroller.setViewportView(msgArea);

panelObject.add(msgScroller);

Here is a small program for your understanding :

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

public class JTextAreaScroller
{
    private JTextArea msgArea;
    private JScrollPane msgScroller;
    private JTextArea logArea;
    private JScrollPane logScroller;
    private JButton sendButton;
    private JButton terminateButton;
    private Timer timer;
    private int counter = 0;
    private String[] messages = {
                                    "Hello there
",
                                    "How you doing ?
",
                                    "This is a very long text that might won't fit in a single line :-)
",
                                    "Okay just to occupy more space, it's another line.
",
                                    "Don't read too much of the messages, instead work on the solution.
",
                                    "Byee byee :-)
",
                                    "Cheers
"
                                };

    private ActionListener timerAction = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            if (counter < messages.length)
                msgArea.append(messages[counter++]);
            else
                counter = 0;
        }
    };

    private void displayGUI()
    {
        JFrame frame = new JFrame("Chat Messenger Dummy");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(0, 1, 5, 5));

        logArea = new JTextArea(10, 10);
        logArea.setWrapStyleWord(true);
        logArea.setLineWrap(true);      

        logScroller = new JScrollPane();        
        logScroller.setBorder(
            BorderFactory.createTitledBorder("Chat Log"));
        logScroller.setViewportView(logArea);

        msgArea = new JTextArea(10, 10);
        msgArea.setWrapStyleWord(true);
        msgArea.setLineWrap(true);      

        msgScroller = new JScrollPane();        
        msgScroller.setBorder(
            BorderFactory.createTitledBorder("Messages"));
        msgScroller.setViewportView(msgArea);

        centerPanel.add(logScroller);
        centerPanel.add(msgScroller);

        JPanel bottomPanel = new JPanel();

        terminateButton = new JButton("Terminate Session");
        terminateButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                if (timer.isRunning())
                    timer.stop();
                else
                    timer.start();
            }
        });
        sendButton = new JButton("Send");

        bottomPanel.add(terminateButton);
        bottomPanel.add(sendButton);

        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(bottomPanel, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        timer = new Timer(1000, timerAction);
        timer.start();
    }

    public static void main(String... args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new JTextAreaScroller().displayGUI();
            }
        });
    }
}

Here is the outcome of the same :

JTEXTAREA SCROLLER


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...