wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Not able to update JTextPane in TreeSelectionListener Action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Not able to update JTextPane in TreeSelectionListener Action" Watch "Not able to update JTextPane in TreeSelectionListener Action" New topic
Author

Not able to update JTextPane in TreeSelectionListener Action

Ganesh Rayner
Greenhorn

Joined: Nov 05, 2009
Posts: 2
Hi,

I have JTree left side and and JTextPane rightside. On selecting some value in tree i am trying to load some data
I am inserting line by line using Doc.Insert in jtext pane.But all the lines are getting painted at single stretch instead of one by
one. I tried with some thing listed below. But none seems to work.

a) textPane.repaint();
b) textPane.setCaretPosition(doc.getLength());
c) textPane..scrollRectToVisible(new Rectangle(0,textPane.getHeight()-2,1,1));

When I set the break point and try to find the problem all the lines are getting painted when the control goes
out of tree selection listener.Is there any thing to do with action listener thread which is not updating the
JTextPane. I have placed the Code Flow here. Need some help.Thanks.



Regards
Ganesh M
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2204
    
    7
All code executes sequentially in the EDT, so yes all the data is added to the text pane and then when the event listener finishes executing the RepaintManager is invoked and the text pane get painted.

If you want the text to be painted after each line is added then you can try wrapping the insertString(...) method in a Swing Utilities.invokeLater() which will cause the code to be added to the end of the EDT and therefore the text pane will be painted one line at a time. Of course this will happens so fast that you won't even notice it being painted one line at a time.

If you really want to animate the painting of each line, then you need to use a Swing Timer to schedule the insertion of each line of text.

I suggest you read the section from the Swing tutorial on "Concurrency" for more information. There is also a section on "How to Use Timers".
Ganesh Rayner
Greenhorn

Joined: Nov 05, 2009
Posts: 2
Hi Rob Camick,

Thanks for the detailed response. I cannot use Swing utilities which is the part of java 1.6. For our application
we are using java 1.5. We have to stick with 1.5 for a while. I tried timer as mentioned in the Java Swing timers.
But not seems to be working. Currently i have created a seperate thread and updating the text pane. It is littlle
bit slow but seems to be ok. The only problem is light flickering is happening when i am updating the lines.Is it
possible to make the repaint state idle for while till i am updating.Because when the lines are getting added
Autoscroll should not happen. I have disabled that.But still flickering is there.Any thing i can do about that?

Once again thanks for the reply.I will be using Swing Utilities method when i am using java 1.6.


Best Regards
Ganesh M
santhosh varala kumar
Greenhorn

Joined: Nov 05, 2009
Posts: 25
Ganesh Rayner wrote:Hi Rob Camick,

Thanks for the detailed response. I cannot use Swing utilities which is the part of java 1.6. For our application
we are using java 1.5. We have to stick with 1.5 for a while. I tried timer as mentioned in the Java Swing timers.
But not seems to be working. Currently i have created a seperate thread and updating the text pane. It is littlle
bit slow but seems to be ok. The only problem is light flickering is happening when i am updating the lines.Is it
possible to make the repaint state idle for while till i am updating.Because when the lines are getting added
Autoscroll should not happen. I have disabled that.But still flickering is there.Any thing i can do about that?

Once again thanks for the reply.I will be using Swing Utilities method when i am using java 1.6.


Best Regards
Ganesh M



Be-careful while implenting document.insertString with respect to multiple threads. Their is chances UI may hang. so always prefer using SwingUtilities method and do above operations
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2204
    
    7
SwingUtilities has been around forever.
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
I did a little example with javax.swing.Timer for inserting words. Did this with NetBeans i'm usin the latest JDK, only setted "source/binary format" to JDK 5 from project properties.

No flickering, on insertion i had null value for AttributeSet. And scroll panel scrolls automically.

Or was i understanding your problem somehow wrong...

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to update JTextPane in TreeSelectionListener Action