File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes GUI ~ Image not showing up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI ~ Image not showing up " Watch "GUI ~ Image not showing up " New topic
Author

GUI ~ Image not showing up

Debby Miller
Greenhorn

Joined: Feb 29, 2012
Posts: 7
My image isn't showing up as a label on my GUI business. Do I have something in my code overriding it? Did I code it wrong? My other program with an image as a label works fine. I have verified that I'm using the correct file, and file extension, and that it is in the correct location. As far as research is concerned, I've been reading about ImageIcon to see if there is anything that clashes with it, as all the book does is skim (at best) over graphics in GUI's. Am I even looking in the right place? Thanks in advance for any help/advice you can offer.



[Edit - added code tags - see UseCodeTags for details]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Welcome to the Ranch

I am afraid you will have to provide more details. Where is the image file? Can you load it? Which component is it supposed to be displayed on? Have you used print statements to get debugging information?
I had to edit your code because it has long lines in, which are too wide for a screen.

All sorts of things about your code, I am afraid.
  • 1: Even without reading all your code, I think you have a God class. One that is almighty and tries to do everything. Not usually a good idea at all.
  • 2: You have all sorts of local variables in the main method which appears never to be used; they simply go out of scope.
  • 3: You have lots of fields in the recipe class. I suspect most of them can be local variables in the code which sets up the GUI.
  • 4: Another thing you see in some books, addActionListener(this). That is liable to lead to awkward, non-object-oriented programming. Create your listeners as separate classes.
  • 5: You are using the == operator on reference types (== "Tacos"). That is very unreliable; you should use an equal...() method.
  • I am pleased to see you are not making your principal class extend frame or similar, and that you are using invokeLater.

    I think this discussion would fit better in our GUIs forum, so shall move it.
    Debby Miller
    Greenhorn

    Joined: Feb 29, 2012
    Posts: 7
    Thanks! I'm super new (obviously). You bring up some really great points. I'll work on the stuff you pointed out first, and then come back if my question is still relevant. BTW this place is awesome!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36478
        
      16
    You’re welcome
    How have you got on with it?
    Debby Miller
    Greenhorn

    Joined: Feb 29, 2012
    Posts: 7
    Well, what happened was I wrote the program at home implementing new stuff I learned at school, so every time I learned something (i.e. if-else statements) I would add it to the program. The problem with that is when I learned about using swing and GUI business I didn't know what to do with the rest of my code. So I just commented it out for now, which is why I had all those extra variables. As far as the God class problem is concerned, I've been thinking about how to break it down into separate classes and methods, but not sure where to start. I changed all the == to .equals, thank you for the heads up. I'm trying to figure out how to create my listeners as separate classes, but to do that I think I need to really understand what a listener is first. My book has a paragraph on it, so I think I need to read more on the internet. This place is soo much nicer than the other forum I was introduced to, thanks again for all your help.

    ~Debby
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: GUI ~ Image not showing up
     
    Similar Threads
    Changing Text of JLabel Daynamically
    please help .. please
    Background color change help
    Jlabel creation problem. Do guide
    Won't repaint!