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.

Page 1 of 3 123 LastLast
Results 1 to 25 of 61

Thread: Help with checking JButton positions please

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with checking JButton positions please

    I have added a mouseListener on each of the JButtons:

    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

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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
    .addActionListener(this);
    (when I first created the buttons so each button has a actionListener)

    Then in the actionPerformed :

    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] [] []

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with checking JButton positions please

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with checking JButton positions please

    I have gone through the API as you said.

    So from:

     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

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with checking JButton positions please

    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
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    sim18 (December 1st, 2012)

  17. #16
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with checking JButton positions please

    I will have to have a go tomorrow, thank you very much again

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with checking JButton positions please

    see you tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with checking JButton positions please

    Ok, you're welcome. Good luck.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #20
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

  22. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with checking JButton positions please

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #22
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with checking JButton positions please

    edit

  24. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with checking JButton positions please

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #24
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

  26. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with checking JButton positions please

    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:
    JButton[][] button = new JButton[8][8];
    Given the row,col of a button, index into that array to get a reference to that button.
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 3 123 LastLast

Similar Threads

  1. help to checking the error!!
    By vitorloke in forum Java Theory & Questions
    Replies: 12
    Last Post: April 27th, 2012, 09:46 AM
  2. Checking Blurbs
    By 9erNumber16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2011, 07:29 PM
  3. Need help with checking passwords
    By kb1213 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 12th, 2011, 05:32 PM
  4. Checking in.
    By Johannes in forum Member Introductions
    Replies: 7
    Last Post: August 13th, 2009, 06:11 AM
  5. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM