james falk

Ranch Hand
+ Follow
since Nov 02, 2012
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by james falk

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.
8 years ago
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?
8 years ago
Cool beans. Glad to be able to help.
8 years ago
Hi all,

I am wondering how I reference a file that's in my source folder. I have a folder called 'buttons' that my source folder contains, and that folder contains a bunch of images I am trying to use as the ImageIcon for a JButton.

Right now, I am doing this:



and it's not working. I don't know if my syntax for referencing the image location is incorrect or not, so I need an experienced set of eyes to tell me yay or nay.

Thanks for any help you can offer!
8 years ago
To compile your program, a lot of people make something called a makefile, which isn't too complicated and you should look into when you have second, but for your purposes you can probably just compile it yourself from the terminal using the command 'javac fileNameOfDriverClass'. You have to be in the directory containing the driver class file to do this. All other associated files should be in the same directory too. Then type the command 'java fileNameOfDriverClass filename1 filename2' (<---your args array Strings) to run it from the terminal. I'm kind of new to Java too, but I had to do this numerous times and that's how I did it.
8 years ago
The only way I know how to do it is running the program from a terminal. If you are using Linux, you can just run the program from your terminal, if you are using a Mac, you should have a terminal too, and in Windows you just use the command prompt. Eclipse may have direct terminal interface, but I don't know of any. So you would just compile your program and test it out from whatever terminal interface your operating system works with.
8 years ago
Maybe I can rephrase it. You should have a part of your program that has something very similar to this:



The key here is the String array called 'args'. That String array is comprised of different Strings that the program 'gets' from whomever is running the program. When someone runs the program from the command line, they will type java, then the name of the program, and then they can enter in characters, and those characters, whether they be file names or any other relevant data that the program might need to know to run properly, those characters will be stored in the aforementioned String array called 'args'.

So, inside your program you can reference the String array 'args' and get that data to use as you need to. So, for instance, if someone had run the program and typed "java yourProgram firstFileName secondFileName" (quotes only for clarity, they don't actually type the quotes), inside of your driver class you could reference the file name by referencing the String array 'args' by doing something like:


The first String inside the array (at location '0') is now assigned to the String variable called 'filename', and given my previous example would contain the String: firstFileName and args[1] would contain the String: secondFileName. Each subsequent String (if there were more than two) would be the next String in the 'args' array.

You can then use the file name however you see fit within your program. You can actually skip this step and just reference the String inside args[0] by typing 'args[0]' every time you need to reference the first argument entered after the name of the program was typed on the command line, but I like to reassign it just so I can have the variable name associated with the String have a more descriptive meaning.

Note that you can enter in as many arguments as you like when running the program from the command line, so the String array 'args' could contain as many pieces of data as you need.
8 years ago
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.
8 years ago
I just posted a question, thought I solved it but didn't, and now I'd like to go back and unmark it as 'solved'. Is there a way to do this, or am I better off just re-posting the question?
8 years ago
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!
8 years ago
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?
8 years ago
Nevermind, it turns out I was referencing the buttonPanel.getWidth() before it had been initialized.
8 years ago
(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!
8 years ago
Yeah, sorry about that. I figured out what I was doing wrong, had another question, and thought I could edit it before anyone responded. Sorry for the confusion. Thanks for the help btw.
8 years ago
Hey all,

I am having some trouble with an array. I am wondering how I initialize a 2d array with values? For instance, if I want to create a 2d array with values 1-10 in each of the arrays, how do i do that?

Here's what I tried:

But this gave me wonky results...

Any thoughts?
8 years ago