Hi all. I was setting up a template to make it easier for layout designs, I was having fun until it turn very nasty when I tried to display a .jpg image inside a BorderLayout using JFrame. Compiles okay, but won't display the image. Can some gurus have a look please. Here is the code, I've highlighted the problematic bit:
There's a couple of strange things here. First, JFrame is a top-level container, which means it is suited for containing an application itself. It is probably not the best choice for inserting into another container. Next, the call to getToolkit().getImage returns immediately. You need to use a MediaTracker instance to watch the loading of the image and let you know when it is 100% and ready for display. Finally, why are you creating a new frame in paint()? paint() can be called hundreds of times if a frame is moved or something is dragged on it. I think you should check out the Swing Tutorial. It has a section on graphics which will explain the details of working with images.
Thanks, I'm actually working through it hoping to find the relevant bits and not covering the same ground over and again. Just when I thought I had it well under control, I learn that displaying images in JAVA is perhaps one of the most difficult thing. My productivity will drop down to zero as I come to grips with it. I think JAVA so complicated, it is too clever by half.
I'm not "dissin" JAVA, I think the idea is great, best thing since sliced, whole-meal, multi-grain bread. Thanks for the JFrame thing, but I am trying to embed an image in a container. My research suggests JTextArea. Joe Ess pointed out correctly that JFrame is a top level container and I cannot embed JFrame in a BorderLayout. Thanks for the input.
Embed an image in a container? Are you talking about making a "wallpaper" for your application? This is easily done, but you must override the correct component. A search for "background image" in this Forum yeilds 80 results. The best approach I have seen is by Nathan, one of our wonderful bartenders. His approach can be found here. Notice the method paintComponent in his custom class. That method is the key if in fact you are trying to wallpaper your app. Hope this helps.
Thanks for taking time to look at my problem. I'm also reading up John Zukowski's excellent "Java 2 J2SE1.4", Chapter 10 "Animation and Images" goes in detail on loading an image with media traking. However it seems an overkill at this point. I am just trying to load an icon to display on the screen from the applet I posted earlier. I think the best way to go is use a JTextArea, but now the Java Console is telling me I've an "access privilege" error.
You can't put an image in a JTextArea. Do what Earnest said to do, or use the approach I suggested by Nathan. There should ne more guessing on how to do this. Just do it.
Hi Gregg, I'm trying to understand the way its done, some of the codes are very advanced for me. Here is the code for displaying an image in a JTextArea, unlike my code, it has the benefit of working:
Now here is my tortured code, still trying to put an image into the Borderlayout. It compiles fine, JAVA console displays no error messages but no image, can some see what's wrong with it :
Can someone please explain to me where I've gone wrong. I've made, on a trial and error basis, so many modifications and still no image. No compilation errors, java console shows no exceptions, the html displays the borderlayout with some fancy add-ons, but no picture. If only the java console throws an exception, I can handle that 'cause I'll know where I'm at! But no, not even an exception message.