Thanks for the reply :)
when I do:
Code :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?
Printable View
Thanks for the reply :)
when I do:
Code :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.
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#27Quote:
how do I convert these row/col to get the source ?
Compute the indexes and use them to index into the button array.Quote:
button[4][7] would return a reference to the button 2 col to the right.
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:
Code :JButton leftbtn = button[button1rowLeft][button1column];
Please post the full text of the error message and the source code where the error happens.
"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:
Code :TestSorts.java:138: cannot find symbol symbol : variable var location: class TestSorts var = 2; ^
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?
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.
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.
yes it is deffinatly a 2D array because I used :
Code :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.
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;
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]Code :JButton leftbtn = button[button1rowLeft][button1column];
How then can I get the source for this?
Please explain. What do you mean by "source"?Quote:
then can I get the source for this?
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.Quote:
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.Code :JButton leftbtn = buttons[button1rowLeft][button1column];
Where does the code assign any values to the array that was being accessed?Quote:
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?
To call the method would that be :
Code :int leftHole = (Integer) leftbtn.getClientProperty("ImageIcon");
Because that still doesn't work, what method would you suggest?