Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to update JTextPane in TreeSelectionListener Action

 
Ganesh Rayner
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2584
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2584
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SwingUtilities has been around forever.
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic