• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTextPane Flicker During Image Swap

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I was wondering if someone wiser in the ways of Swing could help with an issue I'm having (see code below). You can run this JTextPane slide show demo code as-is, just substitute some images you have laying around for the slide show in the images[] array. The code assumes they are local to the file system.

The program works fine, but it doesn't always refresh the screen cleanly. I've seen a thumbnail of the graphic show up near top/center briefly before painting the image at full size. I've also seen the image appear full size but way off center before snapping into place. Is there something that can be done to this code to smooth it out?

Part of the problem is that I could not find a way to just replace the graphic within the HTMLDocument. I got the code to work to modify the document's HTML without just completely replacing it, but the image would not repaint when swapped into the document unless the new image had different dimensions than the one being replaced. Changing the IMG ID didn't help. I had to goose the screen size to get that to work, which is somewhat less than professional. So this is a possible second approach to solving the problem: Find a way to swap in the graphic but make the editor realize it has new content.

I also tried a CardLayout with two JTextPane controls, but I found the same issues, and in some cases it never did paint right.

Extra credit for ideas on how to implement cool transition effects. I have a feeling Swing wasn't set up for that. I don't know how much of a challenge it would be to take over painting...

BTW, I'm using Java 1.6.0_21. It was a fun program to write. Enjoy and thanks for any tips!


 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see a number of things I don't like about that code, chief of which is that you are either calling Thread.sleep(...) on the EDT, or calling Swing constructors and methods off of the EDT. Here's a resource to learn more about that: Lesson: Concurrency in Swing.

Then, I don't really understand why you are using a text component to display an image. What's wrong with a JLabel and icon?

Third, you are apparently constructing an ImageIcon only to obtain the image's height. If you must load the image twice, you could save a little overhead -- and the likelihood of the image being not completely loaded when you query the height, possibly one cause of the resizing you observed -- by reading it to a BufferedImage with ImageIO.

Fourth, you are needlessly discarding away any information contained in any Exception by throwing a new IllegalArgumentException from the catch block. If your design requires this (it shouldn't), at the very least you should print or log the stack trace of the original Exception.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Extra credit for ideas on how to implement cool transition effects.



Did you copy that from your assignment, or was that meant for us?

You can create transition effects using a Timer and various approaches available in Java 2D, or you can migrate your application to JavaFX, where transitions are available 'out of the box' -- but note that JavaFX Script is not going to be supported very much longer, and JavaFX 2.0 is in its first public beta.
 
Chris Dole
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daryl,

Thanks for your responses. I chose JTextPane because there's going to be text as well as graphics. I was just trying out graphics to get started. I find it best to see how smaller things work before getting to the bigger things.

Thanks also for the pointers to further resources. I'm not new to Java development, though I am new to Swing. I'm aware of the issues you pointed out. This was just a quick and dirty learner project. I wasn't putting it out there for a general code review.

Having said all that, let's get back to the main thrust of this post. I was hoping to learn more about JTextPane and if there was anything I could do with it, specifically, to alleviate the flickering issue. I outlined some of the things I tried, based on research into other similar posts. I have exhausted my resources. So I'm reaching out to get the benefit of those who are experienced with Swing.

So if you, or anyone else, has some information that's more on topic, I'd be glad to hear it.

Thanks,
Chris
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Darryl's advice was perfectly on topic. Follow his advice and then tell us if you still run into troubles.
 
They weren't very bright, but they were very, very big. Ad contrast:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic