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 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 61

Thread: Help with checking JButton positions please

  1. #26
    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 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?

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    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.

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

    Default Re: Help with checking JButton positions please

    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

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    how do I convert these row/col to get the source ?
    I posted an example in post#27
    button[4][7] would return a reference to the button 2 col to the right.
    Compute the indexes and use them to index into the button array.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Help with checking JButton positions please

    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?

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    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.

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

    Default Re: Help with checking JButton positions please

    Quote Originally Posted by Norm View Post
    Try this:
    JButton leftbtn = button[button1rowLeft][button1column];
    That still comes up with the error 'array required. but java.swing.JButton found' ?

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    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.

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

    Default Re: Help with checking JButton positions please

    "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"

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    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.

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

    Default Re: Help with checking JButton positions please

    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

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    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.

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

    Default Re: Help with checking JButton positions please

    Sorry about the long reply,

    JButton leftbtn = button[button1rowLeft][button1column];

    [button1column]
    ^

    it gives me an error on that line specifically [button1column]

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

    Default Re: Help with checking JButton positions please

    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

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    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.

  16. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    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.

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

    Default Re: Help with checking JButton positions please

    yes it is deffinatly a 2D array because I used :

    JButton[][] button = new JButton[8][8]; //Sets new JButtons

  18. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    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.

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

    Default Re: Help with checking JButton positions please

    What do you mean definition of button sorry?

  20. #45
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    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.

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

    Default Re: Help with checking JButton positions please

    Yes that was my problem thank you

    public static JButton[][] button; -> I had to change it to [] []

    Now
    JButton leftbtn = button[button1rowLeft][button1column];
    works, but now, my leftbtn has something say [3,3]

    How then can I get the source for this?

  22. #47
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    then can I get the source for this?
    Please explain. What do you mean by "source"?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Help with checking JButton positions please

    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

  24. #49
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with checking JButton positions please

    know if the button has an image or not,
    Get a reference to the JButton object and call its methods to determine its state.
    This statement sets leftbtn to the button object:
    JButton leftbtn = buttons[button1rowLeft][button1column];
    Then you can use leftbtn to call the methods for the object and determine its state.

    code gives me a null pointer exception
    Where does the code assign any values to the array that was being accessed?

    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.

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

    Default Re: Help with checking JButton positions please

    To call the method would that be :

    int leftHole = (Integer) leftbtn.getClientProperty("ImageIcon");

    Because that still doesn't work, what method would you suggest?

Page 2 of 3 FirstFirst 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