954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Question about JTextArea

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);
            }
         
            
 }
javaprog200
Newbie Poster
21 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

Hello Ezzaral and mKorbel,

That was very helpful. Thank you!

Regards

javaprog200
Newbie Poster
21 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You