-
Help with checking JButton positions please
Hi guys, I have got an Array of JButtons, that are in a gridLayout 7,7.
I have added an actionListener to these buttons that at the moment only prints out "Button clicked"
At the moment some of these JButtons have an ImageIcon on them and some of them are blank.
For example if I have three JButtons in positions:
[3,3] [3,4] [3,5]
[] [X] [X]
The first is blank and the rest have a ImageIcon.
What I want to happen is when a user clicks on a button with an ImageIcon ([3,5]) and then clicks on a button that is blank ([3,3]) the JButton will affectivly jump over the middle button and will then be at position [3,3] :
[3,3] [3,4] [3,5]
[x] [] []
I know I can do this by making the button have an ImageIcon and make the others blank to have the affect it is being 'jumped over'.
What I want to know is how can I check the position of the JButton the user has click and if it is in a line with the other Jbutton?
To make it clear if a user clicks JButton [3,5] and then wanted to 'jump' it to [0,1], I need a test to show this is not allowed. The JButton is only allowed to move twice to the left, right, up or down depending if the immediate button is 'full' and the button after is 'blank'.
I really hope this makes sense.
1. Would I write code to check what the position in the array of the JButton that the user has clicked is.
2. Then check the second position of the second button user has clicked //How would I check if the user clicked to the left,right,up or down?
Thanks
-
Re: Help with checking JButton positions please
You could simply use a different ActionListener for each JButton (or check which instance it is in a single ActionListener), then call a function with the location of that button.
Or you could just loop through the array and check which instance of JButton matches the ActionEvent's source.
-
Re: Help with checking JButton positions please
I have added a mouseListener on each of the JButtons:
Code :
public void mouseClicked(MouseEvent e)
{
JButton source=(JButton)e.getSource();
System.out.println(source);
}
This will allow me to see the location of each button clicked.
Could you point me in the right direction on how I go about clicking one button then clicking another and checking if that JButton has an ImageIcon or not?
Thank you
-
Re: Help with checking JButton positions please
I don't really know what that MouseListener is going to get you.
I already pointed you in a couple directions. Plus you're going to have to store a short history of button presses- this might just be the last button pressed, depends on your needs.
-
Re: Help with checking JButton positions please
A couple of ways to have the button objects save data about their state or location.
Extend the JButton class and have the class save the data you want. In the listener cast the source object to your wrapper class and you'll be able to call its methods to get the data.
Use the JComponent class's client property interface to save data inside the object.
-
Re: Help with checking JButton positions please
thanks for the replies, but I am still really not sure, I have been trying to figure this out for ages.
I have 3 buttons, two full, one null:
[] [x] [x]
I click the 3rd button using
Code :
.addActionListener(this);
(when I first created the buttons so each button has a actionListener)
Then in the actionPerformed :
Code :
public void actionPerformed(ActionEvent e) {
System.out.println("Click the button you want to move this piece to");
}
}
I tried putting another actionListener for the button that has just been clicked, so when another button is clicked ie:
[] [x] [x] <- pressed (ActionPerformed -> "where do you want to move the piece" -> ActionListener -> this.JButton.setIcon(null) && JButton to the left setIcon null, but not sure how I would know what that is called without it ever being clicked on?
clicked ->[] [x] [x] (ActionPerformed -> this.JButton.setIcon(piece); -> Result:
[x] [] []
-
Re: Help with checking JButton positions please
Quote:
not sure how I would know what that is called without it ever being clicked on?
Can you explain what that means? If there is no click, there is no call to a listener.
Can you post a small program that compiles, executes and shows the problem?
-
Re: Help with checking JButton positions please
I click on [0][0], want to 'move' it to [0][2] jumping over [0][1] -> which means [0][0] & [0][1] are now null and [0][2] has the image
-
Re: Help with checking JButton positions please
Are you asking how to click on one button and set it as the source
and then click on another button and then move something from the first clicked button to the second clicked button?
Quote:
I click on [0][0], want to 'move' it to [0][2] jumping over [0][1] -> which means [0][0] & [0][1] are now null and [0][2] has the image
The above doesn't explain how the target button is chosen.
Is the target always two columns to the right of the clicked button?
What do you mean by "null"? A button without an image?
-
Re: Help with checking JButton positions please
Yes, sorry that is me not explaining properly. Null means a button without an image yes. And also it is always two columns / rows, it can be up, down, left and right, but always two spaces, thanks
-
Re: Help with checking JButton positions please
Is this it then: When a button is clicked on, get its location and move its image icon to the button that is 2 columns to the right.
Can you explain what problem you are having doing this? I see that the buttons all save their row,column locations using client properties. Get those values, add 2 to the column, test bounds and then get the target button's reference at the location in the array of buttons and do the get and sets to move the icon.
-
Re: Help with checking JButton positions please
To be honest I'm not really sure where to start with this, you say that the buttons save their row and columns using client properties, i'm not sure how to get them.
-
Re: Help with checking JButton positions please
I thought that you had written the code that you posted in post #8. That code saves values with the button component's client properties. Those values are saved in a sort of map. Use the client properties corresponding get method to retrieve the values.
Read the API doc for the put method for sample code.
-
Re: Help with checking JButton positions please
I have gone through the API as you said.
So from:
Code :
public void actionPerformed(ActionEvent e) {
JButton src = (JButton) e.getSource();
System.out.println(src);
}
}
That will print out the position of the button just for my reference. Then I can use the getClientProperty to return the value of the JButton?
And then put that into the new JButton ?
Sorry if I am not following
-
Re: Help with checking JButton positions please
Quote:
Then I can use the getClientProperty to return
the value that was written to the button with the putClientProperty() method. The buttons location in a row and column variable.
See post#11
There is a reference to the event button. The button contains its location as row and column. Take that row and column+2 and index into the array to get a reference to the desired button
-
Re: Help with checking JButton positions please
I will have to have a go tomorrow, thank you very much again
-
Re: Help with checking JButton positions please
-
Re: Help with checking JButton positions please
Just thought I would say thank you so much for your help and letting me know about the client properties. I have read over it lots today and have made so much progress. I haven't got stuck at the moment but will keep you posted as I will be doing lots more tomorrow.
-
Re: Help with checking JButton positions please
Ok, you're welcome. Good luck.
-
Re: Help with checking JButton positions please
Hi, thanks for all your help I feel I have done & learnt so much from what you last told me.
I have one last problem. I have done the code that allows me to click one button, click the next button and check if it is 2 rows/columns away, if the first clicked button has an image and the second clicked button doesn't have an image.
If all of these are true then the image is moved.
My last argument needs to say, for this example: "If the button clicked, & the button to the left of that button has an image then carry on..."
if the button has an image or doesn't have an image.
My problem is, how can I get the properties of the image that hasn't been clicked and that's specifically to the left of the button I did just click.
I can post my code if you need.
Thanks again
-
Re: Help with checking JButton positions please
Quote:
how can I get the properties of the image that hasn't been clicked and that's specifically to the left of the button I did just click.
When you get the row,col location of the clicked button, you can use that in the array to get to any other button in the array by changing the row and/or col values.
-
Re: Help with checking JButton positions please
-
Re: Help with checking JButton positions please
Quote:
how to check if there is an image in the location
Given a reference to a JButton object (get that from the array using its r,c location), you can call its methods and determine its state.
Look at the API doc for the JButton class to see what methods will do what you want.
-
Re: Help with checking JButton positions please
Hi, i'm really sorry but I have looked a the JButton API doc, I found 'checkHorizontalKey' method, but I am not sure if this is correct as I tried using it.
I can get the buttons row/col locations, but they are both stored in different variables, and to test it works I do this:
System.out.println("The button to the LEFT : " + "[" +button1rowLeft + "," + button1column + "]");
which would output say [4,5] if I clicked on JButton [4,6]
I'm not sure how to reference that specific JButton just from the row/col I have got?
I seem to have looked everywhere and tried so many things :/
I know its easy to get the JButton I just clicked on as :
JButton btn = (JButton) e.getSource();
gives me all the info I need to know, but without clicking on the 'button to the left' I have no idea :/
Thanks again
-
Re: Help with checking JButton positions please
Quote:
how to reference that specific JButton just from the row/col I have got?
This array needs to be a class variable so it can be used to get a reference to any of the buttons:
Code :
JButton[][] button = new JButton[8][8];
Given the row,col of a button, index into that array to get a reference to that button.