[Logo]
Forums Register Login
Image not appearing on button
(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!
Nevermind, it turns out I was referencing the buttonPanel.getWidth() before it had been initialized.
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?
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!
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.
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
Screenshot of button resource package
[screenshotLento.PNG]
 
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.
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?
I don't see any code that actually adds the JButtons to your GUI. Can you show us that part?
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.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Dec 12, 2017 04:01:25.