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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JButton Current Location on a JPanel

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Thanked 0 Times in 0 Posts

    Default JButton Current Location on a JPanel

    I created a JFrame that contained a 5x5 grid of JButtons. I made it to where only buttons directly one button up, down, left, and right could be clicked. I used the following code to check to make sure that the button being clicked was valid:

    if (pointer == (grid [2][0])){
    if (((pointer.equals(grid[0][1]) || pointer.equals(grid [1][0])) && (e.getSource().equals(grid[0][0]))) == true){
    pointer = grid [0][0];
    else if (((pointer.equals(grid[0][0]) || pointer.equals(grid[0][2]) || pointer.equals(grid[1][1])) && (e.getSource().equals(grid[0][1]))) == true){
    pointer = grid[0][1];

    I have now determined I want the JButtons in a JPanel connected to a SplitJPanel. I have done this successfully. However, now the buttons directly one block away from my "pointer" is marked as invalid. What is the equivalent code to the bolded code listed above but for a JPanel rather than a JFrame? I need to make sure that the CURRENT location of the pointer is found somehow so the program doesn't get confused and make the "pointer" go to the wrong adjacent buttons (a problem I ran into when doing just (if pointer == grid[#][#]).

    Thanks for the help and if any other information is needed I'd be happy to provide it.

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: JButton Current Location on a JPanel

    I can't think of an occasion when a GUI component acts differently on a JPanel than it does on a JFrame. Maybe in default characteristics like opaque versus transparent, but those are documented, and I'm not 100% sure exist.

    The logic you coded for the JButton grid should provide the same results when added to either a JPanel or a JFrame. Something else is causing the differences you've observed, and you haven't shown enough code for a third party - well, this third party - to determine the root cause.

    There's something odd about this if condition:

    if (((pointer.equals(grid[0][1]) || pointer.equals(grid [1][0])) && (e.getSource().equals(grid[0][0]))) == true)

    But I can't quite put my finger on it. I think it's that the '== true' is unnecessary. It's like saying,

    if ( true == true ), or

    if ( false == true )

    when all that is needed is:

    if ( true ), or

    if ( false ).

    But that won't solve your problem. It's just a minor point.

Similar Threads

  1. Replies: 10
    Last Post: April 21st, 2013, 09:28 AM
  2. Help with NetBeans App using JPanel/JFrame with JButton (if statements)
    By deanbyrne95 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 3rd, 2013, 07:48 PM
  3. Adding JPanel using Jbutton, Expanding JFrame as needed.
    By ShadowMystic in forum AWT / Java Swing
    Replies: 1
    Last Post: February 24th, 2013, 09:13 AM
  4. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  5. JButton on JPanel
    By JavaLearner in forum AWT / Java Swing
    Replies: 4
    Last Post: March 26th, 2010, 07:46 AM

Tags for this Thread