This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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!
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.
There are no new questions, but there may be new answers.
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.
Joined: Jun 14, 2011
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.