Solved

JTextArea with JScrollPane

Posted on 2004-08-30
9
606 Views
Last Modified: 2008-02-26
Problem:
In a Java Swing Application (JFrame), I added JScrollPane and JButton. Inside JScrollPane,  a JTextArea is added. The problem is, when i press the button, it is dynamically adding some text to JTextArea continuously. During the time, the scrollbar is not coming in JScrollPane. But, at the end it is coming. I want the scrollbar has to come during run-time(in loop execution).

Before post this question, i searched in some of the discussion forums. but i cannot find the answe for the above problemr. If it is not possible, I want the solution for that situation in any other way. Please write the code also.

Thanks a lot.

-R.Ramaraj
0
Comment
Question by:itramaraj
9 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 11931208
>> it is dynamically adding some text to JTextArea continuously.
Don't get that one.

>> But, at the end it is coming.
At the end of what?
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11931236
One way that might work is to refresh/repaint the JScrollPane. That may not be a very good way but will work.

0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 11935901
Perhaps add the scrollbar at the beginning. After creating the JScrollPane put these in (if you need a horizontal scrollbar change "never" to "always"):
scrollPaneObject.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPaneObject.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
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.

 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 11937313
inside your loop, add the text using:

EventQueue.invokeLater(new Runnable()
{
   public void run()
   {
      // append text here
   }
});
0
 
LVL 6

Expert Comment

by:expertmb
ID: 11938509
>> During the time, the scrollbar is not coming in JScrollPane. But, at the end it is coming. I want the scrollbar has to come during run-time(in loop execution).

if you dont set any scrollbar policy, default scrollbar policy is applied.
VERTICAL_SCROLLBAR_AS_NEEDED
HORIZONTAL_SCROLLBAR_AS_NEEDED

thts why scrollbar is appearing at the end.




0
 
LVL 6

Expert Comment

by:expertmb
ID: 11938529
>>I want the scrollbar has to come during run-time(in loop execution).  
before entering into the loop set the scrollbar policy.
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 11938532
Aye. To go a bit further on what expertmb said: The scrollbar is needed partway through the thread in which the text is being added. However, the "GUI" thread is yielding to the "processing" thread, and until the "processing" thread pauses, the "GUI" thread will not update (even with a validate/repaint). The simple solution is as I suggested above, add the scrollbars at the beginning. :)
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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

895 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

18 Experts available now in Live!

Get 1:1 Help Now