• 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

Image not appearing on button

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(edit: the last post addresses the question I am having. The first two are about a question that I answered myself. Sorry for being messy)

Hi all,

I am having trouble referencing a file in my home folder. The file contains an image that I am using as the ImageIcon for a button. Here's my code to build the button:



And here's the code where I reference the file:




I am getting a null pointer exception on the line with the filepath, which I take to mean that the filepath is incorrect for the image file.

The image file is in a folder called buttons, which is located in the main folder of my program (not the src folder, but the folder that contains the src folder).

I have looked and looked and looked, but I can't find anything definitive on the syntax to reference this filepath.

Can anyone help me out? Thanks in advance!
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind, it turns out I was referencing the buttonPanel.getWidth() before it had been initialized.
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, I take that back :/ The frame opens with the buttons in it, but none of the buttons have the image I'd like them to have, but are just blank buttons. Any body see anything obvious with my code that might make this happen?
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I've been playing with it, and still no image displayed on the buttons. This is where my code currently stands:

Not sure what to do, so any help you could give would be great!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the way you specify the path is relative to the application's 'current directory.' What is likely happening is that the current directory is not the directory you think it is (you seem to think it is your home directory, but there is very little chance of that).

What you should do is package the images into your project / JAR and use them as a resource. Use Class#getResource() (or the related ClassLoader#getResource()) method to load it. Note, the images should be in your classpath, not a random part of your file system.
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure that's the answer, just because when I run the program, it compiles fine and throws no errors, which means that the program can recognize where the images are and is simply not loading them onto the buttons (right?). Is there some command which makes the images show up? Maybe something to do with validate(), or something similar? I've tried using validate in various parts of the process and nothing happens, but a tutorial I watched on adding images to buttons said it was necessary. All the tuts I see seem to convey that this is a pretty straightforward process, so I am guessing it's something simple that I am missing, but for the life of me I can't figure out what. Thanks for the help!

edit: I was thinking that maybe it had to do with the size of the image vs the size of the button? I'm not sur ethis is right either as a tutorial I read said that if that was the case, part of the image would still show up, and I'm getting nothing.

edit: I attached a screenshot of the button package I put into the src folder just to verify that I am referencing it correctly. The code that references it is this:
like I said, I'm not getting any runtime or compile time errors, so I'm hopeful that this part of the code is correct, but if not, please clue me in.
screenshotLento.PNG
[Thumbnail for screenshotLento.PNG]
Screenshot of button resource package
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

james falk wrote:I'm not sure that's the answer, just because when I run the program, it compiles fine and throws no errors, which means that the program can recognize where the images are and is simply not loading them onto the buttons (right?).


The compiler never cares about checking existence of files, so it compiles fine does not mean the image's path is correct!


james falk wrote:Is there some command which makes the images show up? Maybe something to do with validate(), or something similar?


There is no such complicate things for show up images on buttons. It's pretty simple if you do it right. As Steve Luke pointed out, you need to check your image's path. Surely you did something wrong with it.
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still having trouble with this. Here's where I am with the code:


Button class:




GUI Panel/Frame set up:




Button creation:



Driver class:



I can't figure out what I am doing wrong here, but the images are still not showing up on the buttons. Can someone enlighten me?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any code that actually adds the JButtons to your GUI. Can you show us that part?
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed the problem. I just created the buttons as JButtons instead of using my own Button class and that worked. The filepaths I was using were good, just for reference's sake. Thanks for all the help everyone.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic