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");     
               JScrollPane pane = new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
               add(box, BorderLayout.CENTER);
            public static void main (String args[])
                Scrollers scroll = new Scrollers();
                scroll.setSize(500, 500);

Recommended Answers

All 3 Replies

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

Hello Ezzaral and mKorbel,

That was very helpful. Thank you!


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.