Resize JTextArea only vertical


Staff member
What is the best way to resize JTextArea vertical? I think I shoud put JTextArea into JScrollPane (and I can make a simple pane with scrollbar). But I want to JTextArea scroll by mouse vertical. I tried to use class according to this tut.:
<a href="" rel="nofollow"></a>, but it is able to resize component with all sides.

I want something like here:

<a href=";preval=vertical" rel="nofollow">;preval=vertical</a>

or something like textarea which you use to post answer to topic here.


My piece of code with class ComponentResizer (from <a href="" rel="nofollow"></a>)

package textsamplerdemo;

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

public class TextSamplerDemo extends JPanel {

    public TextSamplerDemo() {
        setLayout(new BorderLayout());
        JTextArea textArea = new JTextArea(10, 20);
        JScrollPane scrollPane = new JScrollPane(textArea);
        ComponentResizer componentResizer = new ComponentResizer();
        add(scrollPane, BorderLayout.PAGE_START);

     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event dispatch thread.
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TextSamplerDemo");
        //Add content to the window.
        frame.add(new TextSamplerDemo());
        //Display the window.

    public static void main(String[] args) {
        //Schedule a job for the event dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.FALSE);