Thanks for the reply
when I do:
JButton[][]
I still get the same 'Source' as I do for the button clicked even though it should be the left button of the button I just clicked?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Thanks for the reply
when I do:
JButton[][]
I still get the same 'Source' as I do for the button clicked even though it should be the left button of the button I just clicked?
What is the index into the button array for the clicked button?
Say it was 4,5. Then the button on the same row (4) and two columns to the right would be at column 7
Then the array element: button[4][7] would return a reference to the button 2 col to the right.
If you don't understand my answer, don't ignore it, ask a question.
I get that, but I'm not sure how to actually retrieve the source for the button?
When I create the buttons in the array I give each row/col a reference and then i give each row/col +1 & -1 to get the clicked buttons, right button, left button, above button and below buttons references.
Thanks again for your help
I posted an example in post#27how do I convert these row/col to get the source ?
Compute the indexes and use them to index into the button array.button[4][7] would return a reference to the button 2 col to the right.
If you don't understand my answer, don't ignore it, ask a question.
When I try and index it into the array :
____________________________________ edit:
and when I do System.out.println on leftbtn its output is: [[Ljavax.swing.JButton;@208ee9c9
Is there a way to getSource from that?
button[button1rowLeft][button1column];
refers to a JButton object, not to an array of JButtons.
Try this:
JButton leftbtn = button[button1rowLeft][button1column];
If you don't understand my answer, don't ignore it, ask a question.
Please post the full text of the error message and the source code where the error happens.
If you don't understand my answer, don't ignore it, ask a question.
"Array required, but javax.swing.JButton found
You are using syntax here that suggests that you are trying to access an array element. The variable you refer to is not an array,though"
Please post the FULL text of the error message that shows where the error happened and the source line with the error.
Here is a sample:
TestSorts.java:138: cannot find symbol symbol : variable var location: class TestSorts var = 2; ^
If you don't understand my answer, don't ignore it, ask a question.
I'm not sure what you mean, I am using eclipse and when I try and run the program it just says :
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
The type of the expression must be an array type but it resolved to JButton
at MyDrawingPanel$MyActionListener.actionPerformed(Ar ray.java:220)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
It is line 220
Where on the line with the error does the IDE put the indicator flag that points to the location of the error on the line? See the ^ in post #35. It's beneath where there error is: var
Where does the IDE point for the error it gives you?
If you don't understand my answer, don't ignore it, ask a question.
Sorry about the long reply,
JButton leftbtn = button[button1rowLeft][button1column];
[button1column]
^
it gives me an error on that line specifically [button1column]
I'm really struggling on this, I need to get this done by today and it is this one tiny bit that I need to do to complete my program
The error message could mean that the array only has one dimension not two.
Check that the definition for the array you are trying to use has 2 dimensions.
If you don't understand my answer, don't ignore it, ask a question.
The error message could mean that the array only has one dimension not two.
Check that the definition for the array you are trying to use has 2 dimensions.
If you don't understand my answer, don't ignore it, ask a question.
yes it is deffinatly a 2D array because I used :
JButton[][] button = new JButton[8][8]; //Sets new JButtons
You are not using that definition of button which is local to the constructor. Look for the definition of button that you are using.
If you don't understand my answer, don't ignore it, ask a question.
What do you mean definition of button sorry?
Where is the variable: button defined?
Every variable in a program must be defined before it can be used. Some variable definitions:
Object obj;
int anInt;
double aDbl;
If you don't understand my answer, don't ignore it, ask a question.
Yes that was my problem thank you
public static JButton[][] button; -> I had to change it to [] []
Nowworks, but now, my leftbtn has something say [3,3]JButton leftbtn = button[button1rowLeft][button1column];
How then can I get the source for this?
Please explain. What do you mean by "source"?then can I get the source for this?
If you don't understand my answer, don't ignore it, ask a question.
I am basically trying to act like I clicked on that button to easily get the information about it, as this code gives me a null pointer exception
I really hope I am explaining properly :/ I have no other way to explain sorry
Get a reference to the JButton object and call its methods to determine its state.know if the button has an image or not,
This statement sets leftbtn to the button object:Then you can use leftbtn to call the methods for the object and determine its state.JButton leftbtn = buttons[button1rowLeft][button1column];
Where does the code assign any values to the array that was being accessed?code gives me a null pointer exception
What did you do when you changed the code shown in post#46?
What did you do with the local definition for button that I talked about in post #43?
If you don't understand my answer, don't ignore it, ask a question.
To call the method would that be :
int leftHole = (Integer) leftbtn.getClientProperty("ImageIcon");
Because that still doesn't work, what method would you suggest?