Hello,

The following program displays the text area for the entire JFrame even though I have set the dimensions of the text area to be 10 x 15. Any help will be greatly appreciated.

Thank you!

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

public class Scrollers extends JFrame {

            JTextArea area;            
            JButton button;
            
            public Scrollers ()
            {
               super ("Text Area and Scroller");
               Box box = Box.createVerticalBox();
               String m  = "I am here";
               area = new JTextArea(m, 10, 15);
               button = new JButton ("Click");     
               //area.setLineWrap(true);
               JScrollPane pane = new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
               
               box.add(pane);
               box.add(button);
               add(box, BorderLayout.CENTER);
             
            }
            
            public static void main (String args[])
            {
                Scrollers scroll = new Scrollers();
                scroll.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                scroll.setSize(500, 500);
                scroll.setVisible(true);
            }
         
            
 }

Components placed in BorderLayout.CENTER expand to fill the available space in the container.

if you set JTextArea(m, 10, 15); , then scroll.setSize(500, 500); is too contraproductive, call for scroll.pack(); before scroll.setVisible(true); rather than manually set for size on the screen

This question has already been answered. Start a new discussion instead.