Solved

Adding JScrolBar to JTextArea

Posted on 2006-07-21
20
666 Views
Last Modified: 2011-09-20
How to add JScrolBar to JTextArea and make it automatically visible if needed?

If I have:

JTextArea textArea = new JTextArea();
0
Comment
Question by:javamate06
  • 8
  • 8
  • 3
  • +1
20 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17159030
JScrollPane pane = new JScrollPane(textArea);

then add the pane to your gui instead of your textArea
0
 

Author Comment

by:javamate06
ID: 17159046
I mean JScrolBar beacuse I  tried
JScrollPane pane = new JScrollPane(textArea);
but it does not work
0
 
LVL 92

Expert Comment

by:objects
ID: 17159054
a scroll pane will add scrolls as required.
or u can change the policy if u always want them to appear
0
 

Author Comment

by:javamate06
ID: 17159074
When I do this

JScrollPane pane = new JScrollPane(textArea);

The textArea is not visible in the frame.
0
 
LVL 92

Expert Comment

by:objects
ID: 17159098
are you adding the scroll pane to your frame?
and *not* adding the text area.
0
 

Author Comment

by:javamate06
ID: 17159110
Yes,

I did this:


JScrollPane pane = new JScrollPane();

this.getContentPane().add(pane);

pane = new JScrollPane(textArea);


is it correct?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17159145
On the 3rd line you throw the 1st pane away!

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17159147
Should be:

JScrollPane pane = new JScrollPane(textArea);

this.getContentPane().add(pane);

;JOOP!
0
 

Author Comment

by:javamate06
ID: 17159214
The text is visible now,

Still the scroll bar does not working and it is not visible
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17159244
Of course not when the text area is smaller than the panel.
Just fill it with lots of text and the scroll bars as needed appear.

You can also force the scrollbars into sight by:

       pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
       pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

but you will probably not like that after some time.

;JOOP!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:javamate06
ID: 17159592
"Of course not when the text area is smaller than the panel.
Just fill it with lots of text and the scroll bars as needed appear."

It was needed but it was not appear.
0
 

Author Comment

by:javamate06
ID: 17159612
OK...

Can some one help me and give me sample exaple:

TextArea with scrollBar.


0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17159629
I gave you a correct and working example.
To what are you adding this JScrollPane?

Try this to see it work:

JFrame fr = new JFrame();
fr.setSize(400, 300);
fr.getContentpane().add(pane, BorderLayout.SOUTH);

And understand why you didn't see it before.

;JOOP!
0
 

Author Comment

by:javamate06
ID: 17159858
---------------------------

Lets I have this class

I want to show the all strings written in the TextArea by sorollBar movement down and up.

----------------------------
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JTextArea;
import java.awt.Rectangle;

public class Frame1 extends JFrame
{
  private JTextArea textArea = new JTextArea();

  public Frame1()
  {
    try
    {
      test();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

  }

  private void test() throws Exception
  {
    this.getContentPane().setLayout(null);
    this.setSize(new Dimension(263, 103));
    this.setTitle("Text Area Test");
    textArea.setText("Test\ntest\ntest\nTest\ntest\ntest");
    textArea.setBounds(new Rectangle(10, 5, 230, 50));
    this.getContentPane().add(textArea, null);
  }
}
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17159865
>>>     this.getContentPane().setLayout(null);  // ????????????????????????????????? Nonsense.

>>>     textArea.setBounds(new Rectangle(10, 5, 230, 50));  // Why? That spoils the whole thing.
 
I don't know how you have come to that code. Why fumble in constructors?
Use my code instead.

;JOOP!
0
 

Author Comment

by:javamate06
ID: 17159893
When I call it from the main method it is working

what is wrong in it?


The problem in the ScrollBar only I cannnot do it
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17159897
Delete that stupid "setLayout(null);"
Do not set the size on the text area! You want it to scroll? Set a size on the JScrollPane().

;JOOP!
0
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 50 total points
ID: 17159904
HI,

Just try  

import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JTextArea;
import java.awt.Rectangle;

public class Frame1 extends JFrame
{
  private JTextArea textArea = null;

  public Frame1()
  {
    try
    {
      test();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

  }

  private void test() throws Exception
  {
  //  this.getContentPane().setLayout(null);
    this.setSize(new Dimension(263, 103));
    this.setTitle("Text Area Test");
//    textArea.setText("Test\ntest\ntest\nTest\ntest\ntest");
 //   textArea.setBounds(new Rectangle(10, 5, 230, 50));

 textArea= new TextArea("Test\ntest\ntest\nTest\ntest\ntest\ntest\ntest\ntest\ntest\ntest", 5, 40);

JScrollPane pane = new JScrollPane(textArea);
this.getContentPane().add(pane);
}
}

this must work.

Thank You.

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17159909
May be he listens to you ............................................
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17162314
He finally did.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java. non-English characters encoding problem. intellij idea 3 74
Starting to learn JAVA, 7 60
DO we need Java installed on a Windows PC and WHY ? 13 102
JAVA part two 5 58
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now